# Efficient design for library of objects

**URL:** https://discourse.julialang.org/t/efficient-design-for-library-of-objects/26890
**Category:** New to Julia
**Tags:** performance, design
**Created:** [July 27, 2019, 6:18pm UTC](https://discourse.julialang.org/t/efficient-design-for-library-of-objects/26890 "2019-07-27T18:18:30Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Julia\_coder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/julia_coder/32/8960_2.png) [@Julia\_coder](https://discourse.julialang.org/u/Julia_coder)
#### Post date: [July 27, 2019, 6:18pm UTC](https://discourse.julialang.org/t/efficient-design-for-library-of-objects/26890/1 "2019-07-27T18:18:30Z")

</div>

Hello,

I’m trying to make a library of user-constructed mutable structs which I can quickly search through, extract values from, and add new structs to. The most obvious design to me is to put each struct object into an array.

Is there an efficient way to search through a specific field of each object in the array? Alternatively I am wondering if it would be more sensible (and faster) to replace the array single struct where the fields are arrays of values from each struct? Or perhaps dictionaries would be helpful?

Thanks in advance.

---

<div class="post-metadata">

### Author: ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)
#### Post date: [July 28, 2019, 10:43am UTC](https://discourse.julialang.org/t/efficient-design-for-library-of-objects/26890/2 "2019-07-28T10:43:16Z")

</div>

Searching through a specific field can be done with `findfirst()`/`findlast()`/`findall()`:

```julia
# finds all indices of A where A[i].fieldname == 5
findall(x->x.fieldname==5, A)

# a wrapper for convenience
function findfield(predicate::Function, A::AbstractArray, name::Symbol) 
    findall(x->predicate(getfield(x, name)), A)
end

```

Your second question is the classic “array of structs vs. struct of arrays” dilemma. Depending on the use patterns, one or the other might be better. SoA is more cache- and SIMD-friendly if you only want to process one field at a time. AoS is better when multiple fields have to be processed at once. AoS also makes algorithms like sorting much easier to write.

---

<div class="post-metadata">

### Author: ![Julia\_coder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/julia_coder/32/8960_2.png) [@Julia\_coder](https://discourse.julialang.org/u/Julia_coder)
#### Post date: [July 29, 2019, 4:53pm UTC](https://discourse.julialang.org/t/efficient-design-for-library-of-objects/26890/3 "2019-07-29T16:53:18Z")

</div>

That’s very helpful, thank you!
