Skip to content

Commit 1a02226

Browse files
committed
BitSet: add a couple small optimizations
* 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
1 parent 53fb940 commit 1a02226

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

base/bitset.jl

+8-7
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ function empty!(s::BitSet)
227227
s
228228
end
229229

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

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

@@ -344,18 +344,19 @@ function ==(s1::BitSet, s2::BitSet)
344344
included = overlap0 >= l2 # whether a2's indices are included in a1's
345345
overlap = included ? l2 : overlap0
346346

347-
# compare overlap values
348-
if overlap > 0
349-
_memcmp(pointer(a1, b2-b1+1), pointer(a2), overlap<<3) == 0 || return false
350-
end
351-
352-
# Ensure remaining chunks are zero
347+
# Ensure non-overlap chunks are zero (unlikely)
353348
_check0(a1, 1, l1-overlap0) || return false
354349
if included
355350
_check0(a1, b2-b1+l2+1, l1)
356351
else
357352
_check0(a2, 1+overlap, l2) || return false
358353
end
354+
355+
# compare overlap values
356+
if overlap > 0
357+
_memcmp(pointer(a1, b2-b1+1), pointer(a2), overlap<<3) == 0 || return false
358+
end
359+
359360
return true
360361
end
361362

0 commit comments

Comments
 (0)