Hello! In order to avoid performance problems, I would like to know whether boxes can be avoided in two kinds of closures. Without closures, all problems mentioned below disappear. I’m using Julia 1.6.0.
Consider the following example:
function h1() f(x, y) = x+y f(x; p = 0) = x+p return f end f = h1()
@code_warntype f(1) contains the lines
Body::Any 1 ─ %1 = Core.getfield(#self#, Symbol("#f#1"))::Core.Box
You also see a box in
@code_warntype f(1; p = 1), but not in
@code_warntype f(1,1). The problem disappears if the keyword argument is removed from the second method, but also if instead the first method is removed, so that there is only the second method left (with the keyword argument). Is there a way to have multiple methods and keyword arguments at the same time without creating boxes?
The GitHub issue #19668 might be related, but it has been fixed already. (I tried to insert a link, but Discourse wouldn’t let me do it.)
In the example
function h2() f(x, y) = x+y f(x) = f(x, x) return f end f = h2()
the output of
@code_warntype f(1) contains
Body::Any 1 ─ %1 = Core.getfield(#self#, :f)::Core.Box
In this specific example I can of course avoid that
f calls itself by saying
function h3() g(x, y) = x+y f(x, y) = g(x, y) f(x) = g(x, x) return f end f = h3()
and this fixes the problem. But what do I do for a truly recursive function, say
function h4() f(n) = n == 0 ? 1 : n*f(n-1) return f end f = h4()
Is there a way to avoid the creation of boxes in this case?
Thanks a lot!