Skip to content

Commit 8269a19

Browse files
martinholtersKristofferC
authored andcommitted
Simplify _isequal, _isless, __inc, and __dec
...by removing unnecessary type checks/assertions. This basically restores the function bodies to what they looked like prior to #40594 and only keeps the changed signatures (with the additional changes to circumvent #39088. (cherry picked from commit df81a0d)
1 parent b739ff4 commit 8269a19

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

base/multidimensional.jl

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,9 @@ module IteratorsMD
125125

126126
# comparison
127127
@inline isless(I1::CartesianIndex{N}, I2::CartesianIndex{N}) where {N} = _isless(0, I1.I, I2.I)
128-
@inline function _isless(ret, I1::Tuple{Int,Vararg{Int,N}}, I2::Tuple{Int,Vararg{Int,N}}) where {N}
128+
@inline function _isless(ret, I1::Tuple{Int,Vararg{Int}}, I2::Tuple{Int,Vararg{Int}})
129129
newret = ifelse(ret==0, icmp(last(I1), last(I2)), ret)
130-
t1, t2 = Base.front(I1), Base.front(I2)
131-
# avoid dynamic dispatch by telling the compiler relational invariants
132-
return isa(t1, Tuple{}) ? _isless(newret, (), ()) : _isless(newret, t1, t2::Tuple{Int,Vararg{Int}})
130+
return _isless(newret, Base.front(I1), Base.front(I2))
133131
end
134132
_isless(ret, ::Tuple{}, ::Tuple{}) = ifelse(ret==1, true, false)
135133
icmp(a, b) = ifelse(isless(a,b), 1, ifelse(a==b, 0, -1))
@@ -409,15 +407,13 @@ module IteratorsMD
409407
valid = __is_valid_range(I, rng) && state[1] != last(rng)
410408
return valid, (I, )
411409
end
412-
@inline function __inc(state::Tuple{Int,Int,Vararg{Int,N}}, indices::Tuple{OrdinalRangeInt,OrdinalRangeInt,Vararg{OrdinalRangeInt,N}}) where {N}
410+
@inline function __inc(state::Tuple{Int,Int,Vararg{Int}}, indices::Tuple{OrdinalRangeInt,OrdinalRangeInt,Vararg{OrdinalRangeInt}})
413411
rng = indices[1]
414412
I = state[1] + step(rng)
415413
if __is_valid_range(I, rng) && state[1] != last(rng)
416414
return true, (I, tail(state)...)
417415
end
418-
t1, t2 = tail(state), tail(indices)
419-
# avoid dynamic dispatch by telling the compiler relational invariants
420-
valid, I = isa(t1, Tuple{Int}) ? __inc(t1, t2::Tuple{OrdinalRangeInt}) : __inc(t1, t2::Tuple{OrdinalRangeInt,OrdinalRangeInt,Vararg{OrdinalRangeInt}})
416+
valid, I = __inc(tail(state), tail(indices))
421417
return valid, (first(rng), I...)
422418
end
423419

@@ -522,15 +518,13 @@ module IteratorsMD
522518
valid = __is_valid_range(I, rng) && state[1] != first(rng)
523519
return valid, (I,)
524520
end
525-
@inline function __dec(state::Tuple{Int,Int,Vararg{Int,N}}, indices::Tuple{OrdinalRangeInt,OrdinalRangeInt,Vararg{OrdinalRangeInt,N}}) where {N}
521+
@inline function __dec(state::Tuple{Int,Int,Vararg{Int}}, indices::Tuple{OrdinalRangeInt,OrdinalRangeInt,Vararg{OrdinalRangeInt}})
526522
rng = indices[1]
527523
I = state[1] - step(rng)
528524
if __is_valid_range(I, rng) && state[1] != first(rng)
529525
return true, (I, tail(state)...)
530526
end
531-
t1, t2 = tail(state), tail(indices)
532-
# avoid dynamic dispatch by telling the compiler relational invariants
533-
valid, I = isa(t1, Tuple{Int}) ? __dec(t1, t2::Tuple{OrdinalRangeInt}) : __dec(t1, t2::Tuple{OrdinalRangeInt,OrdinalRangeInt,Vararg{OrdinalRangeInt}})
527+
valid, I = __dec(tail(state), tail(indices))
534528
return valid, (last(rng), I...)
535529
end
536530

base/tuple.jl

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -366,11 +366,8 @@ filter(f, t::Tuple) = length(t) < 32 ? filter_rec(f, t) : Tuple(filter(f, collec
366366

367367
isequal(t1::Tuple, t2::Tuple) = length(t1) == length(t2) && _isequal(t1, t2)
368368
_isequal(::Tuple{}, ::Tuple{}) = true
369-
function _isequal(t1::Tuple{Any,Vararg{Any,N}}, t2::Tuple{Any,Vararg{Any,N}}) where {N}
370-
isequal(t1[1], t2[1]) || return false
371-
t1, t2 = tail(t1), tail(t2)
372-
# avoid dynamic dispatch by telling the compiler relational invariants
373-
return isa(t1, Tuple{}) ? true : _isequal(t1, t2::Tuple{Any,Vararg{Any}})
369+
function _isequal(t1::Tuple{Any,Vararg{Any}}, t2::Tuple{Any,Vararg{Any}})
370+
return isequal(t1[1], t2[1]) && _isequal(tail(t1), tail(t2))
374371
end
375372
function _isequal(t1::Any32, t2::Any32)
376373
for i = 1:length(t1)

test/tuple.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,3 +636,12 @@ end
636636

637637
# https://github.com/JuliaLang/julia/issues/40814
638638
@test Base.return_types(NTuple{3,Int}, (Vector{Int},)) == Any[NTuple{3,Int}]
639+
640+
# issue #42457
641+
f42457(a::NTuple{3,Int}, b::Tuple)::Bool = Base.isequal(a, Base.inferencebarrier(b)::Tuple)
642+
@test f42457((1, 1, 1), (1, 1, 1))
643+
@test !isempty(methods(Base._isequal, (NTuple{3, Int}, Tuple)))
644+
g42457(a, b) = Base.isequal(a, b) ? 1 : 2.0
645+
@test only(Base.return_types(g42457, (NTuple{3, Int}, Tuple))) === Union{Float64, Int}
646+
@test only(Base.return_types(g42457, (NTuple{3, Int}, NTuple))) === Union{Float64, Int}
647+
@test only(Base.return_types(g42457, (NTuple{3, Int}, NTuple{4}))) === Float64

0 commit comments

Comments
 (0)