diff --git a/internal/usage/pair_scratch_test.go b/internal/usage/pair_scratch_test.go index 4214a1235..3d0796f27 100644 --- a/internal/usage/pair_scratch_test.go +++ b/internal/usage/pair_scratch_test.go @@ -3,6 +3,7 @@ package usage import ( "fmt" "maps" + "runtime" "testing" "time" @@ -119,3 +120,59 @@ func TestPairScratchGoesWhenUnused(t *testing.T) { } } } + +// A pass's decode buffer is kept for the next across collections, as the +// matcher's scratch is (#2278), and goes on ReleaseScratch. +func TestRowBufferKeptPastCollections(t *testing.T) { + now := time.Date(2026, 10, 1, 13, 0, 0, 0, time.UTC) + c := &rowChunk{} + for i := range 300 { + c.add(Row{Record: Record{Time: now.Add(-time.Duration(i) * time.Minute), Agent: "claude", Model: "m", Session: "s", Input: i, RequestID: fmt.Sprint("req-", i)}}, "", int64(i), false) + } + packed := c.pack() + if len(packed.Archive) == 0 { + t.Fatal("the chunk wasn't packed") + } + t.Cleanup(ReleaseScratch) + ReleaseScratch() + read := func() *rowBuffer { + rows := lentRows() + open := rows.of(packed) + if len(open.Rows) != 300 || open.row(299).Input != 299 { + t.Fatalf("read %d rows", len(open.Rows)) + } + b := rows.b + rows.release() + return b + } + first := read() + runtime.GC() + runtime.GC() + if got := read(); got != first { + t.Fatal("a pass after a collection decoded into a new buffer") + } + // two passes at once each have one, and both are kept + a, b := lentRows(), lentRows() + a.of(packed) + b.of(packed) + if a.b == b.b { + t.Fatal("two passes at once shared a buffer") + } + a.release() + b.release() + n := 0 + for i := range keptRows { + if keptRows[i].Load() != nil { + n++ + } + } + if n != 2 { + t.Fatalf("%d buffers kept after two passes", n) + } + ReleaseScratch() + for i := range keptRows { + if keptRows[i].Load() != nil { + t.Fatal("ReleaseScratch kept a decode buffer") + } + } +} diff --git a/internal/usage/request_page.go b/internal/usage/request_page.go index 7c42c9ee2..bc8af2fa8 100644 --- a/internal/usage/request_page.go +++ b/internal/usage/request_page.go @@ -453,7 +453,7 @@ type chunkRows struct { } // rowBuffer is what a chunkRows decodes into, kept between passes -// (rowBuffers): each of a page's passes over every chunk grew its own from +// (keptRows): 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 { @@ -464,7 +464,40 @@ type rowBuffer struct { open rowChunk } -var rowBuffers = sync.Pool{New: func() any { return new(rowBuffer) }} +// keptRows is the buffers passes put back, kept as matchScratch is. A +// sync.Pool kept them until the next collection, which a poll while an +// agent works runs several times a second, and the next pass grew one to +// the largest session chunk again: ~550 MB of the ~2.7 GB 90 s of polls +// allocated, in matchedBlocks alone (#2278). They go with the matcher's +// scratch, on ReleaseScratch or scratchLinger after the last pass. +var keptRows [4]atomic.Pointer[rowBuffer] + +// rowBufferFor is a kept buffer, or a new one when every kept one is out +func rowBufferFor() *rowBuffer { + for i := range keptRows { + if b := keptRows[i].Swap(nil); b != nil { + return b + } + } + return new(rowBuffer) +} + +// keepRows puts b back, unless as many are kept as keptRows holds +func keepRows(b *rowBuffer) { + for i := range keptRows { + if keptRows[i].CompareAndSwap(nil, b) { + lingerScratch() + return + } + } +} + +// releaseRows lets go of every kept buffer +func releaseRows() { + for i := range keptRows { + keptRows[i].Store(nil) + } +} // lentRows is a chunkRows whose strings are lent (chunkRows.lent). func lentRows() chunkRows { return chunkRows{lent: true} } @@ -477,7 +510,7 @@ func (r *chunkRows) of(c *rowChunk) *rowChunk { } if r.c != c { if r.b == nil { - r.b = rowBuffers.Get().(*rowBuffer) + r.b = rowBufferFor() } b := r.b var lent *[]byte @@ -511,7 +544,7 @@ func (r *chunkRows) release() { 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) + keepRows(b) } } @@ -1634,6 +1667,7 @@ func mapFor[K comparable, V any](m map[K]V, n int) map[K]V { func ReleaseScratch() { keptMatch.Store(nil) keptPicks.Store(nil) + releaseRows() releasePriced() } @@ -1659,6 +1693,7 @@ func lingerScratch() { scratchSweep.t = time.AfterFunc(scratchLinger, func() { keptMatch.Store(nil) keptPicks.Store(nil) + releaseRows() }) return }