# Memory allocation on multiple return values

**URL:** <https://discourse.julialang.org/t/memory-allocation-on-multiple-return-values/32805>\
**Category:** Performance\
**Created:** [December 30, 2019, 7:22pm UTC](https://discourse.julialang.org/t/memory-allocation-on-multiple-return-values/32805 "2019-12-30T19:22:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lucas711642](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucas711642/32/12051_2.png) [@lucas711642](https://discourse.julialang.org/u/lucas711642)\
**Post date:** [December 30, 2019, 7:22pm UTC](https://discourse.julialang.org/t/memory-allocation-on-multiple-return-values/32805/1 "2019-12-30T19:22:51Z")

</div>

Is there any way to avoid allocations on functions that return multiple values, when some of these values are mutable?

For example:

```julia
julia> x = [0];
julia> fun1(x) = @time((x, 1));
julia> fun2(x) = (fun1(x); nothing);
julia> fun1(x)
  0.000000 seconds (1 allocation: 32 bytes)
([0], 1)
julia> fun2(x)
  0.000000 seconds

```

`fun2` does not allocate memory in this case, but it does when `fun1` is not inlined:

```julia
julia> @noinline fun1(x) = @time((x, 1));
julia> fun1(x)
  0.000000 seconds (1 allocation: 32 bytes)
([0], 1)
julia> fun2(x)
  0.000000 seconds (1 allocation: 32 bytes)

```

In my work, I need to return these preallocated objects for consistency with other methods defined for the same function.

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [December 30, 2019, 7:33pm UTC](https://discourse.julialang.org/t/memory-allocation-on-multiple-return-values/32805/2 "2019-12-30T19:33:24Z")

</div>

Escaping structs with heap-allocated elements will also be heap allocated.  
In the case of multiple return values, that means the tuple you’re constructing (the one holding all the returned values) must be heap allocated.

Given that you’re already preallocating, two options are:

1. Don’t return the preallocated object. The caller must already have access to it.
2. Preallocate objects with space for all the returned values, eg

```julia
mutable struct ArrayAndInt{T}
    x::Array{T}
    i::Int
end

```

then pass a preallocated instance, mutating `i`, and returning.

---

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [December 30, 2019, 9:10pm UTC](https://discourse.julialang.org/t/memory-allocation-on-multiple-return-values/32805/3 "2019-12-30T21:10:58Z")

</div>

I’ve run into the same issue a few times. Although there are of multiple workarounds they nine are elegant. It would be very nice to have this fixed at the language level.

Most of the time the allocation doesn’t matter and one can just ignore it but occasionally it does make a difference. (And also for the typical “obsessive-compulsive” academic it is good for peace of mind to have zero allocations.)

---

<div class="post-metadata">

**Author:** ![lucas711642](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucas711642/32/12051_2.png) [@lucas711642](https://discourse.julialang.org/u/lucas711642)\
**Post date:** [December 31, 2019, 4:01am UTC](https://discourse.julialang.org/t/memory-allocation-on-multiple-return-values/32805/4 "2019-12-31T04:01:56Z")

</div>

Indeed, it would be great to have this optimization without having to sacrifice productivity. Thank you anyway.
