# Understanding allocations when collapsing function arugments

**URL:** <https://discourse.julialang.org/t/understanding-allocations-when-collapsing-function-arugments/59668>\
**Category:** General Usage\
**Created:** [April 20, 2021, 2:14pm UTC](https://discourse.julialang.org/t/understanding-allocations-when-collapsing-function-arugments/59668 "2021-04-20T14:14:20Z")\
**Posts on this page:** 1\
**Showing post:** 3

<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:** [April 20, 2021, 2:27pm UTC](https://discourse.julialang.org/t/understanding-allocations-when-collapsing-function-arugments/59668/3 "2021-04-20T14:27:54Z")

</div>

What you are doing there is using a lot of global variables inside `fun1`. You are doing something like:

```julia
julia> g(x,y) = x + y
g (generic function with 1 method)

julia> function f1(x)
         g(x,y)
       end
f1 (generic function with 1 method)

```

and `y` is a global variable inside `f1 `. That would be ok if `y` was a `const` (constant global), but does not seem to be the case. That is very bad for performance because the type of `y` is not stable.

You can use a closure, for example:

```julia
julia> function f2(x, inner_g)
            inner_g(x)
       end
f2 (generic function with 1 method)

julia> f2(1, x -> g(x,2))
3

```

Or look at this thread: [Define data-dependent function, various ways](https://discourse.julialang.org/t/define-data-dependent-function-various-ways/48988) for more alternatives.

---

_[View the full topic](https://discourse.julialang.org/t/understanding-allocations-when-collapsing-function-arugments/59668)._
