# About memory allocation of auxiliary array in successive calls of a function

**URL:** https://discourse.julialang.org/t/about-memory-allocation-of-auxiliary-array-in-successive-calls-of-a-function/41535
**Category:** Performance
**Created:** [June 16, 2020, 4:52pm UTC](https://discourse.julialang.org/t/about-memory-allocation-of-auxiliary-array-in-successive-calls-of-a-function/41535 "2020-06-16T16:52:36Z")
**Posts on this page:** 7
**Page:** 1

<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: [June 16, 2020, 4:52pm UTC](https://discourse.julialang.org/t/about-memory-allocation-of-auxiliary-array-in-successive-calls-of-a-function/41535/1 "2020-06-16T16:52:36Z")

</div>

If I call a function that internally requires the allocation of an array for some computation, and afterwards I call this same function with the exact same type of input variables, is the auxiliary array which was allocated the first time allocated again?

Example:

```julia
x = rand(3)
function h!(x)
   A = rand(3,3)
   x = A*x
end
# first call (function is compiled and certainly A is allocated)
h!(x)
# second call (function is not compiled, but is A reallocated?)
h!(x)

```

My question is because I am not sure if, to improve performance, it is a good idea to pass `A` as well, with:

```julia
x = rand(3)
A = Matrix{Float64}(undef,3,3)
function h!(x,A)
   @. A = rand()
   x = A*x
end

```

Or if the compiler/running-time does that already, allowing a cleaner code. From a few tests I performed here it seems that the second option is needed for maximum performance, but I would like to hear from someone that really knows what is under the hood.

(ps: I am coming from Fortran, in which everything is allocated on execution, such that the declaration of auxiliary arrays inside a function certainly does not lead to multiple allocations, and the explicit passing of the the auxiliary arrays which are only used internally and have fixed sizes is not needed).

Thank you.

---

<div class="post-metadata">

### Author: ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)
#### Post date: [June 16, 2020, 6:53pm UTC](https://discourse.julialang.org/t/about-memory-allocation-of-auxiliary-array-in-successive-calls-of-a-function/41535/2 "2020-06-16T18:53:29Z")

</div>

> [@lmiq](#):
>
> ```julia
> x = rand(3)
> function h!(x)
> A = rand(3,3)
> x = A*x
> end
> # first call (function is compiled and certainly A is allocated)
> h!(x)
> # second call (function is not compiled, but is A reallocated?)
> h!(x)
> 
> ```

A is allocated every time you call `h!(x)`

You can do this

```julia
function h!(x,A)
          Random.rand!(A)
          mul!(x,A,x);
       end

```

---

<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: [June 16, 2020, 6:57pm UTC](https://discourse.julialang.org/t/about-memory-allocation-of-auxiliary-array-in-successive-calls-of-a-function/41535/3 "2020-06-16T18:57:55Z")

</div>

Great. But ‘x=A\*x’ doesn’t allocate anything, wright? Using mult! there makes any difference?

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [June 16, 2020, 7:01pm UTC](https://discourse.julialang.org/t/about-memory-allocation-of-auxiliary-array-in-successive-calls-of-a-function/41535/4 "2020-06-16T19:01:03Z")

</div>

`x = A*x` allocates a vector for `x`.

---

<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: [June 16, 2020, 7:04pm UTC](https://discourse.julialang.org/t/about-memory-allocation-of-auxiliary-array-in-successive-calls-of-a-function/41535/5 "2020-06-16T19:04:48Z")

</div>

Uhm… thanks. That independence of names from actual values still confuses me from time to time. That is the same as ‘y=A\*x’, of course.

Edit: added this comment just to remember how things work, and perhaps helps other newbies:

```julia
julia> function f(x)
           x = 2*x
           return x
       end
f (generic function with 1 method)

julia> x = [1, 2]
2-element Array{Int64,1}:
 1
 2

julia> y = f(x)
2-element Array{Int64,1}:
 2
 4

julia> x
2-element Array{Int64,1}:
 1
 2

```

Note that the `x` which enters `f(x)` is not modified.

---

<div class="post-metadata">

### Author: ![robsmith11](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robsmith11/32/29641_2.png) [@robsmith11](https://discourse.julialang.org/u/robsmith11)
#### Post date: [June 17, 2020, 4:45am UTC](https://discourse.julialang.org/t/about-memory-allocation-of-auxiliary-array-in-successive-calls-of-a-function/41535/6 "2020-06-17T04:45:40Z")

</div>

I don’t think `mul!` can be used as suggested without using a 2nd vector. If you look at the docs, it says that the 1st and 3rd arguments can not be aliases of each other, which appears to be correct – when I try it, I get a zero vector as a result.

Julia does have a solution for “static” arrays so that you can call a function multiple times without having to reallocate or explicitly pass an array. I’d prefer Julia had a cleaner static syntax, but this works:

```julia
using LinearAlgebra
using Random
let y = zeros(3), A = zeros(3,3)
  global function h!(x)
    rand!(A)
    mul!(y,A,x)
    x .= y
  end
end

```

```julia
julia> x = rand(3)
3-element Array{Float64,1}:
 0.528643672437896
 0.6445978634354903
 0.13390924294137552

julia> @time h!(x) # notice zero allocations reported
  0.000005 seconds
3-element Array{Float64,1}:
 0.9482372634076288
 0.9135226035098386
 0.6381315118219281

julia> x
3-element Array{Float64,1}:
 0.9482372634076288
 0.9135226035098386
 0.6381315118219281

```

---

<div class="post-metadata">

### Author: ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)
#### Post date: [June 17, 2020, 8:31am UTC](https://discourse.julialang.org/t/about-memory-allocation-of-auxiliary-array-in-successive-calls-of-a-function/41535/7 "2020-06-17T08:31:02Z")

</div>

If `A` is of fixed and small dimension, you can use `StaticArrays` to move everything from the heap onto the stack:

```julia
julia> function h!(x)
          A = @SMatrix rand(3,3)
          x .= A*x
       end
       x = @MVector rand(3)
       @time h!(x) # This compiles and hence allocates
       @time h!(x) # Look Ma, no allocations!
  0.010066 seconds (23.40 k allocations: 977.899 KiB)
  0.000004 seconds
3-element MArray{Tuple{3},Float64,1,3} with indices SOneTo(3):
 0.3388153014844782
 0.9433372130533029
 0.8048580079870412

```
