Skip to content

Commit fccdc63

Browse files
tamirmsclaude
andcommitted
fastaggregation: count before allocating in FastAnd
FastAnd with three or more inputs intersected them pairwise, allocating an intermediate container per input and key even when the result was empty. The input with the fewest keys now drives: each of its keys is looked up in the first four inputs with stack cursors, and an array key is also checked for a shared value, before anything is allocated. A key that survives is intersected count-first: bitmaps in a stack scratch bitmap, with runs clearing what they do not cover, and arrays through the existing and and iand. Any further inputs are intersected into the result one at a time, as before; walking every input for every key was up to 1.9x slower than pairwise And when many inputs keep their keys. bitmapContainer.andArray hoists its slice headers out of the loop, a quarter faster on amd64 and arm64, and ParAnd's worker now calls the same andKChain. Xeon 8375C, against pairwise And in the same binary: jointly-empty 14.0 µs -> 1.9 µs (7.3x), 1 alloc instead of 18 singletons-10000 25.9 µs -> 12.4 µs (2.1x) array30-runs 486 ns -> 286 ns (1.7x) BenchmarkRealDataFastAnd (added) is 1.9x to 97x faster than master on all twelve datasets. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0198cmGsvonq19wTyzKBsw2Z
1 parent 1c0052a commit fccdc63

5 files changed

Lines changed: 466 additions & 30 deletions

File tree

‎bitmapcontainer.go‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -845,15 +845,13 @@ func (bc *bitmapContainer) iandArray(ac *arrayContainer) container {
845845

846846
func (bc *bitmapContainer) andArray(value2 *arrayContainer) *arrayContainer {
847847
answer := newArrayContainerCapacity(len(value2.content))
848-
answer.content = answer.content[:cap(answer.content)]
849-
c := value2.getCardinality()
848+
out, bitmap := answer.content[:cap(answer.content)], bc.bitmap
850849
pos := 0
851-
for k := 0; k < c; k++ {
852-
v := value2.content[k]
853-
answer.content[pos] = v
854-
pos += int(bc.bitValue(v))
850+
for _, v := range value2.content {
851+
out[pos] = v
852+
pos += int((bitmap[v>>6] >> (v & 63)) & 1)
855853
}
856-
answer.content = answer.content[:pos]
854+
answer.content = out[:pos]
857855
return answer
858856
}
859857

‎fastaggregation.go‎

Lines changed: 173 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -125,26 +125,188 @@ func (x1 *Bitmap) repairAfterLazy() {
125125
}
126126
}
127127

128-
// FastAnd computes the intersection between many bitmaps quickly
129-
// Compared to the And function, it can take many bitmaps as input, thus saving the trouble
130-
// of manually calling "And" many times.
128+
// FastAnd computes the intersection between many bitmaps quickly.
129+
// Compared to the And function, it can take many bitmaps as input.
131130
//
132-
// Performance hints: if you have very large and tiny bitmaps,
133-
// it may be beneficial performance-wise to put a tiny bitmap
134-
// in first position.
131+
// With three or more inputs, FastAnd walks the keys of the input with the
132+
// fewest keys and looks each one up in the first four inputs, also checking
133+
// an array container for a shared value, before allocating anything for it.
134+
// The remaining inputs are then intersected into the result one at a time.
135135
func FastAnd(bitmaps ...*Bitmap) *Bitmap {
136-
if len(bitmaps) == 0 {
136+
switch len(bitmaps) {
137+
case 0:
137138
return NewBitmap()
138-
} else if len(bitmaps) == 1 {
139+
case 1:
139140
return bitmaps[0].Clone()
141+
case 2:
142+
return And(bitmaps[0], bitmaps[1])
140143
}
141-
answer := And(bitmaps[0], bitmaps[1])
142-
for _, bm := range bitmaps[2:] {
143-
answer.And(bm)
144+
driver := 0
145+
for i := 1; i < len(bitmaps); i++ {
146+
if bitmaps[i].highlowcontainer.size() < bitmaps[driver].highlowcontainer.size() {
147+
driver = i
148+
}
149+
}
150+
dra := &bitmaps[driver].highlowcontainer
151+
// The walk covers the first width inputs, each with a cursor that resumes
152+
// where the previous key stopped. Past those, intersecting an input into
153+
// the result costs less per key than walking it.
154+
const width = 4
155+
n := min(len(bitmaps), width)
156+
var pos [width]int
157+
var cs [width + 1]container // the window and a driver past it
158+
answer := NewBitmap()
159+
keys:
160+
for j := 0; j < dra.size(); j++ {
161+
key := dra.getKeyAtIndex(j)
162+
dc := dra.getContainerAtIndex(j)
163+
// Only an array is checked for a value it shares with each input: a
164+
// lookup per value that usually stops at the first. For a run or a
165+
// bitmap the check costs about as much as the intersection.
166+
_, check := dc.(*arrayContainer)
167+
m := 0
168+
if !dc.isFull() { // a full container changes nothing
169+
cs[0] = dc
170+
m = 1
171+
}
172+
for i, bm := range bitmaps[:n] {
173+
if i == driver {
174+
continue
175+
}
176+
ra := &bm.highlowcontainer
177+
p := pos[i]
178+
if p < ra.size() && ra.getKeyAtIndex(p) < key {
179+
p = ra.advanceUntil(key, p)
180+
}
181+
pos[i] = p
182+
if p == ra.size() {
183+
break keys
184+
}
185+
if ra.getKeyAtIndex(p) != key {
186+
continue keys
187+
}
188+
pos[i]++
189+
c := ra.getContainerAtIndex(p)
190+
if c.isFull() {
191+
continue
192+
}
193+
if check && !dc.intersects(c) {
194+
continue keys
195+
}
196+
cs[m] = c
197+
m++
198+
}
199+
if c := andK(cs[:m]); c != nil {
200+
if answer.IsEmpty() {
201+
// Room for up to 64 of the driver's remaining keys at once.
202+
k := min(dra.size()-j, 64)
203+
answer.highlowcontainer = roaringArray{
204+
keys: make([]uint16, 0, k),
205+
containers: make([]container, 0, k),
206+
needCopyOnWrite: make([]bool, 0, k),
207+
}
208+
}
209+
answer.highlowcontainer.appendContainer(key, c, false)
210+
}
211+
}
212+
for i := n; i < len(bitmaps) && !answer.IsEmpty(); i++ {
213+
if i != driver {
214+
answer.And(bitmaps[i])
215+
}
144216
}
145217
return answer
146218
}
147219

220+
// andK intersects the containers of one key, none of them full, and returns
221+
// nil when the intersection is empty. It may reorder cs.
222+
func andK(cs []container) container {
223+
switch len(cs) {
224+
case 0:
225+
return newRunContainer16Range(0, maxCapacity-1)
226+
case 1:
227+
return cs[0].clone()
228+
}
229+
// The smallest array goes first; the rest keep their order.
230+
smallest := -1
231+
for i, c := range cs {
232+
if x, ok := c.(*arrayContainer); ok && (smallest < 0 || x.getCardinality() < cs[smallest].getCardinality()) {
233+
smallest = i
234+
}
235+
}
236+
if smallest >= 0 {
237+
a := cs[smallest]
238+
copy(cs[1:smallest+1], cs[:smallest])
239+
cs[0] = a
240+
return andKChain(cs)
241+
}
242+
n := 0
243+
for i, c := range cs {
244+
if _, ok := c.(*runContainer16); ok {
245+
cs[n], cs[i] = cs[i], cs[n]
246+
n++
247+
}
248+
}
249+
if n == len(cs) {
250+
return andKChain(cs)
251+
}
252+
return andKBitmaps(cs[n:], cs[:n])
253+
}
254+
255+
// andKChain intersects cs in order: the first two into a new container, then
256+
// the rest into it in place, so nothing bigger than the first result is built.
257+
func andKChain(cs []container) container {
258+
c := cs[0].and(cs[1])
259+
for i := 2; i < len(cs) && !c.isEmpty(); i++ {
260+
c = c.iand(cs[i])
261+
}
262+
if c.isEmpty() {
263+
return nil
264+
}
265+
return c
266+
}
267+
268+
// andKBitmaps ANDs the bitmaps in a scratch bitmap on the stack, stopping as
269+
// soon as the result is empty. Runs act as masks: only the words between
270+
// their common start and end are ANDed, and the gaps inside a run are cleared
271+
// afterwards.
272+
func andKBitmaps(bms, runs []container) container {
273+
lo, hi := 0, maxCapacity-1
274+
gaps := false
275+
for _, c := range runs {
276+
rc := c.(*runContainer16)
277+
lo, hi = max(lo, int(rc.minimum())), min(hi, int(rc.maximum()))
278+
gaps = gaps || len(rc.iv) > 1
279+
}
280+
if lo > hi {
281+
return nil
282+
}
283+
first, last := lo/64, hi/64
284+
var scratch [bitmapContainerSize]uint64
285+
w := scratch[first : last+1]
286+
src := bms[0].(*bitmapContainer).bitmap[first : last+1]
287+
card := uint64(0)
288+
if len(bms) == 1 {
289+
copy(w, src)
290+
}
291+
for _, c := range bms[1:] {
292+
if card = andCardSlice(w, src, c.(*bitmapContainer).bitmap[first:last+1]); card == 0 {
293+
return nil
294+
}
295+
src = w
296+
}
297+
if len(bms) == 1 || gaps || lo%64 != 0 || (hi+1)%64 != 0 {
298+
resetBitmapRange(scratch[:], first*64, lo)
299+
resetBitmapRange(scratch[:], hi+1, (last+1)*64)
300+
for _, c := range runs {
301+
clearBitmapGaps(scratch[:], c.(*runContainer16).iv, lo, hi+1)
302+
}
303+
if card = popcntSlice(w); card == 0 {
304+
return nil
305+
}
306+
}
307+
return containerFromWords(scratch[:], first, last, int(card))
308+
}
309+
148310
// FastOr computes the union between many bitmaps quickly, as opposed to having to call Or repeatedly.
149311
// It might also be faster than calling Or repeatedly.
150312
func FastOr(bitmaps ...*Bitmap) *Bitmap {

0 commit comments

Comments
 (0)