Skip to content

Commit

Permalink
Better codegen for specialized in(v, ::Tuple)
Browse files Browse the repository at this point in the history
Since we were already specializing in for Tuples anyway, we should be
generating code that can take advantage of the tuple type, rather than
doing a generic, type-unstable traversal.

Before:
```julia
julia> @Btime Base.in($(5), (1, 2.0, 3, "hey", 5.2))
  53.541 ns (6 allocations: 224 bytes)
false
```

After:
```julia
julia> @Btime Base.in($(5), (1, 2.0, 3, "hey", 5.2))
  1.666 ns (0 allocations: 0 bytes)
false
```

The new code statically unrolls the tuple comparisons, and thus also
compiles away any of the comparisons that are statically known to be
not equal, such as the `==(::Int, ::String)` comparison for element 4 in
the example above.
  • Loading branch information
NHDaly committed Apr 10, 2024
1 parent e9a24d4 commit 8ccf78b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
45 changes: 32 additions & 13 deletions base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1332,22 +1332,41 @@ used to implement specialized methods.
"""
in(x) = Fix2(in, x)

for ItrT = (Tuple,Any)
# define a generic method and a specialized version for `Tuple`,
# whose method bodies are identical, while giving better effects to the later
@eval function in(x, itr::$ItrT)
$(ItrT === Tuple ? :(@_terminates_locally_meta) : :nothing)
anymissing = false
for y in itr
v = (y == x)
if ismissing(v)
anymissing = true
elseif v
return true
end
function in(x, itr::Any)
anymissing = false
for y in itr
v = (y == x)
if ismissing(v)
anymissing = true
elseif v
return true
end
end
return anymissing ? missing : false
end

# Specialized variant of in for Tuple, which can generate typed comparisons for each element
# of the tuple, skipping values that are statically known to be != at compile time.
function in(x, itr::Tuple)
@_terminates_locally_meta
_in_tuple(x, itr, false)
end
# This recursive function will be unrolled at compiletime, and will not generate separate
# llvm-compiled specializations for each step of the recursion.
@inline function _in_tuple(x, @nospecialize(itr::Tuple), anymissing::Bool)
@_terminates_locally_meta
# Base case
if isempty(itr)
return anymissing ? missing : false
end
# Recursive case
v = (itr[1] == x)
if ismissing(v)
anymissing = true
elseif v
return true
end
return _in_tuple(x, Base.tail(itr), anymissing)
end

const = in
Expand Down
12 changes: 12 additions & 0 deletions test/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,21 @@ end
@test lt5(4) && !lt5(5)
end

@testset "in tuples" begin
@test (5, (1,5,10,11))
@test (0, (1,5,10,11))
@test (5, (1,"hi","hey",5.0))
@test (0, (1,"hi","hey",5.0))
@test (5, (5,))
@test (0, (5,))
@test (5, ())
end

@testset "ni" begin
@test ([1,5,10,11], 5)
@test !([1,10,11], 5)
@test ((1,5,10,11), 5)
@test ((1,10,11), 5)
@test (5)([5,1])
@test !(42)([0,1,100])
@test (0)(1:10)
Expand Down

0 comments on commit 8ccf78b

Please sign in to comment.