-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
assignment out of an inner function leads to unnecessary boxing #47539
Comments
Isn't this an instance of the infamous "slow closure bug"? #15276 |
I'm kind of interested in what actual changes are needed to fix those examples. julia> function f(c, x)
# local var = nothing
# if c
# var = Any[]
# end
var = Any[]
function usevar(x)
if var !== nothing
push!(var, x)
return var
else
return nothing
end
end
return usevar(x)
end
f (generic function with 1 method)
julia> code_lowered(f, (Bool,Int))
1-element Vector{Core.CodeInfo}:
CodeInfo(
1 ─ var = Base.getindex(Main.Any)
│ %2 = Main.:(var"#usevar#3")
│ %3 = Core.typeof(var)
│ %4 = Core.apply_type(%2, %3)
│ usevar = %new(%4, var)
│ %6 = (usevar)(x)
└── return %6
) so it seems that some general fix is needed here, but rather we want to fix these example one by one? |
There is a section on this in the Julia docs performance tips. I'm not deeply into the codebase, but as far as I have heard other people talk about it, the problem is that at the lowering stage, there is no type info, so Julia does not know if var can change type in the if statement. Therefore, it emits a box. The fix is supposedly to rewrite the lowering step in Julia - but I'm sure the other compiler devs have thought quite a lot about this bug |
Well, as far as all the assignments happens out side of the scope of inner function, we can safely unbox it? Maybe I should better read through the code base though. This seems to be very much an implementation detail. |
Just pointing it out explicitly, there is no re-assignment to the variable inside the closure in this case. |
IIUC, rewriting parsing and lowering in Julia (i.e. JuliaSyntax) is just a (preferred) prerequisite for solving this problem. Doing the same lowering in Julia that FemtoLisp does will not of course solve the problem. |
FWIW, JuliaSyntax is only parsing, not lowering. There are no plans to port lowering to Julia. |
Duplicate of #15276 |
This may have been filed already, but let me file it here anyway as I couldn't find existing issue:
I'm willing to look at improving the lowering implementation if anyone kindly tell me which part of our codebase should be modified.
The text was updated successfully, but these errors were encountered: