Skip to content

Commit

Permalink
Specialize tuple setindex to avoid ntuple-related performance regress…
Browse files Browse the repository at this point in the history
…ion. (#46050)
  • Loading branch information
maleadt committed Jul 20, 2022
1 parent 017228a commit d7c56ba
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
4 changes: 2 additions & 2 deletions base/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ function setindex(x::Tuple, v, i::Integer)
_setindex(v, i, x...)
end

function _setindex(v, i::Integer, args...)
function _setindex(v, i::Integer, args::Vararg{Any,N}) where {N}
@inline
return ntuple(j -> ifelse(j == i, v, args[j]), length(args))
return ntuple(j -> ifelse(j == i, v, args[j]), Val{N}())
end


Expand Down
3 changes: 3 additions & 0 deletions test/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -754,3 +754,6 @@ g42457(a, b) = Base.isequal(a, b) ? 1 : 2.0
@test only(Base.return_types(g42457, (NTuple{3, Int}, Tuple))) === Union{Float64, Int}
@test only(Base.return_types(g42457, (NTuple{3, Int}, NTuple))) === Union{Float64, Int}
@test only(Base.return_types(g42457, (NTuple{3, Int}, NTuple{4}))) === Float64

# issue #46049: setindex(::Tuple) regression
@inferred Base.setindex((1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16), 42, 1)

0 comments on commit d7c56ba

Please sign in to comment.