A kept parse holds a model or two, a day or two and a few tools, and Go's
map of one entry is a whole group of eight: ~500 bytes for one model's
Tokens, where a sorted slice entry is 56. Over a long history (15.9k kept
parses) state.Models, Days, day.Models, Tools and Skills held 18.4MB as
maps and 4.2MB as named[V], a slice kept in name order. It is written as
the same JSON object a map writes, byte for byte, so the parse cache
saved before reads unchanged; names are interned with unique.Make, and
plain integers in ints and Tokens are read without reflection.
Measured with the real ~/.codex and ~/.claude histories (sandbox HOME,
warm cache, 40s after start), base 3393e61d0 vs this change, two
alternating runs each:
live heap 102 / 105MB -> 92 / 91MB
footprint 164 / 167MB -> 156 / 150MB
Cache decode at start: ~58ms -> ~61ms (best of 7).
Side by side on the same data, /api/sessions/overview?agent=codex and
/api/stats?days=30 are identical; the other overview queries differed
only in the two Claude sessions being written during the run.
TestNamedIsAMapInJSON: marshal is byte-equal to a map's (HTML-escaped
key included), round trip, a repeated name keeps its last value, an
unknown Tokens field is ignored, omitempty leaves none out, and 1.5,
"1", [1], 1e3 and a too-big int fail as they fail into a map. Broken by
hand (the int fast path accepting 1.5) it went red.
Run at this head (rebased on 2beed3b04): gofmt clean; go build ./...;
GOOS=linux go vet -tags nogui sessions, gui, usage; GOOS=windows vet
sessions; go test -timeout 45m ./internal/sessions ./internal/usage
./internal/gui ./internal/gateway all ok.
Not run: the full ./... suite and the GUI node tests (no GUI change).
A long history's kept parses held 28MB live after a start's decode of
sessions.json (15,624 files). 15,345 of them have exactly one day, and
that day's model map is the file's own, so every such file held two
equal maps of ~700B each. decodeCache's share now points the day at the
file's map when they are equal.
A kept state is never written: use, unuse and dropUse are the only
writers of Models, and every parser (sessions.go, calls.go, calls_dsh,
dsh, droid, grok, kimi, cline) writes a clone, which copies the two
maps apart.
Run, at this head on 42ab93551:
- the real sessions.json (sbpairs, 22.8MB): live heap after decode
28MB -> 21MB, 3 of 3.
- StatsAt(3650) over the real sessions in a sandbox HOME, digest of
the 96 days before 10-24: a21dc1d1... with this change (twice, the
second from the cache it wrote) and with the sharing turned off
(twice): the same.
- TestDecodeCacheSharesOneDaysModels: red with the sharing removed
("holds its models twice") and red with clone keeping the shared map
("the kept state was written").
- go test -count=1 ./internal/sessions ./internal/usage ./internal/gui
ok; go test -race ./internal/sessions ok; GOOS=linux and
GOOS=windows go vet -tags nogui on the three ok; gofmt -l clean.
Not tried: a full go test ./... (one suite per machine).
A save marshalled the whole index into one buffer: 22.8MB for a long
history (15,624 files), grown by doubling to ~85MB of allocation a save,
and a save follows every refresh that read a changed session file, so a
start while agents run, and a Sessions or Usage poll after any call. It
is written through a json.Encoder into a 256KB buffer handed to a 1MB
bufio.Writer on the temp file, keys in json.Marshal's order and escaped
as it escapes them, so the file is the same bytes.
Measured on the real sessions.json (sbpairs, 22,836,803 bytes):
json.Marshal 54-73ms and 85MB alloc; encodeCache 51ms and 14MB alloc;
the output equal byte for byte.
Run at this tree (3767addd4 + the race skip + this change):
- go test -race -count=1 ./internal/sessions: ok
- go test -count=1 ./internal/sessions ./internal/usage ./internal/gui
./internal/agent/...: ok
- GOOS=linux and GOOS=windows go vet -tags nogui ./internal/sessions: ok;
gofmt -l clean
- TestEncodeCacheIsMarshal compares with json.Marshal for 3002 files (a
key and a title with <, &, quotes and U+2028, a file of no parse), an
empty index and none. Broken by hand (keys in map order): red; restored:
green.
Not tried: the save on Windows or Linux.
decodeCache's Unmarshal made a string for every file's title, cwd and
model names, however many files gave the same one. On the real history
copy (sbpairs, 27,699 kept files) 23,689 sessions a script ran carry one
~420-byte prompt as their title. The kept parses held 54 MB after a
collection, 11 MB of it such copies. Each decode worker now takes those
fields (title, cwd, first, named, model, provider, msg_model, msg_day,
codex_nickname, and the Codex identity's role and parents) through
unique.Make, so the parses hold 44 MB. The decode took 104-114 ms, as it
did (121 ms when measured for e2dbb0340).
One QueryPage(All) from requests-v1.bin under GOGC=25, as the GUI's
warm-up runs it: max RSS 319-327 MB -> 296-305 MB. From cold (no file):
390 MB, against 448-462 MB before this and 31bff66be.
TestDecodeCacheSharesRepeats decodes 2000 files of two titles and one
cwd, model and role, and counts the copies held. It is red with share
taken out (2000 of each).
Run at this head: go test ./internal/sessions ./internal/usage (ok);
go vet ./internal/sessions, GOOS=linux and GOOS=windows -tags nogui;
gofmt -l. Not run: the full suite (one per machine).
One json.Unmarshal of sessions.json (40MB, 27,679 files on a long
history) took 310 to 440ms of every start before the first page (#3493).
decodeCache checks the file is valid JSON (gjson), reads its version and
zone, splits the files without copying them, and decodes each file's
parse with json.Unmarshal on up to 8 goroutines. The result is what one
Unmarshal gave: a key given twice is its last, and the file is refused
where it was refused (bad JSON, another version or zone, no files, a
parse that doesn't decode). Only the keys are copied out of the read.
Real data (sandbox copy of the live sessions.json, load ~60): Unmarshal
311ms, decodeCache 121ms, reflect.DeepEqual the same 27,679 parses.
Run at 5d2260d3e (origin/main, fetched): go test -timeout 45m of
sessions (14.6s), usage (18.4s), usageapi (7.9s), cli (35.9s) and tui
(15.4s) ok; TestDecodeCacheIsUnmarshal -race ok. gofmt clean; GOOS=linux
and GOOS=windows go vet -tags nogui ./internal/sessions/ clean.
TestDecodeCacheIsUnmarshal (new) compares decodeCache with Unmarshal on
3000 files, escaped keys, a null parse, a key given twice, no files, an
old version, another zone, a bad parse first and last, a cut file,
trailing bytes, a missing comma and garbage. Red with the validity check
taken out (trailing, missing comma), with a bad parse ignored, with the
first of two keys kept, and with the zone not compared.