Skip to content

Commit

Permalink
change intersect order to reduce time (#36339)
Browse files Browse the repository at this point in the history
  • Loading branch information
Moelf committed Dec 2, 2021
1 parent 26d2e19 commit ed9ad60
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
16 changes: 15 additions & 1 deletion base/abstractset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,14 @@ Set{Float64} with 1 element:
```
"""
function intersect(s::AbstractSet, itr, itrs...)
# heuristics to try to `intersect` with the shortest Set on the left
if length(s)>50 && haslength(itr) && all(haslength, itrs)
min_length, min_idx = findmin(length, itrs)
if length(itr) > min_length
new_itrs = setindex(itrs, itr, min_idx)
return intersect(s, itrs[min_idx], new_itrs...)
end
end
T = promote_eltype(s, itr, itrs...)
if T == promote_eltype(s, itr)
out = intersect(s, itr)
Expand All @@ -156,7 +164,13 @@ function intersect(s::AbstractSet, itr, itrs...)
return intersect!(out, itrs...)
end
intersect(s) = union(s)
intersect(s::AbstractSet, itr) = mapfilter(in(s), push!, itr, emptymutable(s, promote_eltype(s, itr)))
function intersect(s::AbstractSet, itr)
if haslength(itr) && hasfastin(itr) && length(s) < length(itr)
return mapfilter(in(itr), push!, s, emptymutable(s, promote_eltype(s, itr)))
else
return mapfilter(in(s), push!, itr, emptymutable(s, promote_eltype(s, itr)))
end
end

const = intersect

Expand Down
3 changes: 3 additions & 0 deletions test/sets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,9 @@ end
s = S([1,2]) S([3,4])
@test s == S()
s = intersect(S([5,6,7,8]), S([7,8,9]))
slong = S(collect(3:63))
# test #36339 length/order short-cut
@test intersect(S([5,6,7,8]), slong) == intersect(slong, S([5,6,7,8]))
@test s == S([7,8])
@test intersect(S([2,3,1]), S([4,2,3]), S([5,4,3,2])) == S([2,3])
let s1 = S([1,2,3])
Expand Down

2 comments on commit ed9ad60

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

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

Executing the daily package evaluation, I will reply here when finished:

@nanosoldier runtests(ALL, isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

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

Your package evaluation job has completed - possible new issues were detected. A full report can be found here.

Please sign in to comment.