Understanding allocations when collapsing function arugments

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

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> 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 for more alternatives.

1 Like