From 31bff66be3eb981ccc8a4a217c34173c3d6ca176 Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 25 Oct 2026 06:20:17 +0800 Subject: [PATCH] usage: a session file's few calls are kept packed too pack left a chunk of fewer than 64 rows as rows. Most session files hold a handful of calls, so a cold build (the first start after an update, or with no requests-v1.bin) kept 13,972 of 15,624 local chunks unpacked: 98,320 rows. The same chunks read back from requests-v1.bin are all archives, so the cold build alone held them as rows. Now every chunk with a row is packed when its archive is smaller, as the file's entries already were. Measured on the real history copy (sbpairs: 15.6k session files, 420k calls), one QueryPage(All) from cold under GOGC=25 as the GUI's warm-up runs it, three runs each, old then new: - heap in use after: 267/267/269 MB -> 229/227/232 MB - peak heap in use: 357/367/341 MB -> 308/338/336 MB - max RSS: 454/462/448 MB -> 420/424/449 MB - page: 2.95/2.41/2.52 s -> 3.66/2.19/2.42 s (no change past noise) A page after a new call, packed chunks read from the file: 120-143 ms, as before. TestFewRowsArePacked packs three rows and reads them back. It is red with the old 64-row floor. Run at this head: go test ./internal/usage (ok); go test -run 'Usage|Request|Tray|Session' ./internal/gui ./internal/gateway (ok); go vet ./internal/usage, GOOS=linux and GOOS=windows -tags nogui; gofmt -l. Not run: the full suite (one per machine). --- internal/usage/pack_archive_test.go | 19 +++++++++++++++++++ internal/usage/request_snapshot.go | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/internal/usage/pack_archive_test.go b/internal/usage/pack_archive_test.go index 2a956fa73..19717e53a 100644 --- a/internal/usage/pack_archive_test.go +++ b/internal/usage/pack_archive_test.go @@ -60,3 +60,22 @@ func TestPackedArchiveHoldsOnlyItsBytes(t *testing.T) { t.Fatalf("read back %q and %q", ua.row(99).Model, ub.row(99).Model) } } + +// A block of a few rows is packed too: most session files hold a handful +// of calls, and a started magpie kept ~14k of them unpacked, ~98k rows +// and ~38 MB more than the same blocks read back from requests-v1.bin. +func TestFewRowsArePacked(t *testing.T) { + now := time.Now() + c := &rowChunk{} + for i := range 3 { + c.add(Row{Record: Record{Time: now.Add(time.Duration(i) * time.Second), Agent: "codex", Provider: "openai", Model: "gpt-5.5", Input: 1200 + i, Output: 300, Session: "s"}}, fmt.Sprint("msg", i), int64(i), false) + } + packed := c.pack() + if len(packed.Archive) == 0 || packed.Rows != nil || packed.Bytes >= c.Bytes { + t.Fatalf("3 rows: archive %d bytes, %d rows kept, %d bytes for %d", len(packed.Archive), len(packed.Rows), packed.Bytes, c.Bytes) + } + back := packed.unpack() + if back == nil || len(back.Rows) != 3 || back.row(2).Input != 1202 || back.row(2).Model != "gpt-5.5" { + t.Fatal("3 rows: not read back") + } +} diff --git a/internal/usage/request_snapshot.go b/internal/usage/request_snapshot.go index b4a268132..788eac307 100644 --- a/internal/usage/request_snapshot.go +++ b/internal/usage/request_snapshot.go @@ -33,7 +33,7 @@ var rowDecodes = sync.Pool{New: func() any { return new([]byte) }} var rowEncodes = sync.Pool{New: func() any { return new([2][]byte) }} func (c *rowChunk) pack() *rowChunk { - if c == nil || len(c.Rows) < 64 || len(c.Archive) != 0 { + if c == nil || len(c.Rows) == 0 || len(c.Archive) != 0 { return c } encoded, locations := c.encode(func(encoded []byte) bool { return int64(len(encoded)) < c.Bytes })