# Reducing memory allocations when storing vectors to fields of a mutable type

**URL:** https://discourse.julialang.org/t/reducing-memory-allocations-when-storing-vectors-to-fields-of-a-mutable-type/64623
**Category:** Performance
**Created:** [July 14, 2021, 9:23am UTC](https://discourse.julialang.org/t/reducing-memory-allocations-when-storing-vectors-to-fields-of-a-mutable-type/64623 "2021-07-14T09:23:22Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![LionMod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lionmod/32/22149_2.png) [@LionMod](https://discourse.julialang.org/u/LionMod)
#### Post date: [July 14, 2021, 9:23am UTC](https://discourse.julialang.org/t/reducing-memory-allocations-when-storing-vectors-to-fields-of-a-mutable-type/64623/1 "2021-07-14T09:23:22Z")

</div>

I have a mutable type called `Data`

```julia
mutable struct Data
        data_a::Vector{Int64}
        data_b::Vector{Int64}
end

```

I get two vectors that are of type `Vector{Float64}` from a solution of an ODE and want to construct the type `Data` field’s being the two vectors.

I am using a function to do so something like this

```julia
function store_vectors!(input::Data, solution)
    times = 0.0:0.01:10.0
    states = solution(times)

    input.data_a = [state[1] for state in states]
    input.data_b = [state[2] for state in states]
end

```

The ODE used `StaticArrays.jl` to improve the performance which is working as intended but as soon as I store the vectors in `Data` the allocations jump considerably and I feel like there is a way to decrease the allocations.

Any thoughts/ideas as to ways to improve this?

I was initially thinking to initialise the type `Data` with a fixed size `Vector` and then assigning data to it.

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [July 14, 2021, 9:33am UTC](https://discourse.julialang.org/t/reducing-memory-allocations-when-storing-vectors-to-fields-of-a-mutable-type/64623/2 "2021-07-14T09:33:56Z")

</div>

Unless you intend to reassign the fields of `Data`, you can make the struct immutable and simply return a new `Data` object.

Your function allocates because `[state[1] for state in states]` creates a new array. Depending on the type/shape of `states`, this may be superfluos (what is the type of `states`?).

---

<div class="post-metadata">

### Author: ![LionMod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lionmod/32/22149_2.png) [@LionMod](https://discourse.julialang.org/u/LionMod)
#### Post date: [July 14, 2021, 10:16am UTC](https://discourse.julialang.org/t/reducing-memory-allocations-when-storing-vectors-to-fields-of-a-mutable-type/64623/3 "2021-07-14T10:16:57Z")

</div>

`Vector{SVector{4, Float64}}`

And I’m having issues slicing it.

---

<div class="post-metadata">

### Author: ![LionMod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lionmod/32/22149_2.png) [@LionMod](https://discourse.julialang.org/u/LionMod)
#### Post date: [July 14, 2021, 12:51pm UTC](https://discourse.julialang.org/t/reducing-memory-allocations-when-storing-vectors-to-fields-of-a-mutable-type/64623/4 "2021-07-14T12:51:44Z")

</div>

I may have found a solution.

Rather than using `state = solution(times)` and then allocating them.

I can take `solution` from the input and slice it e.g. `solution[1, :]` and use `setfield!`.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [July 14, 2021, 12:57pm UTC](https://discourse.julialang.org/t/reducing-memory-allocations-when-storing-vectors-to-fields-of-a-mutable-type/64623/5 "2021-07-14T12:57:14Z")

</div>

> [@LionMod](#):
>
> `input.data_a = [state[1] for state in states]`

This line allocates a new array for the right-hand-side every time you execute it, so it’s hardly surprising that it will allocate. It has nothing to do with whether you store the result in a `mutable struct`.

If you want to avoid allocation, you need to overwrite a pre-allocated vector. If you don’t know the size that you need in advance, you can always resize it as needed, for example:

```julia
resize!(input.data_a, length(states)) .= getindex.(states, 1)

```

---

<div class="post-metadata">

### Author: ![LionMod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lionmod/32/22149_2.png) [@LionMod](https://discourse.julialang.org/u/LionMod)
#### Post date: [July 14, 2021, 1:33pm UTC](https://discourse.julialang.org/t/reducing-memory-allocations-when-storing-vectors-to-fields-of-a-mutable-type/64623/6 "2021-07-14T13:33:15Z")

</div>

Thank you @stevengj . This is what I had in mind but couldn’t figure out how to do so.

I’m guessing if size is known the pre-allocated vector can be overwritten using something like

`input.data_a .= *insert vector of correct type and length here*`

With the key bit being the `.=` which is used to “reuse” existing memory?

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [July 14, 2021, 2:43pm UTC](https://discourse.julialang.org/t/reducing-memory-allocations-when-storing-vectors-to-fields-of-a-mutable-type/64623/7 "2021-07-14T14:43:53Z")

</div>

> [@LionMod](#):
>
> input.data\_a .= _insert vector of correct type and length here_

No. The right hand side cannot be a _new_ vector. If you want to avoid allocating, it has to be a “dot call” broadcast operation that is “fused” with the assignment on the left-hand-side.

See the manual: [Functions · The Julia Language](https://docs.julialang.org/en/v1/manual/functions/#man-vectorized)

(Or just write a loop, of course.)
