-
-
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
performance of captured variables in closures #15276
Comments
This is due to that line capturing |
Just want to add that this has quite severe consequences for |
This also has severe consequences for but now everything is just Now sure how to work around this. This feels like a quite significant regression. Now suddenly any variable captured by a closure poisons the variable for the whole outer function. Even if it is only read from. Edit: The example below has been fixed. Ex: function newton_{T}(initial_x::Vector{T})
nn = length(initial_x)
xold = fill(convert(T, NaN), nn)
fvec = Array(T, nn)
f_calls = 1
function fo(xlin::Vector)
if xlin != xold
print(f_calls)
end
return(dot(fvec, fvec) / 2)
end
return
end
@code_warntype newton_(rand(5)) Here, |
Until the number of elements passed via varargs is inferred, we need to use a wrapper function. Also remove access to lev[i] from anonymous function, which is currently buggy on 0.5 (JuliaLang/julia#15276).
Until the number of elements passed via varargs is inferred, we need to use a wrapper function. Also remove access to lev[i] from anonymous function, which is currently buggy on 0.5 (JuliaLang/julia#15276).
Should this get a 0.5 label? For my projects this seems to cause a significant increase in runtime (~30%) for projects that worked well on 0.4. |
Everything tagged "regression" will get attention before 0.5-final. |
Thanks! |
… block helps the easier cases of #15276
Ok, I have a quick fix for many of the examples posted here. Not including @timholy 's original example in this issue, of course :/ |
I'll try my cases once this has landed in a windows nightly binary. |
Before they were pessimized a bit in a way that's no longer necessary after the function redesign. helps #15276
Before they were pessimized a bit in a way that's no longer necessary after the function redesign. helps #15276
Under what circumstance is this true? I don't see it: julia> @btime (()->begin
x::Int = 1
f() = (x = x+1; x)
for i=1:1000; f() end
x
end)()
4.200 μs (490 allocations: 7.66 KiB)
1001
julia> @btime (()->begin
x = Ref(1)
f() = (x[] = x[] + 1; x[])
for i=1:1000; f() end
x[]
end)()
1.800 ns (0 allocations: 0 bytes)
1001 (Julia 1.9.0-beta3) (inspired by this discourse discussion, which showed that global variables share similar performance characteristics w.r.t. typed global vs. const |
@c42f, if you happen to be rewriting lowering it would be nice to fix this issue. |
Ran into this again here |
Performance issues related to code containing closures are frequently discussed (e.g., #15276, #56561). Several approaches can be considered to address this problem, one of which involves re-inferring code containing closures (#56687). To implement this idea, it is necessary to determine whether a given piece of code includes a closure. However, there is currently no explicit mechanism for making this determination (although there are some code that checks whether the function name contains `"#"` for this purpose, but this is an ad hoc solution). To address this, this commit lays the foundation for future optimizations targeting closures by defining closure functions as a subtype of the new type `Core.Closure <: Function`. This change allows the optimizer to apply targeted optimizations to code containing calls to functions that are subtype of `Core.Closure`.
Performance issues related to code containing closures are frequently discussed (e.g., #15276, #56561). Several approaches can be considered to address this problem, one of which involves re-inferring code containing closures (#56687). To implement this idea, it is necessary to determine whether a given piece of code includes a closure. However, there is currently no explicit mechanism for making this determination (although there are some code that checks whether the function name contains `"#"` for this purpose, but this is an ad hoc solution). To address this, this commit lays the foundation for future optimizations targeting closures by defining closure functions as a subtype of the new type `Core.Closure <: Function`. This change allows the optimizer to apply targeted optimizations to code containing calls to functions that are subtype of `Core.Closure`.
Test:
Now comment out the
indexesK = ...
line (the output of which is not used at all). Suddenlyprepad
is inferred asArray{Int, 1}
.The text was updated successfully, but these errors were encountered: