Commit Graph
4 Commits
Author SHA1 Message Date
yetone ba301e98e2 usage: a page's passes over the chunks decode into buffers kept between them (#3493)
Each pass of a Requests page over every chunk (foldCodexRows, the
response pairs, visibleLocal, matchedBlocks, visit, partsOf, newest,
rowsOf) started a chunkRows with no buffers and grew its rows, strings,
shapes and decode buffer from nothing. A warm start's All page and
Codex's effort after it read the same rows pass after pass, ~30 MB a
pass. chunkRows now takes a rowBuffer from a pool, unpacks into it
(unpackReusing gets a dst chunk too, so the open chunk isn't allocated
each block), and gives it back on release, its strings cleared so no
row text is held.

Measured on the real data copy (sbpairs: 12k Codex rollouts and the
Claude projects), warm-up harness TestZZWarm, each binary run 3 times
in a row and the first run dropped (the chunk file is named by this
package's source, so alternating binaries rebuilds it):
  step 2 (QueryPage All) allocated   835 MB -> 666 MB
  allocated through step 3 (effort)  994 MB -> 820 MB
  unpackReusing in step 2            163 MB -> 34 MB
  peak footprint                     ~240 MB, unchanged

Run at this head (base defbb1a15): go test ./internal/usage
./internal/sessions ./internal/gui ok; -race -count=3 of the chunkRows,
Pack and Archive tests ok; GOOS=linux and windows go vet -tags nogui
./internal/usage ok; gofmt clean. TestChunkRowsReuseReadsEachBlock now
runs 4 passes, lent and not, in both orders, through one pool; broken
by hand (a reused rows slice cut to its cap, not the block's count) it
fails "pass 0 block 1: 0 rows, want 3".

Not tried: Linux and Windows real-data runs.
2026-10-25 15:10:47 +08:00
yetone 3767addd4d 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.
2026-10-25 07:06:36 +08:00
yetone 31bff66be3 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).
2026-10-25 06:20:17 +08:00
yetone 16d20c0b3d usage: a packed block keeps its compressed bytes, not S2's worst-case buffer
s2.Encode makes its output MaxEncodedLen(len(src)) long, more than the
rows' own bytes, and pack kept the archive as a slice of it
(slices.Clip trims the capacity, not the allocation). Every cached
session block and every gateway log block (log_index.go) so held its
whole uncompressed size plus S2's slack, while Bytes counted only the
compressed length, so the cache budget undercounted it too. pack now
encodes into pooled scratch (the rows' bytes and S2's output) and
copies the archive out at its own length.

Measured on a sandbox HOME (a clone of a 23 GB Codex + Claude Code
history, ~380k calls), magpie serve --web, warm call caches, heap
profile from /v1/magpie/heap:
- at rest (2 min): s2.Encode's archives 17.2 MB -> 9.2 MB (slices.Clone)
- at the warm-up's peak (6 s): archives 45.3 MB -> 30.6 MB
- liveHeap at 30 s 96.1 MB -> 90.3 MB, footprint 213 MB -> 203 MB

TestPackedArchiveHoldsOnlyItsBytes packs 100 blocks of 2000 rows and
measures what stays reachable after a collection. On the old pack it
fails: "100 packed blocks hold 16035 KB for 3887 KB of archives"
(3 of 3 runs); with the change it passes, and blocks packed through
the same scratch read back their own rows.

At 8b48dc5d6 (this commit and the two beside it, on 5c557c793):
gofmt -l clean; GOOS=linux and GOOS=windows go vet -tags nogui ./...
clean; go test -race -tags nogui ./internal/usage/ ./internal/sessions/
ok; go test -tags nogui -timeout 45m ./... with pipefail: 68 packages
ok, no FAIL.
Warm start in a sandbox HOME, magpie serve --web (ps RSS / footprint /
heapSys at 30s): origin/main 650 / 213 / 649 MB, all three 606 / 208 /
598 MB. Allocated in the first 30s: 2557 MB before, 1878 MB after.
2026-10-21 09:41:29 +08:00