Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

usage: a page's passes over the chunks decode into buffers kept between them (#3493)

yetoneba301e98e220
diff --git a/internal/usage/codex_identity.go b/internal/usage/codex_identity.go
index 4a19cded6..d63cd96c6 100644
--- a/internal/usage/codex_identity.go
+++ b/internal/usage/codex_identity.go
@@ -196,6 +196,7 @@ func codexChunkResponsePairs(gateways, locals []*rowChunk, duplicateLocal map[ro
}
picks := map[string]*pick{}
var rows chunkRows
+ defer rows.release()
for _, chunk := range gateways {
open := rows.of(chunk)
for i := range open.Rows {
@@ -214,6 +215,7 @@ func codexChunkResponsePairs(gateways, locals []*rowChunk, duplicateLocal map[ro
}
}
lent := lentRows() // picks are looked up by a local's id, not keyed
+ defer lent.release()
for _, chunk := range locals {
open := lent.of(chunk)
for i := range open.Rows {
@@ -250,6 +252,7 @@ func foldCodexRows(locals []*rowChunk) map[rowRef]bool {
first := map[uint64]rowRef{}
again := map[uint64][]rowRef{}
rows := lentRows() // an id is kept as its hash
+ defer rows.release()
for _, chunk := range locals {
open := rows.of(chunk)
for i := range open.Rows {
diff --git a/internal/usage/pack_archive_test.go b/internal/usage/pack_archive_test.go
index 10b9cfc6e..280346bb6 100644
--- a/internal/usage/pack_archive_test.go
+++ b/internal/usage/pack_archive_test.go
@@ -95,20 +95,33 @@ func TestChunkRowsReuseReadsEachBlock(t *testing.T) {
}
return c, c.pack()
}
- var rows chunkRows
- for k, n := range []int{400, 3, 120, 1, 400} {
- plain, packed := block(k, n)
- open := rows.of(packed)
- if len(open.Rows) != n {
- t.Fatalf("block %d: %d rows, want %d", k, len(open.Rows), n)
- }
- for i := range n {
- if got, want := open.row(i), plain.row(i); !reflect.DeepEqual(got, want) {
- t.Fatalf("block %d row %d: read %+v, packed %+v", k, i, got, want)
+ sizes := []int{400, 3, 120, 1, 400}
+ plains, packs := make([]*rowChunk, len(sizes)), make([]*rowChunk, len(sizes))
+ for k, n := range sizes {
+ plains[k], packs[k] = block(k, n)
+ }
+ // a pass after another reads through the buffers the last one released,
+ // lent or not: in turn, and a large block after a small one
+ for pass, lent := range []bool{false, true, false, true} {
+ rows := chunkRows{lent: lent}
+ for j := range packs {
+ k := j
+ if pass%2 == 1 {
+ k = len(packs) - 1 - j
+ }
+ open := rows.of(packs[k])
+ if len(open.Rows) != sizes[k] {
+ t.Fatalf("pass %d block %d: %d rows, want %d", pass, k, len(open.Rows), sizes[k])
+ }
+ for i := range sizes[k] {
+ if got, want := open.row(i), plains[k].row(i); !reflect.DeepEqual(got, want) {
+ t.Fatalf("pass %d block %d row %d: read %+v, packed %+v", pass, k, i, got, want)
+ }
}
}
- }
- if cap(rows.strs) == 0 || cap(rows.shapes) == 0 {
- t.Fatalf("no buffers kept: %d strings, %d shapes", cap(rows.strs), cap(rows.shapes))
+ if cap(rows.b.strs) == 0 || cap(rows.b.shapes) == 0 {
+ t.Fatalf("no buffers kept: %d strings, %d shapes", cap(rows.b.strs), cap(rows.b.shapes))
+ }
+ rows.release()
}
}
diff --git a/internal/usage/request_cells.go b/internal/usage/request_cells.go
index f33bedbd1..6affa3599 100644
--- a/internal/usage/request_cells.go
+++ b/internal/usage/request_cells.go
@@ -312,6 +312,7 @@ func (ps *pairedRows) partsOf(pass *cellPass, cache *partCache, identities map[s
sigs := ps.sigs(identities)
parts := make([]*chunkPart, len(ps.all))
var rows chunkRows
+ defer rows.release()
var r Row
for i, c := range ps.all {
sig := sigs[c]
@@ -357,6 +358,7 @@ func (ps *pairedRows) newest(f Filter, parts []*chunkPart, take int) newestHeap
return b.Latest.Compare(a.Latest)
})
var rows chunkRows
+ defer rows.release()
var r Row
for _, c := range order {
if len(selected) == take && !c.Latest.IsZero() && c.Latest.Before(selected[0].at) {
diff --git a/internal/usage/request_page.go b/internal/usage/request_page.go
index bc3baa450..289fdd2fa 100644
--- a/internal/usage/request_page.go
+++ b/internal/usage/request_page.go
@@ -402,18 +402,29 @@ func rowsIn(c *rowChunk) int {
// chunk. Its strings' and shapes' slices are reused the same way.
type chunkRows struct {
c, open *rowChunk
- buf []packedRow
- strs []string
- shapes [][rowTexts]uint32
// lent: the strings are of the decoded block, not a copy of them, and
// the next chunk is decoded over them. For a pass that keeps nothing of
// a row past the next chunk, and clones a string it keeps: the passes
// that pair a page's calls copied every chunk's strings five times,
// ~250 MB of a warm start's page (#3493).
- lent bool
+ lent bool
+ b *rowBuffer
+}
+
+// rowBuffer is what a chunkRows decodes into, kept between passes
+// (rowBuffers): each of a page's passes over every chunk grew its own from
+// nothing, and a warm start's page and Codex's effort after it allocated
+// ~30 MB a pass to read the same rows (#3493).
+type rowBuffer struct {
+ rows []packedRow
+ strs []string
+ shapes [][rowTexts]uint32
decoded []byte
+ open rowChunk
}
+var rowBuffers = sync.Pool{New: func() any { return new(rowBuffer) }}
+
// lentRows is a chunkRows whose strings are lent (chunkRows.lent).
func lentRows() chunkRows { return chunkRows{lent: true} }
@@ -424,22 +435,27 @@ func (r *chunkRows) of(c *rowChunk) *rowChunk {
return c
}
if r.c != c {
+ if r.b == nil {
+ r.b = rowBuffers.Get().(*rowBuffer)
+ }
+ b := r.b
var lent *[]byte
if r.lent {
- lent = &r.decoded
+ lent = &b.decoded
}
- open := c.unpackReusing(r.buf, r.strs, r.shapes, lent)
+ open := c.unpackReusing(&b.open, b.rows, b.strs, b.shapes, lent)
if open == nil { // never written so: pack wrote it in this process
- open = &rowChunk{Source: c.Source, Computer: c.Computer}
+ b.open = rowChunk{Source: c.Source, Computer: c.Computer}
+ open = &b.open
}
- if cap(open.Rows) > cap(r.buf) {
- r.buf = open.Rows[:0]
+ if cap(open.Rows) > cap(b.rows) {
+ b.rows = open.Rows[:0]
}
- if cap(open.Strings) > cap(r.strs) {
- r.strs = open.Strings[:0]
+ if cap(open.Strings) > cap(b.strs) {
+ b.strs = open.Strings[:0]
}
- if cap(open.Shapes) > cap(r.shapes) {
- r.shapes = open.Shapes[:0]
+ if cap(open.Shapes) > cap(b.shapes) {
+ b.shapes = open.Shapes[:0]
}
r.c, r.open = c, open
}
@@ -447,6 +463,17 @@ func (r *chunkRows) of(c *rowChunk) *rowChunk {
return r.open
}
+// release hands r's buffers on to the next pass. Nothing r opened is read
+// after: a lent string kept from it is a clone, and a row a copy.
+func (r *chunkRows) release() {
+ if b := r.b; b != nil {
+ b.open = rowChunk{}
+ clear(b.strs[:cap(b.strs)]) // the strings of a block not lent are its own
+ r.b, r.c, r.open = nil, nil, nil
+ rowBuffers.Put(b)
+ }
+}
+
func (r *chunkRows) row(ref rowRef) Row { return r.of(ref.Chunk).row(ref.Index) }
// rowsOf reads the rows refs name, each chunk once, in the order of chunks.
@@ -457,6 +484,7 @@ func rowsOf(chunks []*rowChunk, refs []rowRef) map[rowRef]Row {
byChunk[ref.Chunk] = append(byChunk[ref.Chunk], ref.Index)
}
var rows chunkRows
+ defer rows.release()
for _, c := range chunks {
indexes, ok := byChunk[c]
if !ok {
@@ -1113,6 +1141,7 @@ func visibleLocal(chunks []*rowChunk) map[rowRef]bool {
duplicates := map[rowRef]bool{}
seen := map[string]bool{}
rows := lentRows() // a kept message id is cloned
+ defer rows.release()
for _, c := range chunks {
open := rows.of(c)
for i := range open.Rows {
@@ -1205,6 +1234,7 @@ func matchedBlocks(gateways []*rowChunk, chunks []*rowChunk, skip map[rowRef]boo
}
byRequest := map[string][]timedRef{}
rows := lentRows() // a kept request id is cloned; a key is looked up
+ defer rows.release()
for _, c := range chunks {
open := rows.of(c)
for i := range open.Rows {
@@ -1462,6 +1492,7 @@ func pairRows(p Period, now time.Time, gateways, chunks, others []*rowChunk, ide
// visit calls fn with each row of the period that no other one stands for.
func (ps *pairedRows) visit(fn func(timedRef, *Row)) {
var rows chunkRows
+ defer rows.release()
// one row for the visit, written over for each: fn is not known
// here, so a row of each would be an allocation of its own, and fn
// keeps none of them
diff --git a/internal/usage/request_snapshot.go b/internal/usage/request_snapshot.go
index 8267a1829..6e0f94869 100644
--- a/internal/usage/request_snapshot.go
+++ b/internal/usage/request_snapshot.go
@@ -149,15 +149,15 @@ func decodeMapped(m *diskMapping, dst, src []byte) (data []byte, ok bool) {
// unpackInto is unpack decoding the rows into rows when it is large enough:
// every field of a row is written, so a reused slice needs no clearing.
func (c *rowChunk) unpackInto(into []packedRow) *rowChunk {
- return c.unpackReusing(into, nil, nil, nil)
+ return c.unpackReusing(nil, into, nil, nil, nil)
}
// unpackReusing is unpackInto decoding the strings' and shapes' slices into
// strs and shapes too, when they are large enough: for a reader that lets
// go of a chunk's rows before it opens the next (chunkRows). With lent, the
// block is decoded into *lent and the strings are of it, not a copy: they
-// are good until *lent is decoded into again.
-func (c *rowChunk) unpackReusing(into []packedRow, strs []string, shapes [][rowTexts]uint32, lent *[]byte) *rowChunk {
+// are good until *lent is decoded into again. The chunk is dst, when given.
+func (c *rowChunk) unpackReusing(dst *rowChunk, into []packedRow, strs []string, shapes [][rowTexts]uint32, lent *[]byte) *rowChunk {
if c == nil || len(c.Archive) == 0 {
return c
}
@@ -185,7 +185,11 @@ func (c *rowChunk) unpackReusing(into []packedRow, strs []string, shapes [][rowT
if rows > uint64(len(data)) || count > uint64(len(data)) {
return nil
}
- next := *c
+ next := dst
+ if next == nil {
+ next = new(rowChunk)
+ }
+ *next = *c
next.Archive, next.Locations, next.mapped = nil, nil, nil
if uint64(cap(into)) >= rows {
next.Rows = into[:rows]
@@ -289,5 +293,5 @@ func (c *rowChunk) unpackReusing(into []packedRow, strs []string, shapes [][rowT
if !valid || len(data) != 0 {
return nil
}
- return &next
+ return next
}