Skip to content

Commit 1215956

Browse files
perf: optimize bitmap container batch iteration
1 parent 65603a1 commit 1215956

1 file changed

Lines changed: 22 additions & 8 deletions

File tree

bitmapcontainer.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,17 @@ func (bcmi *bitmapContainerManyIterator) nextMany(hs uint32, buf []uint32) int {
187187
bitset = bcmi.ptr.bitmap[base]
188188
continue
189189
}
190-
t := bitset & -bitset
191-
buf[n] = uint32(((base * 64) + bits.OnesCount64(t-1))) | hs
192-
n = n + 1
193-
bitset ^= t
190+
if len(buf)-n >= 64 {
191+
for bitset != 0 {
192+
buf[n] = uint32((base*64)+bits.TrailingZeros64(bitset)) | hs
193+
n++
194+
bitset &= bitset - 1
195+
}
196+
continue
197+
}
198+
buf[n] = uint32((base*64)+bits.TrailingZeros64(bitset)) | hs
199+
n++
200+
bitset &= bitset - 1
194201
}
195202

196203
bcmi.base = base
@@ -215,10 +222,17 @@ func (bcmi *bitmapContainerManyIterator) nextMany64(hs uint64, buf []uint64) int
215222
bitset = bcmi.ptr.bitmap[base]
216223
continue
217224
}
218-
t := bitset & -bitset
219-
buf[n] = uint64(((base * 64) + bits.OnesCount64(t-1))) | hs
220-
n = n + 1
221-
bitset ^= t
225+
if len(buf)-n >= 64 {
226+
for bitset != 0 {
227+
buf[n] = uint64((base*64)+bits.TrailingZeros64(bitset)) | hs
228+
n++
229+
bitset &= bitset - 1
230+
}
231+
continue
232+
}
233+
buf[n] = uint64((base*64)+bits.TrailingZeros64(bitset)) | hs
234+
n++
235+
bitset &= bitset - 1
222236
}
223237

224238
bcmi.base = base

0 commit comments

Comments
 (0)