Commit Graph
1 Commits
Author SHA1 Message Date
yetone 432393217a sessions: a shard's gob definitions are compiled once, not once a shard (#3493)
Every call shard's frame is its own gob stream: callFrame's type
definitions, then the frame. loadCalls made a gob.Decoder for each, so
each of a start's ~15.6k shards compiled the same definitions again. A
cold Requests build (every upgrade, since requests-v1.bin is keyed on the
binary) spent ~400 MB of its allocation and most of its GC there.

decodeFrame keeps one Decoder in a pool along with the definition bytes
it has read, and hands it only the value message when a frame's
definitions are byte for byte those. A frame of other definitions gets a
new Decoder; a Decoder that failed is dropped; bytes left over after the
frame are an error, as before.

Measured on a copy of real data (15,656 shards, 117 MB), cold step 2 of
the GUI warm-up (QueryPage(All)): allocation 2190 -> 1968 MB, CPU
5.51 -> 3.92 s, maxrss 448 -> ~410 MB. Wall stays ~2.2-2.6 s: the
serial row building on the consumer goroutine is the limit, next.

Run at this head (94503901c + this, origin/main has only 402577c15 in
internal/gateway since):
- go test -count=1 -timeout 45m ./internal/sessions ./internal/usage: ok
- TestDecodeFrameAsAFreshDecoder: every frame equals a fresh Decoder's,
  a frame of other definitions in between, and cut, padded, empty and
  definitions-only frames are errors that leave the next frame reading.
- TestDecodeFrameKeepsDefinitions: 8 allocations a frame kept vs 478
  fresh.
- Broken by hand: always a fresh Decoder -> KeepsDefinitions red (479
  vs 478); reuse without comparing definitions -> AsAFreshDecoder red
  ("unknown type id or corrupted data" on the other writer's frame).
- gofmt -l clean; GOOS=linux and GOOS=windows go vet -tags nogui
  ./internal/sessions ok.
2026-10-25 06:45:23 +08:00