Skip to content

Commit

Permalink
minor change and comment in bitset
Browse files Browse the repository at this point in the history
  • Loading branch information
gaissmai committed Feb 18, 2025
1 parent 9e288ef commit da51e73
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
19 changes: 13 additions & 6 deletions internal/bitset/bitset.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// can inline BitSet.Set with cost 63
// can inline BitSet.Clear with cost 24
// can inline BitSet.Test with cost 26
// can inline BitSet.Rank0 with cost 66
// can inline BitSet.Rank0 with cost 57
// can inline BitSet.Clone with cost 7
// can inline BitSet.Compact with cost 35
// can inline BitSet.FirstSet with cost 25
Expand Down Expand Up @@ -275,19 +275,22 @@ func (b BitSet) Rank0(i uint) (rnk int) {
if wordIdx := int(i >> 6); wordIdx >= len(b) {
// inlined popcount, whole slice
for _, x := range b {
// don't test for x != 0,
// simpler and faster, most of the time
rnk += bits.OnesCount64(x)
}
} else {
// inlined popcount, partial slice ...
// don't test for x != 0,
// simpler and faster, most of the time
for _, x := range b[:wordIdx] {
rnk += bits.OnesCount64(x)
}

// ... plus partial word?
if bitsIdx := i & 63; bitsIdx != 0 {
rnk += bits.OnesCount64(b[wordIdx] << (64 - bitsIdx))
}

// ... plus partial word,
// make it unconditional, don't test i&63 != 0,
// simpler and faster, most of the time
rnk += bits.OnesCount64(b[wordIdx] << (64 - i&63))
}

// correct for offset by one
Expand All @@ -298,6 +301,8 @@ func (b BitSet) Rank0(i uint) (rnk int) {
func popcntSlice(s []uint64) (cnt int) {
for _, x := range s {
// count all the bits set in slice.
// don't test for x != 0,
// simpler and faster, most of the time
cnt += bits.OnesCount64(x)
}
return
Expand All @@ -307,6 +312,8 @@ func popcntSlice(s []uint64) (cnt int) {
func popcntAnd(s, m []uint64) (cnt int) {
for j := 0; j < len(s) && j < len(m); j++ {
// words are bitwise & followed by popcount.
// don't test for x != 0,
// simpler and faster, most of the time
cnt += bits.OnesCount64(s[j] & m[j])
}
return
Expand Down
22 changes: 17 additions & 5 deletions internal/bitset/bitset_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ func BenchmarkBitSetRankChild(b *testing.B) {

// make unique random numbers
randsUnique := map[int]bool{}
for range 10 {
randsUnique[rand.IntN(256)] = true
for range 20 {
bit := rand.IntN(500)
if bit > 255 {
continue
}
randsUnique[bit] = true
}

// sort them ascending
Expand All @@ -42,13 +46,21 @@ func BenchmarkBitSetRankChild(b *testing.B) {
func BenchmarkBitSetRankPrefix(b *testing.B) {
var bs BitSet
for range 200 {
bs = bs.Set(uint(rand.IntN(512)))
bit := rand.IntN(1_000)
if bit > 511 {
continue
}
bs = bs.Set(uint(bit))
}

// make uniques random numbers
randsUnique := map[int]bool{}
for range 10 {
randsUnique[rand.IntN(512)] = true
for range 20 {
bit := rand.IntN(1_000)
if bit > 511 {
continue
}
randsUnique[bit] = true
}

// sort them ascending
Expand Down

0 comments on commit da51e73

Please sign in to comment.