# How to support passing temporary buffers around

**URL:** <https://discourse.julialang.org/t/how-to-support-passing-temporary-buffers-around/70880>\
**Category:** Internals & Design\
**Tags:** memory-allocation, functions\
**Created:** [November 3, 2021, 2:45pm UTC](https://discourse.julialang.org/t/how-to-support-passing-temporary-buffers-around/70880 "2021-11-03T14:45:23Z")\
**Posts on this page:** 1\
**Showing post:** 13

<div class="post-metadata">

**Author:** ![Lilith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lilith/32/27492_2.png) [@Lilith](https://discourse.julialang.org/u/Lilith)\
**Post date:** [November 5, 2021, 4:48pm UTC](https://discourse.julialang.org/t/how-to-support-passing-temporary-buffers-around/70880/13 "2021-11-05T16:48:20Z")

</div>

> [@roflmaostc](#):
>
> `f_safe` might allocate a lot of memory

By this I’m assuming you mean “own” i.e. as we keep `f_safe` around the memory will be in use.

If there are three two different functions `f`, and `g`, both of which mutate an array but require temporary storage `similar` to their input, and I want to compute `f(g(x))` for two different x of the same type and size, naively, I’ll allocate 4 times, with `gen_f_safe` or `gen_f` I’ll still allocate twice, while manual temporary storage passing can drop allocations to 1.

Perhaps some sort compiler pass or macro `@reuse_temporary` that can convert

```julia
temp1 = similar(x)
f(x, temp=temp1)
#temp 1 is never used again
temp2 = similar(x)
g(x, temp=temp2)

```

to

```julia
temp1 = similar(x)
f(x, temp=temp1)
temp2 = temp1 # or, as necessary, reinterpret(temp1) and maybe even resize!ing?
f(x, temp=temp2)

```

Which would let me use Base’s existing sorting methods without this problem:

```julia
@time sort!(rand(1000,1000), dims=1; alg=QuickSort); #2k allocs
@time sort!(rand(1000,1000), dims=1; alg=MergeSort); #5k allocs b.c. MergeSort takes temporary array

```

---

_[View the full topic](https://discourse.julialang.org/t/how-to-support-passing-temporary-buffers-around/70880)._
