From 1ff860cf5085a6c2de471c33415c2f099f7fb92e Mon Sep 17 00:00:00 2001 From: Rafael Fourquet Date: Tue, 12 Dec 2017 11:41:50 +0100 Subject: [PATCH] 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 --- base/bitset.jl | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/base/bitset.jl b/base/bitset.jl index eb27c2024fb40..59402f962346e 100644 --- a/base/bitset.jl +++ b/base/bitset.jl @@ -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! @@ -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