From 3767addd4d8b8924e538b461fe74b6f0922946ee Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 25 Oct 2026 07:06:36 +0800 Subject: [PATCH] usage: a block's strings and shapes are read into buffers the next block reuses Each Requests pass (warm-up step 2's whole-history page, step 3's Codex effort pairs) decodes every packed block through chunkRows, which already reused the rows' buffer. Each block still made its strings slice, its shapes slice and a []int of every string's offset and length, ~170MB of the ~450MB a pass allocated on a 15.6k-file history. chunkRows now keeps the strings' and shapes' slices too (unpackReusing), and the offsets are found by walking the copied section's length prefixes a second time instead of being stored. The strings themselves still point into one copy per block, so a row taken from a block keeps its text after the buffer moves on; only the slices are reused, and nothing holds a block's slices past the next of(). Measured (real history, sbpairs: 15,656 call shards, gui warm-up steps, zzbase = 2a3d9f5ae, alternated, two rounds each): - total alloc through step 3, cold: base 3171/2994MB, new 2610/2626MB - warm (requests-v1.bin read): base 1763/1751MB, new 1399/1408MB - times within noise: cold step 2 base 1.86/1.61s, new 1.56/1.85s; step 3 0.55-0.79s both Run at this tree (2a3d9f5ae + this change): - go test -race -count=1 ./internal/usage: ok - go test -count=1 ./internal/gui ./internal/sessions: ok - GOOS=linux and GOOS=windows go vet -tags nogui ./internal/usage ./internal/gui: ok; gofmt -l clean - TestChunkRowsReuseReadsEachBlock reads blocks of 400, 3, 120, 1 and 400 rows through one chunkRows against their unpacked rows. Broken by hand (keep a string the earlier block left in the buffer): red; restored: green. Not tried: a Windows or Linux run of the warm-up. --- internal/usage/pack_archive_test.go | 33 +++++++++++++++++++ internal/usage/request_page.go | 12 +++++-- internal/usage/request_snapshot.go | 51 +++++++++++++++++++++-------- 3 files changed, 81 insertions(+), 15 deletions(-) diff --git a/internal/usage/pack_archive_test.go b/internal/usage/pack_archive_test.go index 19717e53a..10b9cfc6e 100644 --- a/internal/usage/pack_archive_test.go +++ b/internal/usage/pack_archive_test.go @@ -2,6 +2,7 @@ package usage import ( "fmt" + "reflect" "runtime" "testing" "time" @@ -79,3 +80,35 @@ func TestFewRowsArePacked(t *testing.T) { t.Fatal("3 rows: not read back") } } + +// Blocks read one after another through one chunkRows reuse its rows', +// strings' and shapes' buffers, a large block's and then a small one's, and +// each reads back as it was packed: a row's text is the block's own, never +// a string or shape the block before left in the buffer. +func TestChunkRowsReuseReadsEachBlock(t *testing.T) { + now := time.Now().Round(0) + block := func(k, n int) (*rowChunk, *rowChunk) { + c := &rowChunk{} + for i := range n { + r := Row{Record: Record{Time: now.Add(time.Duration(i) * time.Second), Agent: fmt.Sprint("agent", k, "-", i%3), Provider: "p", Model: fmt.Sprint("m", k, "-", i%5), Input: k*1000 + i, Session: fmt.Sprint("s", k, "-", i)}} + c.add(r, fmt.Sprint("msg", k, "-", i), int64(i), i%4 == 0) + } + 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) + } + } + } + if cap(rows.strs) == 0 || cap(rows.shapes) == 0 { + t.Fatalf("no buffers kept: %d strings, %d shapes", cap(rows.strs), cap(rows.shapes)) + } +} diff --git a/internal/usage/request_page.go b/internal/usage/request_page.go index 6aa8da943..a8f445c50 100644 --- a/internal/usage/request_page.go +++ b/internal/usage/request_page.go @@ -397,10 +397,12 @@ func rowsIn(c *rowChunk) int { // packed copies it keeps: ~100 MB of a start that peaked at 508 MB over // 420k calls. A packed chunk is decoded when a pass reaches it, into a // buffer the next one's decode reuses, and refs still name the packed -// chunk. +// chunk. Its strings' and shapes' slices are reused the same way. type chunkRows struct { c, open *rowChunk buf []packedRow + strs []string + shapes [][rowTexts]uint32 } // of is c's rows to read. What the page set on c since (a Codex actor or @@ -410,13 +412,19 @@ func (r *chunkRows) of(c *rowChunk) *rowChunk { return c } if r.c != c { - open := c.unpackInto(r.buf) + open := c.unpackReusing(r.buf, r.strs, r.shapes) if open == nil { // never written so: pack wrote it in this process open = &rowChunk{Source: c.Source, Computer: c.Computer} } if cap(open.Rows) > cap(r.buf) { r.buf = open.Rows[:0] } + if cap(open.Strings) > cap(r.strs) { + r.strs = open.Strings[:0] + } + if cap(open.Shapes) > cap(r.shapes) { + r.shapes = open.Shapes[:0] + } r.c, r.open = c, open } r.open.codexActors, r.open.codexCompactions, r.open.codexIdentities = c.codexActors, c.codexCompactions, c.codexIdentities diff --git a/internal/usage/request_snapshot.go b/internal/usage/request_snapshot.go index 788eac307..bb60f5d21 100644 --- a/internal/usage/request_snapshot.go +++ b/internal/usage/request_snapshot.go @@ -134,6 +134,13 @@ func (k keptStrings) of(s string) string { // 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) +} + +// 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). +func (c *rowChunk) unpackReusing(into []packedRow, strs []string, shapes [][rowTexts]uint32) *rowChunk { if c == nil || len(c.Archive) == 0 { return c } @@ -154,8 +161,8 @@ func (c *rowChunk) unpackInto(into []packedRow) *rowChunk { data = data[k:] return n } - rows, strs := take(), take() - if rows > uint64(len(data)) || strs > uint64(len(data)) { + rows, count := take(), take() + if rows > uint64(len(data)) || count > uint64(len(data)) { return nil } next := *c @@ -165,18 +172,21 @@ func (c *rowChunk) unpackInto(into []packedRow) *rowChunk { } else { next.Rows = make([]packedRow, int(rows)) } - next.Strings = make([]string, int(strs)) + if uint64(cap(strs)) >= count { + next.Strings = strs[:count] + } else { + next.Strings = make([]string, int(count)) + } next.Bytes = int64(len(next.Rows))*int64(unsafe.Sizeof(packedRow{})) + codexActorBytes(next.codexActors) // The strings are copied out in one piece: one allocation a block, not - // one a string. - at := make([]int, 0, 2*int(strs)) + // one a string. The section is walked once to find its end, and its + // copy again to cut the strings from it. section := data for range next.Strings { n := take() if n > uint64(len(data)) { return nil } - at = append(at, len(section)-len(data), int(n)) data = data[n:] } if !valid { @@ -184,19 +194,34 @@ func (c *rowChunk) unpackInto(into []packedRow) *rowChunk { } text := string(section[:len(section)-len(data)]) for i := range next.Strings { - from, n := at[2*i], at[2*i+1] - next.Strings[i] = text[from : from+n] + var n, shift uint64 + for k := 0; ; k++ { + b := text[k] + n |= uint64(b&0x7f) << shift + if b < 0x80 { + text = text[k+1:] + break + } + shift += 7 + } + next.Strings[i] = text[:n] + text = text[n:] next.Bytes += int64(n) + 16 } - shapes := take() - if shapes > uint64(len(data)) { + shapeCount := take() + if shapeCount > uint64(len(data)) { return nil } - next.Shapes, next.shapeIDs = make([][rowTexts]uint32, int(shapes)), nil + if uint64(cap(shapes)) >= shapeCount { + next.Shapes = shapes[:shapeCount] + } else { + next.Shapes = make([][rowTexts]uint32, int(shapeCount)) + } + next.shapeIDs = nil for i := range next.Shapes { for j := range next.Shapes[i] { n := take() - if n >= strs { + if n >= count { return nil } next.Shapes[i][j] = uint32(n) @@ -216,7 +241,7 @@ func (c *rowChunk) unpackInto(into []packedRow) *rowChunk { } for j := range p.Varied { n := take() - if n >= strs { + if n >= count { return nil } p.Varied[j] = uint32(n)