How to support passing temporary buffers around

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

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

to

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:

@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
1 Like