# Strategies to reuse memory

**URL:** <https://discourse.julialang.org/t/strategies-to-reuse-memory/74743>\
**Category:** New to Julia\
**Tags:** question, memory-allocation, functions\
**Created:** [January 17, 2022, 9:11am UTC](https://discourse.julialang.org/t/strategies-to-reuse-memory/74743 "2022-01-17T09:11:16Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dharik\_Arsath](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dharik_arsath/32/27461_2.png) [@Dharik\_Arsath](https://discourse.julialang.org/u/Dharik_Arsath)\
**Post date:** [January 17, 2022, 9:11am UTC](https://discourse.julialang.org/t/strategies-to-reuse-memory/74743/1 "2022-01-17T09:11:16Z")

</div>

I’ve a preallocated vector temp which has the values of (y - y\_pred ).^2 where y and y\_pred are 1D vectors.

I’ve to reuse that vector in 2 or more different functions, what is the best solution for this. Note: performance matters✏️

eg:

const temp = Vector{Float64}(undef, size(x, 1))

function mse(x::Vector{Float64}, y::Vector{Int}, w::Float64, b::Float64)

```
for ind in eachindex(y,y_pred)
    temp[ind] = (y[ind] - y_pred[ind]) ^ 2 # (y - y_pred)^2
end 

```

end

function batchgd(x::Vector{Float64},y::Vector{Int},w::Float64,b::Float64, lr::Float64, threshold = 0.00001, max\_iter = 10000)

> ```
> for ind in eachindex(y, y_pred)
> temp[ind] = sqrt(y[ind] - y_pred[ind]) # since we have squared value we do sqrt.(reusing memory)
> end
> 
> ```

end

Here I reuse memory temp with minor change i.e) already I have squared value in temp and so in the function batchgd I do sqrt. so my question is what is the best way to reuse memory.

1. Creating a vector globally and calling in wherever required
2. computing y - y\_pred everytime within each function where needed
3. returning the vector from one function and using them in another.
4. is there any better way.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [January 17, 2022, 9:25am UTC](https://discourse.julialang.org/t/strategies-to-reuse-memory/74743/3 "2022-01-17T09:25:55Z")

</div>

Also, `temp` in your example is a non-`const` global, which is really bad for performance. See the “Performance tips” in the docs.

---

<div class="post-metadata">

**Author:** ![Dharik\_Arsath](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dharik_arsath/32/27461_2.png) [@Dharik\_Arsath](https://discourse.julialang.org/u/Dharik_Arsath)\
**Post date:** [January 17, 2022, 9:54am UTC](https://discourse.julialang.org/t/strategies-to-reuse-memory/74743/4 "2022-01-17T09:54:38Z")

</div>

I have changed my code now. I guess it is good enough now. Thanks

---

<div class="post-metadata">

**Author:** ![pixel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel/32/26437_2.png) [@pixel](https://discourse.julialang.org/u/pixel)\
**Post date:** [January 17, 2022, 10:14am UTC](https://discourse.julialang.org/t/strategies-to-reuse-memory/74743/5 "2022-01-17T10:14:05Z")

</div>

for me its an interesting question - I have many algorithms that need to set something up (allocate memory, pre-calculate some parameters) and then an actual function that uses them to do the main part of the work. So what is the best way to pass the allocated/pre-calculated parameters to the actual function? Sure, passing them in as parameters, or passing a structure containing them works fine. But then I have a dozen or so algorithms so I end up passing a large number of parameters, which gets messy.

In other languages I would just use a global variable or a common block for the precalculated stuff which means a lot of complexity vanishes, But that’s not good for paralellisation and not the Julia way. Not a big issue, I just find passing a lot of parameters around to be a bit mechanical and distracting from the actual problem solving.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [January 17, 2022, 12:30pm UTC](https://discourse.julialang.org/t/strategies-to-reuse-memory/74743/6 "2022-01-17T12:30:56Z")

</div>

The only “Julian” improvement I may see is maybe use `NamedTuple`s, so each function has at most one parameter that has everything that needs to be precomputed/preallocated without having to create multiple distinct `structs` depending on exactly which objects each function takes.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [January 17, 2022, 12:49pm UTC](https://discourse.julialang.org/t/strategies-to-reuse-memory/74743/7 "2022-01-17T12:49:44Z")

</div>

An interesting pattern for this is to define the function with keyword parameters defining the auxiliary arrays (I will use a tuple here as suggested above):

```julia
julia> function f!(x;aux=(zeros(3),zeros(3)))
           a, b = aux
           a .= 2 .* x
           b .= x .^ 2
           x .= x .+ a .* b
           return x
       end
f (generic function with 1 method)

julia> x = rand(3);

julia> @btime f!(x) setup=(x=copy(xin)) evals=1
  135.000 ns (2 allocations: 160 bytes)
3-element Vector{Float64}:
 0.9603701960196425
 1.0196162985304351
 2.727934952974922

julia> auxin = (zeros(3),zeros(3));

julia> @btime f!(x,aux=aux) setup=(x=copy(xin);aux=deepcopy(auxin)) evals=1
  66.000 ns (0 allocations: 0 bytes)
3-element Vector{Float64}:
 0.9603701960196425
 1.0196162985304351
 2.727934952974922

```

The nice thing of this pattern is that you can develop the code without worrying about these allocations and then add them as a performance optimization where needed. Thus the complexity appears only where really needed.

It is possible, of course, to create constant global variables to act as a buffer, but that will break modularity and at the end increase code maintenance complexity.

> [@Dharik\_Arsath](#):
>
> returning the vector from one function and using them in another.

Concerning this point, I think one useful pattern is to define two functions, taking the advantage of multiple dispatch, one that receives preallocated vectors and return them, and the other that does not receive them and does not return them. Then you can again only take of care preallocations when needed, as an optimization:

```julia
julia> function f!(x,aux)
           a, b = aux
           x .= x .+ a .* b
           return x, aux
       end
f! (generic function with 2 methods)

julia> function f!(x)
           aux = (zeros(3),zeros(3))
           f!(x,aux)
           return x
       end
f! (generic function with 2 methods)

julia> x = rand(3); aux = (zeros(3),zeros(3))
([0.0, 0.0, 0.0], [0.0, 0.0, 0.0])

julia> x, aux = f!(x,aux)
([0.01274472197573262, 0.9823930168953983, 0.48667397158434544], ([0.0, 0.0, 0.0], [0.0, 0.0, 0.0]))

julia> x = f!(x)
3-element Vector{Float64}:
 0.01274472197573262
 0.9823930168953983
 0.48667397158434544

```

(ps: since I wrote everything mutating in place, returning the variables is optional here, in these examples the functions could well return `nothing` instead)

---

<div class="post-metadata">

**Author:** ![Dharik\_Arsath](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dharik_arsath/32/27461_2.png) [@Dharik\_Arsath](https://discourse.julialang.org/u/Dharik_Arsath)\
**Post date:** [February 8, 2022, 11:25am UTC](https://discourse.julialang.org/t/strategies-to-reuse-memory/74743/8 "2022-02-08T11:25:30Z")

</div>

My next question arises that why julia is following this convention of function\_name followed by “!”. why not pass parameter like.

function sum\_of\_arr(arr,out=nothing)  
if out === nothing  
create new array  
else # meaning some preallocated array is passed  
sum all and store in out array.

why julia community not following this way. The problem I see is that I have to write functions 2 times with ! and without !. which makes code longer.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [February 8, 2022, 12:02pm UTC](https://discourse.julialang.org/t/strategies-to-reuse-memory/74743/9 "2022-02-08T12:02:09Z")

</div>

Not all functions have something to be mutable or not, some functions have more than one mutable input, some functions have optional mutables, that could not be a single standard notation. The `!` is a warning that something is being mutated there.

You of course don’t have to write twice the same code, you just have to define a function that calls the other, as:

```julia
julia> function f!(x,y) # mutates
           y[1] = 2 * x[1] # true function body, complex code
           return y
       end
f! (generic function with 1 method)

julia> function f(x) # does not mutate
           y = zero(x) # allocate
           return f!(x,y) # just call the previous one
       end
f (generic function with 1 method)

```

Thus, the “real” function is the one that does the operation in place, and the one ~~with~~ without the `!` just preallocates stuff and calls that one.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [February 14, 2022, 11:41pm UTC](https://discourse.julialang.org/t/strategies-to-reuse-memory/74743/11 "2022-02-14T23:41:41Z")

</div>

`function sum_of_arr(arr,out=nothing)` actually defines _two_ methods `sum_of_arr(arr)` and `sum_of_arr(arr, out)`, so along with some compiler optimizations, it ends up doing the same thing as @lmiq’s `f` and `f!` methods. The “`f` uses `f!`” is really just a convention for quickly spotting a function call that may mutate any of its (mutable) inputs.

There are a couple problems with using an `out` convention. The smaller one is that a method can mutate many arguments, not just 1 `out`, but that can be fixed by naming arguments as `out1`/`out2`/etc or appending `!` to more distinctive names. The bigger one is that you can’t write positional arguments with their names in _function calls_: `function sum_of_arr(arr, out! = nothing)` can be called as `sum_of_arr(arr1, arr2)`, NOT `sum_of_arr(arr1, out! = arr2)`. And really, function calls are where you would want to see if a variable is being mutated.

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [February 15, 2022, 10:17am UTC](https://discourse.julialang.org/t/strategies-to-reuse-memory/74743/12 "2022-02-15T10:17:43Z")

</div>

There are real reasons where specifying an out parameter does not fit the model.

```julia
# just some random dataframe in case you want to try it
df = DataFrame(a=rand(1_000_000), b=rand(1_000_000), c=rand(1_000_000));
# this would *copy* the data, which might be Gb in size.
# and the writer of select must do a dance to see 
# if the in and out parameter are the same, 
# otherwise the data might be clobbered before it can be copied
select(df, Not(:c), out=df)
# this does *not* copy the data, it just drops the column
select!(df, Not(c:))
```
