Skip to content

Commit cbe3975

Browse files
committed
merge: feed one TurboPFor block (<=256) to pforWriter
Before this commit, DeltaReader.Read decoded a TurboPFor block of up to 256 values and then called pforWriter.PutUint32, one value at a time (this is 23% of the merge's CPU). This change brings down shard 0’s merge time from 512s to 433s. Found using Claude Fable 5.1.
1 parent 01a4b35 commit cbe3975

3 files changed

Lines changed: 29 additions & 7 deletions

File tree

‎internal/index/merge.go‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -307,11 +307,11 @@ func writeDocids(destdir string, trigrams []Trigram, idxDocid map[Trigram][]uint
307307
docids[0] += (idx.docidBase - last)
308308
for docids != nil {
309309
for _, d := range docids {
310-
if err := dw.PutUint32(d); err != nil {
311-
return err
312-
}
313310
last += d
314311
}
312+
if err := dw.PutUint32s(docids); err != nil {
313+
return err
314+
}
315315
docids = dr.Read()
316316
}
317317
}
@@ -459,10 +459,8 @@ func writePos(destdir string, trigrams []Trigram, idxDocid map[Trigram][]uint32,
459459
dr.Reset(&meta, idx.rd.data.Data)
460460

461461
for docids := dr.Read(); docids != nil; docids = dr.Read() {
462-
for _, d := range docids {
463-
if err := dw.PutUint32(d); err != nil {
464-
return err
465-
}
462+
if err := dw.PutUint32s(docids); err != nil {
463+
return err
466464
}
467465
}
468466
}

‎internal/index/pforwriter.go‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ func (pw *pforWriter) PutUint32(u uint32) error {
3535
return nil
3636
}
3737

38+
// PutUint32s is a more efficient version of PutUint32
39+
// for len(vals)<=256 (one TurboPFor block).
40+
func (pw *pforWriter) PutUint32s(vals []uint32) error {
41+
if encoded := pw.enc.AddBlock(vals); encoded != nil {
42+
if _, err := pw.f.Write(encoded); err != nil {
43+
return err
44+
}
45+
}
46+
return nil
47+
}
48+
3849
func (pw *pforWriter) Flush() error {
3950
b := pw.enc.EncodeBlock()
4051
if len(b) == 0 {

‎internal/turbopfor/pforenc/pforenc.go‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,19 @@ func (se *StreamEncoder) Add(val uint32) (full bool) {
223223
return se.n == len(se.vals)
224224
}
225225

226+
// AddBlock stores len(vals)<=256 uint32s in the StreamEncoder's buffer.
227+
func (se *StreamEncoder) AddBlock(vals []uint32) []byte {
228+
n := copy(se.vals[se.n:], vals)
229+
se.n += n
230+
full := se.n == len(se.vals)
231+
if full {
232+
encoded := se.EncodeBlock()
233+
se.n = copy(se.vals[:], vals[n:])
234+
return encoded
235+
}
236+
return nil
237+
}
238+
226239
// EncodeBlock encodes the accumulated values in TurboPFor format
227240
// and returns the bytes of the (full or remainder) block.
228241
//

0 commit comments

Comments
 (0)