-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Specialize tuple setindex to avoid ntuple-related performance regression. #46050
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
Conversation
|
Maybe add a test so that this won't be regressed in the future? |
Any suggestions? string-matching |
|
What about something like your example, if there are dynamic dispatches there will be allocation so |
1e6bd35 to
75b5ddb
Compare
julia> Base.return_types(Base.setindex, Tuple{NTuple{16,Int},Int,Int})
1-element Vector{Any}:
Tuple{Vararg{Int64}} # NTuple{16, Int64} after this PRThe improvement comes from the fact that function setindex(x::Tuple, v, i::Integer)
@boundscheck 1 <= i <= length(x) || throw(BoundsError(x, i))
@inline
ntuple(j -> ifelse(j == i, v, x[j]), Val(length(x)))
end |
75b5ddb to
141167e
Compare
Thanks, I've pushed this version. |
141167e to
8fd8823
Compare
|
Sorry for my recklessness, we still need |
Why not add |
Ah, I thought it was a performance consideration, and didn't realize it actually triggers during CI. I'm not sure it's worth reusing |
4fa5ec6 to
ceed845
Compare
|
I've rebased-out your change and revert, @N5N3, because it included a bit of unnecessary churn. I've left the explicit |
|
I think we'd better avoid unnecessary splat. As our compiler might refuse to inline julia> a = ntuple(identity, 33);
julia> @btime setindex($a, 1, 1);
352.995 ns (2 allocations: 544 bytes) # no splat : 3.200 ns (0 allocations: 0 bytes) |
|
I'll take this as a fix for the regression, further improvements can of course be made as follow ups. |
Fixes #46049. Not sure what a good test would be, as this does infer correctly.
cc @OlivierHnt