Commit Graph
2 Commits
Author SHA1 Message Date
yetone bbb4fd478a sessions: a parse's models, days and tools are sorted slices, not maps
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).
2026-10-27 04:39:56 +08:00
yetone a0f3b33b1f sessions: a Claude reply's tool blocks and usage are decoded from their object, not by a decoder copying the rest of the line
ccTools and ccReply's usage each opened a json.Decoder over the rest of
the line, and the decoder copies what it reads ahead into its own buffer.
A long tool input was copied once per tool block and once for usage. In a
cold-start heap profile (11.57 GB allocated in the first 2 minutes) this
was the largest site: Decoder.refill, 1.6 GB, via ccReply/ccTools.
objectEnd now finds the end of the object (brackets outside strings) and
json.Unmarshal decodes only those bytes, which still checks them.

Run:
- TestClaudeReplyDecodesWithoutCopyingTheLine (new; a 1 MiB tool input
  plus a Skill tool through claudeLine): red on the old decoder path,
  "a 1024 KB reply allocated 4100 KB to read"; green now, under 256 KB.
- TestObjectEnd (new): nesting, braces and escapes in strings,
  unterminated, a leading space, empty.
- go test ./internal/sessions/ ok, and with -race ok (79s);
  -race -count=5 on the new tests ok.
- gofmt -l clean; GOOS=linux and GOOS=windows go vet -tags nogui ok.
2026-10-21 18:19:47 +08:00