Performance of global variables as default argument values

AFIU, yes. And I’d say that the implications of having to dynamically dispatch based on typeof(globalval) are twofold:

  • it involves a lookup at runtime, which costs something in itself, and
  • it prevents the compiler from knowing at compile-time what will be the type of the return value, which in turn prevents further optimizations down the road (in the context of the caller)
globalval = 10

function addxy(x, y=globalval)
    return x + y
end

function foo(x)
   z = addxy(10) # (1) which specialization of addxy(x,y) to choose based on `typeof(globalval)`?
   return z + 1  # (2) which method of + to choose based on `typeof(z)`?
end
2 Likes