Skip to content

Commit

Permalink
BitSet: add a couple small optimizations
Browse files Browse the repository at this point in the history
* isempty: using _check0 instead of all makes it 35% faster
* ==: checking first non-overlapping parts is more likely
  to be faster, as the the lower and upper parts of the bits
  field are unlikely to be zero (at least for a freshly
  created BitSet
  • Loading branch information
rfourquet committed Dec 12, 2017
1 parent 13f97db commit 1ff860c
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions base/bitset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ function empty!(s::BitSet)
s
end

isempty(s::BitSet) = all(equalto(CHK0), s.bits)
isempty(s::BitSet) = _check0(s.bits, 1, length(s.bits))

# Mathematical set functions: union!, intersect!, setdiff!, symdiff!

Expand Down Expand Up @@ -343,18 +343,19 @@ function ==(s1::BitSet, s2::BitSet)
included = overlap0 >= l2 # whether a2's indices are included in a1's
overlap = included ? l2 : overlap0

# compare overlap values
if overlap > 0
_memcmp(pointer(a1, b2-b1+1), pointer(a2), overlap<<3) == 0 || return false
end

# Ensure remaining chunks are zero
# Ensure non-overlap chunks are zero (unlikely)
_check0(a1, 1, l1-overlap0) || return false
if included
_check0(a1, b2-b1+l2+1, l1)
else
_check0(a2, 1+overlap, l2) || return false
end

# compare overlap values
if overlap > 0
_memcmp(pointer(a1, b2-b1+1), pointer(a2), overlap<<3) == 0 || return false
end

return true
end

Expand Down

0 comments on commit 1ff860c

Please sign in to comment.