Skip to content

Commit

Permalink
Simplify _isequal, _isless, __inc, and __dec
Browse files Browse the repository at this point in the history
...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..
  • Loading branch information
martinholters committed Oct 19, 2021
1 parent e1afeda commit 4890be5
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 13 deletions.
12 changes: 3 additions & 9 deletions base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,7 @@ module IteratorsMD
@inline isless(I1::CartesianIndex{N}, I2::CartesianIndex{N}) where {N} = _isless(0, I1.I, I2.I)
@inline function _isless(ret, I1::Tuple{Int,Vararg{Int}}, I2::Tuple{Int,Vararg{Int}})
newret = ifelse(ret==0, icmp(last(I1), last(I2)), ret)
t1, t2 = Base.front(I1), Base.front(I2)
# avoid dynamic dispatch by telling the compiler relational invariants
return isa(t1, Tuple{}) ? _isless(newret, (), t2::Tuple{}) : _isless(newret, t1, t2::Tuple{Int,Vararg{Int}})
return _isless(newret, Base.front(I1), Base.front(I2))
end
_isless(ret, ::Tuple{}, ::Tuple{}) = ifelse(ret==1, true, false)
icmp(a, b) = ifelse(isless(a,b), 1, ifelse(a==b, 0, -1))
Expand Down Expand Up @@ -439,9 +437,7 @@ module IteratorsMD
if __is_valid_range(I, rng) && state[1] != last(rng)
return true, (I, tail(state)...)
end
t1, t2 = tail(state), tail(indices)
# avoid dynamic dispatch by telling the compiler relational invariants
valid, I = isa(t1, Tuple{Int}) ? __inc(t1, t2::Tuple{OrdinalRangeInt}) : __inc(t1, t2::Tuple{OrdinalRangeInt,OrdinalRangeInt,Vararg{OrdinalRangeInt}})
valid, I = __inc(tail(state), tail(indices))
return valid, (first(rng), I...)
end

Expand Down Expand Up @@ -552,9 +548,7 @@ module IteratorsMD
if __is_valid_range(I, rng) && state[1] != first(rng)
return true, (I, tail(state)...)
end
t1, t2 = tail(state), tail(indices)
# avoid dynamic dispatch by telling the compiler relational invariants
valid, I = isa(t1, Tuple{Int}) ? __dec(t1, t2::Tuple{OrdinalRangeInt}) : __dec(t1, t2::Tuple{OrdinalRangeInt,OrdinalRangeInt,Vararg{OrdinalRangeInt}})
valid, I = __dec(tail(state), tail(indices))
return valid, (last(rng), I...)
end

Expand Down
5 changes: 1 addition & 4 deletions base/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -383,10 +383,7 @@ filter(f, t::Tuple) = length(t) < 32 ? filter_rec(f, t) : Tuple(filter(f, collec
isequal(t1::Tuple, t2::Tuple) = length(t1) == length(t2) && _isequal(t1, t2)
_isequal(::Tuple{}, ::Tuple{}) = true
function _isequal(t1::Tuple{Any,Vararg{Any}}, t2::Tuple{Any,Vararg{Any}})
isequal(t1[1], t2[1]) || return false
t1, t2 = tail(t1), tail(t2)
# avoid dynamic dispatch by telling the compiler relational invariants
return isa(t1, Tuple{}) ? _isequal((), t2::Tuple{}) : _isequal(t1, t2::Tuple{Any,Vararg{Any}})
return isequal(t1[1], t2[1]) && _isequal(tail(t1), tail(t2))
end
function _isequal(t1::Any32, t2::Any32)
for i = 1:length(t1)
Expand Down

0 comments on commit 4890be5

Please sign in to comment.