# Preallocating and returning anonymous function

**URL:** <https://discourse.julialang.org/t/preallocating-and-returning-anonymous-function/115241>\
**Category:** Performance\
**Created:** [June 5, 2024, 4:02pm UTC](https://discourse.julialang.org/t/preallocating-and-returning-anonymous-function/115241 "2024-06-05T16:02:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kockahonza](https://avatars.discourse-cdn.com/v4/letter/k/ecc23a/32.png) [@kockahonza](https://discourse.julialang.org/u/kockahonza)\
**Post date:** [June 5, 2024, 4:02pm UTC](https://discourse.julialang.org/t/preallocating-and-returning-anonymous-function/115241/1 "2024-06-05T16:02:58Z")

</div>

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.

```julia
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?

```

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [June 5, 2024, 4:11pm UTC](https://discourse.julialang.org/t/preallocating-and-returning-anonymous-function/115241/2 "2024-06-05T16:11:08Z")

</div>

This’ll work.

> [@kockahonza](#):
>
> something like an object which will carry its own memory and be able to be called while reusing that internal memory

These are [function-like objects](https://docs.julialang.org/en/v1/manual/methods/#Function-like-objects). The mechanics of functions, including anonymous ones, are basically function-like objects with singleton types that have no fields and subtype `Function`, which does dispatch to some methods (affecting how it prints) and involves a [non-specialization heuristic](https://docs.julialang.org/en/v1/manual/performance-tips/#Be-aware-of-when-Julia-avoids-specializing). This is why many higher order functions like `map` don’t dispatch on `f::Function`, to accommodate callable objects that don’t subtype `Function`.

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [June 5, 2024, 4:14pm UTC](https://discourse.julialang.org/t/preallocating-and-returning-anonymous-function/115241/3 "2024-06-05T16:14:53Z")

</div>

Maybe a callable `struct` would be simpler here?

```julia
struct ActOnA{B}
  buffer::B
end

# define what happens when we use an ActOnA object like a function
function (b::ActOnA)(A)
  return acting_on_A_using_B_as_buffer(A, b.buffer)
end

act_on_A = ActOnA(similar(A)) # create buffer object
act_on_A(A) # call buffer object

```

Looks like the above poster beat me to this suggestion, but I’m still posting this for the snippet.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [June 5, 2024, 4:21pm UTC](https://discourse.julialang.org/t/preallocating-and-returning-anonymous-function/115241/4 "2024-06-05T16:21:43Z")

</div>

Also worth pointing out the smart move to decouple the buffer-allocating step (a simple `similar` for now) from instantiating `ActonA`. Now you don’t have to allocate a buffer for every instantiation, and an instantiation with a reused buffer `ActonA(act_on_A.buffer)` would return the _same_ instance in fact (this would not be the case if there were more fields with different values).

---

<div class="post-metadata">

**Author:** ![kockahonza](https://avatars.discourse-cdn.com/v4/letter/k/ecc23a/32.png) [@kockahonza](https://discourse.julialang.org/u/kockahonza)\
**Post date:** [June 5, 2024, 4:27pm UTC](https://discourse.julialang.org/t/preallocating-and-returning-anonymous-function/115241/5 "2024-06-05T16:27:51Z")

</div>

Wow, thank you both for such quick and good responses! I am surprised and impressed by Julia that this works as simple as that. And thank you for the struct suggestion, I am just learning the language and did not realize I could do such a thing. As mentioned, this has some advantages and I also feel like it’s a bit less repetitive than having to call the function itself repeating all the arguments.
