# Avoid allocation for local array

**URL:** <https://discourse.julialang.org/t/avoid-allocation-for-local-array/124602>\
**Category:** General Usage\
**Created:** [January 9, 2025, 5:59pm UTC](https://discourse.julialang.org/t/avoid-allocation-for-local-array/124602 "2025-01-09T17:59:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ocp](https://avatars.discourse-cdn.com/v4/letter/o/c5a1d2/32.png) [@ocp](https://discourse.julialang.org/u/ocp)\
**Post date:** [January 9, 2025, 5:59pm UTC](https://discourse.julialang.org/t/avoid-allocation-for-local-array/124602/1 "2025-01-09T17:59:32Z")

</div>

I’m curious if there is a way to have a small, local array without allocations.

Here is a short, contrived example:

```julia
julia> function foo(x)
           arr = [1.0, 2.0, 3.0]
           if x < 1
               arr[1] *= x
           elseif x < 2
               arr[2] *= x
           elseif x < 3
               arr[3] *= x
           end
           return sum(arr)
       end
foo (generic function with 1 method)

julia> @time foo(1.9)
  0.000006 seconds (1 allocation: 48 bytes)
7.8

```

I’m assuming the allocations are due to `arr`.

`StaticArrays` doesn’t seems to be a solution since they are immutable and `MVector`s are heap-allocated.

Any suggestions?

---

<div class="post-metadata">

**Author:** ![Domenico\_Lahaye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/domenico_lahaye/32/203728_2.png) [@Domenico\_Lahaye](https://discourse.julialang.org/u/Domenico_Lahaye)\
**Post date:** [January 9, 2025, 6:32pm UTC](https://discourse.julialang.org/t/avoid-allocation-for-local-array/124602/2 "2025-01-09T18:32:32Z")

</div>

Not entire sure what you are looking for.

Does below help you out?

```julia
using StaticArrays 
using BenchmarkTools

```

```julia
arr = [1.0, 2.0, 3.0]

function foo!(arr,x) 
           if x < 1
               arr[1] *= x
           elseif x < 2
               arr[2] *= x
           elseif x < 3
               arr[3] *= x
           end
           return sum(arr)
end

```

```julia
foo!(arr,1.)

```

> 6.0

```julia
@btime foo!($arr,$1.)

```

> 3.958 ns (0 allocations: 0 bytes)

---

<div class="post-metadata">

**Author:** ![JonasWickman](https://avatars.discourse-cdn.com/v4/letter/j/9de0a6/32.png) [@JonasWickman](https://discourse.julialang.org/u/JonasWickman)\
**Post date:** [January 9, 2025, 7:05pm UTC](https://discourse.julialang.org/t/avoid-allocation-for-local-array/124602/3 "2025-01-09T19:05:41Z")

</div>

Perhaps you could give Bumper.jl a go? I’ve not really used it myself, but it seems like it might do the trick:

```julia
using Bumper
using BenchmarkTools
function foo(x)
    @no_escape begin
        arr = @alloc(typeof(x), 3)
        arr .= 1:3
        if x < 1
            arr[1] *= x
        elseif x < 2
            arr[2] *= x
        elseif x < 3
            arr[3] *= x
        end
        sum(arr)
    end
end
## --
foo(2.0) # 9.0
@btime foo(2.0) # 16.994 ns (0 allocations: 0 bytes)

```

Not super fast for this toy example though.

---

<div class="post-metadata">

**Author:** ![ocp](https://avatars.discourse-cdn.com/v4/letter/o/c5a1d2/32.png) [@ocp](https://discourse.julialang.org/u/ocp)\
**Post date:** [January 9, 2025, 8:10pm UTC](https://discourse.julialang.org/t/avoid-allocation-for-local-array/124602/4 "2025-01-09T20:10:38Z")

</div>

Thanks for the suggestion, but I’m trying to avoid having the user pass in a scratch buffer for computation.

---

<div class="post-metadata">

**Author:** ![ocp](https://avatars.discourse-cdn.com/v4/letter/o/c5a1d2/32.png) [@ocp](https://discourse.julialang.org/u/ocp)\
**Post date:** [January 9, 2025, 8:12pm UTC](https://discourse.julialang.org/t/avoid-allocation-for-local-array/124602/5 "2025-01-09T20:12:06Z")

</div>

Thanks, this appears to meet my need. I’ll check it out!

---

<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 9, 2025, 8:12pm UTC](https://discourse.julialang.org/t/avoid-allocation-for-local-array/124602/6 "2025-01-09T20:12:41Z")

</div>

> [@ocp](#):
>
> `MVector`s are heap-allocated

Did you try it? If it does not escape it may be stack allocated by the compiler. (use @btime or you may get an apparent allocation from the output to the REPL).

---

<div class="post-metadata">

**Author:** ![ocp](https://avatars.discourse-cdn.com/v4/letter/o/c5a1d2/32.png) [@ocp](https://discourse.julialang.org/u/ocp)\
**Post date:** [January 9, 2025, 8:17pm UTC](https://discourse.julialang.org/t/avoid-allocation-for-local-array/124602/7 "2025-01-09T20:17:45Z")

</div>

Thanks for the suggestion! I hadn’t tried it and just assumed it would be heap allocated. Looks like you’re right and the compiler turned it into a stack allocation.

```julia
julia> function foo2(x)
           arr = MVector(1.0, 2.0, 3.0)
           if x < 1
               arr[1] *= x
           elseif x < 2
               arr[2] *= x
           elseif x < 3
               arr[3] *= x
           end
           return sum(arr)
       end
foo2 (generic function with 1 method)

julia> @btime foo2(1.9)
  3.333 ns (0 allocations: 0 bytes)
7.8

```
