How different are the ways of aliasing functions?

Say I have 2 functions:

function f1(i::Int)
    i
end

function f2(i::Int)
    2*i
end

I have these 2 ways of aliasing them:

f = f1
g(i::Int) = f1(i)

Calling either f or g give the right result, but somehow, calling f results in more memory allocation. Why is that?

f is a global scope variable that can change during the lifetime of your program. Julia has to compile code that does not become invalid if you later set f=0.

You can later on add/redefine methods for the function f1 and julia is capable of tracking which compiled methods need to be invalidated / recompiled. Julia is not willing to track the same for global non-constant variables.

TLDR: const f=f1

1 Like