Give hot dumped types one-letter serde names

Merged
SA
2mo ago
+304-28

Should also be improved by https://github.com/twittner/minicbor/pull/73

Session dumps are CBOR maps keyed by field and variant name, so key strings are most of a dump.
Measured on two sessions, key strings were 76% of a 500-snippet, 250-function REPL session and 69% of a session holding 2,000 small dicts.
The worst offenders are types written once per value, heap entry or location: every Value wrote its variant name (InternString is 13 bytes around a 1–3 byte id), every heap entry refcount/color/data, every dict item key/value/hash.

This renames every field and variant of those types to one upper-case letter (first letter of the name, else the next unused letter in it, else the first unused letter of the alphabet):
Value, HeapEntry, CcColor, List, Tuple, Dict (its hand-written impl), DictEntry, SetEntry, CodeRange, LocationEntry and CodeLoc.
Everything else keeps readable names; per-function and one-off types are a small share of a dump.

Notes for review:

  • HeapEntry and CcColor are in heap/mod.rs; the change there is serde attributes only.
  • CodeLoc is public in monty-types, so its serde shape for hosts becomes {"L": …, "C": …}.
  • HeapData variants are not renamed: heap_payloads! forwards variant attributes to HeapReadOutput, which has no serde derive.
  • Dumps are unpublished in this format, so there are no aliases; dump_v13.bin is regenerated (18.7 KB → 12.3 KB).
  • CLAUDE.md records the convention: a new field or variant on these types takes an unused letter.
  • #909 adds SuspendPosition to code.rs; it should get the same treatment once both land.

Measured with these renames applied on top of #909:

beforeafter
2,000-dict session dump817 KB407 KB
500-snippet session dump322 KB211 KB
session_load (local criterion)588 µs541 µs (−8.6%)
session_dump (local criterion)145.6 µs149.0 µs (noise)

On CodSpeed most of session_dump's cost was glibc realloc copying the growing buffer, which the local macOS run does not show, so CodSpeed is the better measure of dump time.

🤖 Generated with Claude Code

https://claude.ai/code/session_01DFJ9rUjXVHCtQpxhZvoiGb


Summary by cubic

Shrinks session dumps by renaming serde fields and variants on frequently written types to single upper-case letters. Key strings were ~70% of dump size; this halves it (817 KB → 407 KB for a 2,000-dict session, 322 KB → 211 KB for a 500-snippet session) while load time drops roughly 8% and dump time is unchanged.

Notes

  • Renames fields on per-value, per-heap-entry, per-function, and per-frame types: Value, HeapEntry, CcColor, List, Tuple, Dict, DictEntry, SetEntry, CodeRange, LocationEntry, CodeLoc, Function, Signature, Identifier, NameScope, Code, ExceptionEntry, SerializedFrame, Instance, Closure, FunctionDefaults, and the hot HeapData variants; all other types keep readable names.
  • CodeLoc is public in monty-types, so its host-facing serde shape becomes {"L": …, "C": …}.
  • Dumps are unpublished in this format, so no aliases are needed; dump_v13.bin is regenerated.
  • HeapReadOutput no longer repeats the registry's serde attributes, since they cannot apply to it; the registry's "append-only" notes are dropped since CBOR tags variants by name, not position.
  • dump_rejects_transient_gc_colors was updated to forge the now one-letter C/B keys instead of the old text keys.
  • The datatest harness now round-trips every non-suspending case's idle heap through the dump codec and re-dumps the result, which must match byte for byte, except xfail cases whose expected failure must come from the run itself; mount-fs suspensions also round-trip so open files pass through the dump format.
  • The dump fixture was expanded to cover previously undumped heap types (iterators, dict views, dates, bytes, ranges, slices, bound methods, random.Random) and now suspends inside an exec() frame with a globals dict, finally, except, and __init__; regenerating it asserts the suspension landed on host_call. slice.indices() is documented as unimplemented.
  • CLAUDE.md documents the convention: new fields or variants on these types take an unused letter.