Skip to content
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

Type-stable codegen for specialized in(v, ::Tuple) #54026

Merged
merged 3 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Better codegen for specialized in(v, ::Tuple)
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 authored and aviatesk committed May 9, 2024
commit 3fcb64da3ced98009f198f47e017f1c29f7ffe43
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
aviatesk marked this conversation as resolved.
Show resolved Hide resolved
NHDaly marked this conversation as resolved.
Show resolved Hide resolved
# 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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell, there weren't any tests for in(x, ::Tuple) before this PR, so I added some.


@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