Commit Graph
2 Commits
Author SHA1 Message Date
yetone 2a3d9f5aeb usage: a session file's rows are made by the reader that read it (#3493)
A cold Requests build (every upgrade) read the call shards eight at a
time, but turned every call into a row on the one loop that takes them:
logRecord, the account resolver, pricing, interning and packing, ~1 s
of serial CPU over a real history, with the readers waiting on the
window behind it.

prefetchSources becomes prefetchEach: each reader has a make of its own
from newMake, and the loop has its own for a source not wanted. Request
rows are made there. Each make has its own Pricer and Swapped memo, so
neither needs a lock; the session resolver is made once (sync.OnceValue)
and only read after. The serial number is still given in the loop, in
source order. The window now holds packed chunks, smaller than the
calls it held.

Measured on a copy of real data (15,656 shards, 3,451 Claude Code
subagent meta files), GUI warm-up step 2 (QueryPage(All)), cold
(requests-v1.bin written for the other binary), alternating runs:
- before (432393217): 3.24 s / 448 MB maxrss, 2.18 s / 433 MB
- after: 1.85 s / 417 MB, 1.84 s / 410 MB
The readers are now mostly in syscalls (shard and meta.json reads).

Run at this head (432393217 + this):
- go test -count=1 -timeout 45m ./internal/usage: ok; with -race: ok
- go test -count=1 -timeout 45m ./internal/gui: one red,
  TestRestartHandsTheGatewayOver (18 of 19 requests answered, EOF), the
  open #3451; alone -count=5 it passes. Load was ~45.
- TestPrefetchEachMakeIsAReadersOwn: 9 makes (8 readers, the loop's
  own) and no race. Broken by hand: one make shared by every reader ->
  -race reports DATA RACE, red.
- gofmt -l clean; GOOS=linux and GOOS=windows go vet -tags nogui
  ./internal/usage ok.
2026-10-25 06:58:27 +08:00
yetone 5604c411a9 usage, gui: Usage › Requests' All opens at once after magpie has been up a few seconds, and in half the time before (the owner) — Requests on All stayed a skeleton for 39 s on the owner's magpie right after it started: the first All parses every session file (12.3k here: 11k Codex sessions, 1.2k Claude Code projects' files), one after the other, each a wait on the disk. Those a query has to parse are now read 8 at a time ahead of the loop that takes them in order (prefetchSources), at most 32 waiting, so the history is never all held at once; and magpie reads the whole history in the background once it is up, right after the Sessions page's own warm-up and not beside it on the disk, so the page opens on the kept index. Tested in a sandbox HOME on a copy of the owner's real ~/.codex and ~/.claude history (magpie app, dev UI): the first All right at startup took 10.6 s before and 4.6–4.9 s now; opened 20 s after startup it took 10.0 s before and 0.01 s now; later Alls 0.3–0.4 s as before. TestPrefetchSources (each source handed back as its own, read in parallel, no more than the window ahead, every one read once, an unwanted one read when asked) is new. go vet, go test -race ./internal/usage, go build (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui ./... under a temp HOME pass. 2026-10-05 20:18:46 +08:00