Skip to content

Commit c9156d4

Browse files
authored
Implement specialized _findin when second arg is AbstractSet (#48891)
Prior to this PR, `_findin` converts its second argument to a `Set` before using it. However, this overhead is unnecessary if the argument is already an `AbstractSet`. `_findin` is in turn called by `findall`, so this change provides a fast path for `findall(in(s), ...)`, where `s` is an `AbstractSet`: ```julia julia> s = Set([1,2,3,4]) Set{Int64} with 4 elements: 4 2 3 1 julia> findall(in(s), [1,3,5,7,5,3,1]) 4-element Vector{Int64}: 1 2 6 7 ```
1 parent deba118 commit c9156d4

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

base/array.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2809,14 +2809,14 @@ function indexin(a, b::AbstractArray)
28092809
]
28102810
end
28112811

2812-
function _findin(a::Union{AbstractArray, Tuple}, b)
2812+
function _findin(a::Union{AbstractArray, Tuple}, b::AbstractSet)
28132813
ind = Vector{eltype(keys(a))}()
2814-
bset = Set(b)
28152814
@inbounds for (i,ai) in pairs(a)
2816-
ai in bset && push!(ind, i)
2815+
ai in b && push!(ind, i)
28172816
end
28182817
ind
28192818
end
2819+
_findin(a::Union{AbstractArray, Tuple}, b) = _findin(a, Set(b))
28202820

28212821
# If two collections are already sorted, _findin can be computed with
28222822
# a single traversal of the two collections. This is much faster than

0 commit comments

Comments
 (0)