# Allocations when passing closure as parameter

**URL:** <https://discourse.julialang.org/t/allocations-when-passing-closure-as-parameter/63082>\
**Category:** Performance\
**Tags:** memory-allocation, closure\
**Created:** [June 17, 2021, 12:32pm UTC](https://discourse.julialang.org/t/allocations-when-passing-closure-as-parameter/63082 "2021-06-17T12:32:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [June 17, 2021, 12:32pm UTC](https://discourse.julialang.org/t/allocations-when-passing-closure-as-parameter/63082/1 "2021-06-17T12:32:16Z")

</div>

I struggling to obtain a minimal working example. But I have one situation which looks like:

```julia
function outer!(f::Function, output,x::AbstractVector) 
  for i in 1:10
    output = inner!(f,output,x)
  end
  return output
end

```

`f` is given by the user, most likely as a closure. Something like:

```julia
function run()
  a = 5
  f(x,a) = a*x
  output = outer!( x -> f(x,a), output, x)
end

```

I am seeing some allocations associated with this closure. Curiously, if I modify the `outer!` function to:

```julia
function outer!(f::Function, output,x::AbstractVector) 
  g = (x) -> f(x) # <<< set g here as pass g in the loop to inner!
  for i in 1:10
    output = inner!(g,output,x) 
  end
  return output
end

```

or to

```julia
function outer!(f::Function, output,x::AbstractVector) 
  for i in 1:10
    output = inner!((x) -> f(x),output,x) # pass a closure instead of `f` directly
  end
  return output
end

```

the allocations go away. It seems that telling the compiler how many parameters `f` is what is solving the problem.

Maybe this is related to this [known issue](https://discourse.julialang.org/t/avoiding-allocations-with-user-provided-closures/31279). But I am not completely sure.

As a workaround, adding `g = (x) -> f(x)` in my case is not a problem, but it seems like a hack to something I am not quite understanding.

Any insight is appreciated.

By the way: I cannot find type instabilities anywhere. Does not seem to be related to that. Also there is not such a big performance penalty (as reported in the issue). Just some some thousand allocations more.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [June 17, 2021, 1:08pm UTC](https://discourse.julialang.org/t/allocations-when-passing-closure-as-parameter/63082/2 "2021-06-17T13:08:14Z")

</div>

[https://docs.julialang.org/en/v1/manual/performance-tips/#Be-aware-of-when-Julia-avoids-specializing](https://docs.julialang.org/en/v1/manual/performance-tips/#Be-aware-of-when-Julia-avoids-specializing)

> As a heuristic, Julia avoids automatically specializing on argument type parameters in three specific cases: `Type` , `Function` , and `Vararg` . Julia will always specialize when the argument is used within the method, but not if the argument is just passed through to another function.

So your non-allocating example uses `f`, and thus the method is (presumably) specialized for `f`, while the allocating examples just pass it through, so specialization is avoided.

Be aware that adding the `where` annotation for `f` will lead to compilation for each new `f`.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [June 17, 2021, 1:13pm UTC](https://discourse.julialang.org/t/allocations-when-passing-closure-as-parameter/63082/3 "2021-06-17T13:13:04Z")

</div>

Great, that was exactly what was going on. (I probably want it to specialize for every `f` in this case, although I am not sure if that will have any impact on performance - getting rid of those unexpected allocations is nice anyway).
