Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
usage: a block's strings and shapes are read into buffers the next block reuses
yetone3767addd4d8b
diff --git a/internal/usage/pack_archive_test.go b/internal/usage/pack_archive_test.goindex 19717e53a..10b9cfc6e 100644--- a/internal/usage/pack_archive_test.go+++ b/internal/usage/pack_archive_test.go@@ -2,6 +2,7 @@ package usageimport ("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.goindex 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 *rowChunkbuf []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 processopen = &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.codexIdentitiesdiff --git a/internal/usage/request_snapshot.go b/internal/usage/request_snapshot.goindex 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 := datafor 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 = nilfor 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)