The Sessions and Usage index (sessions.json, ~18k files on a long
history) was JSON. Every start decoded all of it through reflection,
and every save, which happens at most once a minute while a session is
live, marshalled it whole again. It is now a compact binary form
(cache_binary.go):
- A magic and a version header.
- Then length-prefixed records, varints for the numbers.
- Interned names, as the JSON path interned them.
- The rare nested parts (Claude Code's window, a Codex identity,
StateMore) as JSON blobs.
Records are decoded in parallel.
A JSON index from an older magpie is still read. loadCache saves it
again at once, so it turns binary on the first start even when no
session changed; before, a save came only after a file changed.
A binary file is refused whole, never half read: when it is cut
anywhere, has a byte added, or is of another zone or version. That
means one full read again, as a refused JSON file meant before.
TestCacheBinaryKnowsEveryField fails when state, day or Tokens gains a
field that the binary form doesn't carry.
On the real acctp history, on mote (18,346 files):
- The file is 14.8 MB, down from 23.3 MB.
- Decoding takes 19 ms, down from 69 ms, and allocates 28 MB, down
from 58 MB.
- Encoding takes 10 ms, down from 98 ms, and allocates 3.1 MB, down
from 32.5 MB.
- The heap kept after decoding is the same: 24 MB.
- /api/sessions from a w94 build on the binary index and from a w92
build (before) on the JSON index answer the same bytes (5000
sessions). Neither rewrote the index.
- The page-open footprint peak was 152 to 214 MB on both builds at
load 60 to 80. It is too noisy to claim a change.
Run:
- mica-run go test -count=1 -timeout 45m ./internal/sessions
./internal/usage ./internal/gui: all ok.
- GOOS=linux and GOOS=windows go vet ./internal/sessions
./internal/usage: ok. go vet ./internal/sessions: ok.
- gofmt -l: clean.
- TestCacheBinaryRefusesBadFiles cuts the file at every length, adds a
byte, changes the zone and the version, and flips every byte; it
checks that a bad file is never accepted and never panics.
Broken by hand:
- The decoder skipped Provider: TestCacheBinaryIsJSON went red.
- loadCache stopped re-saving a JSON index:
TestCacheJSONRewrittenOnStart went red, the file still JSON.
At the pushed head (rebased on 500f39886): go test ./internal/sessions
./internal/testenv ok.
Not tried: Windows and Linux runs of the package; the mapping read is
shared code and unchanged there.