Hi, so I’m working on a project and I want to avoid many allocations called in a loop as is often suggested. That said I also want to avoid having to pass all the preallocated arrays/space/whatever it is at every function call. This is as I want this part of the code to be generic, in short I am evolving a field by calculating its derivative and then making a small step in its direction. My field is Q and I want my main code to be able to support any function dQdt!(dQdt, Q) which will set dQdt according to the passed Q field. However, many of these functions may be complex and require some additional memory.
Potentially another way to describe the problem is that I want to achieve something like an object which will carry its own memory and be able to be called while reusing that internal memory. Similar to how say the EigenSolver class works in the C++ library Eigen.
What I’ve come up with actually seems to work but I suspect there will be some drawbacks to it and so I want to ask about those and for any alternative method of achieving this goal. In short my method relies on defining another function which allocates the required memory and returns an anonymous function which calls the function that actually does the work. Here acting_on_A_using_B_as_buffer
represents some function with complicated logic that needs an “internal” buffer B but is supplied it to, in order to be allocation free.
function acting_on_A_using_B_as_buffer(A, B)
B .= transpose(A)
A .= B .* A
end
function return_object_like_thing(A)
B = similar(A)
return x -> acting_on_A_using_B_as_buffer(x, B)
end
N = 100
A = rand(N, N);
B = rand(N, N);
@time acting_on_A_using_B_as_buffer(A, B); # has 0 allocs but need to supply B
@time f = return_object_like_thing(A); # seems to allocate correctly
@time f(A); # has 0 allocs, looks good?