usage: a pass's decode buffer is kept past a collection, not grown again after every GC (#2278)
chunkRows decoded each chunk into a rowBuffer from a sync.Pool. A poll
while an agent works collects several times a second, and each
collection emptied the pool, so the next pass grew a buffer to the
largest session chunk again. In 90 s of polls on the 65 GB bench HOME,
unpackReusing allocated 779 MB (486 MB flat), 553 MB of it under
matchedBlocks.
The buffers are now kept as the matcher's scratch is: up to four in
keptRows (atomic pointers), taken by chunkRows.of and put back by
release, and let go with the scratch, on ReleaseScratch or scratchLinger
after the last pass.
Run on mote at this tree, rebased on origin/main:
- go test -count=1 ./internal/usage/ ./internal/usageapi/: ok, at ae906d4df on df31059d6
- GOOS=linux|windows|darwin go vet -tags nogui ./internal/usage/
./internal/usageapi/ ./internal/gui/: ok
- TestRowBufferKeptPastCollections (new): a buffer survives two
runtime.GC() between passes, two passes at once get two and both are
kept, ReleaseScratch drops them. Broken by hand, red each time: release
not putting the buffer back; ReleaseScratch not calling releaseRows.
Bench (hmw.sh, 90 s, gateway calls appended every 3 s, 10 endpoints
polled), second runs, mote load ~32:
- v0.1.2193 (e11): 2742 MB allocated, unpackReusing 779 MB,
matchedBlocks 553 MB, pairRowsIn 858 MB, late GCs 26, fp_peak 333 MB.
- this (e12): 2030 MB, unpackReusing 199 MB, matchedBlocks 48 MB,
pairRowsIn 346 MB, late GCs 16, fp_peak 347 MB (within noise).
Not tried: a real session file growing during the bench (the bench only
appends gateway calls).
This commit is contained in:
@@ -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")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user