4. Measure a speed-up first, and weigh it against what it costs
Status: accepted (2026-10-19), amended 2026-10-27 and 2026-11-30: a measured gain counts at any size; a cost is weighed, not priced at 5%
Context
Every speed-up costs something: a dependency, a slower release build, more code to fuzz. Most ideas come with other projects’ numbers (ruff: PGO 8 to 14%), not ours.
Decision
Build it, time it with hyperfine -N against the same build without it, and check the output is
the same. Keep a speed-up when it is faster beyond noise on the case it targets and not slower
beyond noise on any other. Beyond noise means the medians differ by more than twice their
standard deviation, on a quiet machine, with the runs in both orders.
A speed-up that adds a cost needs more than a gain. A cost is a dependency, a slower build or a platform-specific path. Weigh the cost in the pull request, against what the gain is worth:
- A new dependency must be maintained, pass
cargo deny, and build forwasm32-unknown-unknown. - A release build that gets noticeably slower needs a gain users notice.
- No
unsafein our own crates (0002), whatever the gain.
Record what we turn down here.
Turned down
-
simdutf8 (2026-10-19): 1.8 against 3.6 ms on 82 MB, about 1% of the run. Tried again on 2026-11-30 under the any-size rule: slower beyond noise in both orders on rows.json (176 to 196 ms), rows.jsonl (35.6 to 39.8 ms) and doc-pretty.json (141 to 152 ms), faster on none. Probably because without
unsafethe checked bytes cannot become aString, so each&strborrow validates the text again. -
PGO (2026-10-19, fat LTO both ways): within σ on every
scripts/bench.pycase and on 78 MB of JSON Lines, for twice the release build time. -
A whitespace-run skip in
trivia(2026-10-19): about 3%. An early return measured better instead. -
Skipping the overflow parse of a number with an exponent (2026-12-01): a digit-count check that sends only a possible overflow to
parse::<f64>saved 1.05% of instructions on a file of nothing but exponents and 0.00% on numbers.json and canada, so not worth ten lines. -
Stopping the I-JSON string scan only at lead bytes 0xEF and up (#350, 2026-12-02): CJK 119 to 67 ms, strings.json 45.8 to 40.0 ms and twitter 69 to 63 ms under I-JSON, plain JSON and JSON5 within noise. But past the first lead byte below 0xEF the rest of the string is read byte by byte, and 4 KB strings with one early accent went from 18.3 to 23.1 ms (+16 to +26%) in both orders. Four ways to go back to 16-byte blocks after that byte all fixed the accent case and were slower on plain JSON or JSON5 beyond noise:
- one loop for every dialect: plain strings.json +1.5 to +1.8%, JSON5 +2.3 to +2.4%;
- the loop under
if noncharacters, out of line: plain strings.json +4.1 to +4.5%; - the same, inline: plain strings.json +2.4 to +3.7%, JSON5 +3.5 to +4.3%;
- a parser per invalid-code-point setting, with the loop only in the I-JSON one: rows.json +6 to +7% in JSON and I-JSON, strings.json +3.9 to +4.5% in JSON and JSON5, doc-min.json +2.5 to +4.3%, with 2 to 3% fewer instructions on plain JSON.
A gate on the dialect moves the codegen of the paths it skips.
-
Memory maps, argued: they need
unsafe(0002) and raise SIGBUS on a file truncated while mapped.
Consequences
Kept under the rule: checking files on every core (#62, 1,100 files 14.2 to 4.0 ms) and splitting
large JSON Lines files (#68, 78 MB 264 to 89 ms), and a byte table for strings (#116: 9 MB of
short keys 27.5 to 25.3 ms, files full of escapes or JSON5 tabs 10 to 25% faster, up to 6% on
scripts/bench.py).
A speed-up on one kind of file counts, if it is no slower on the others. Until 2026-11-30 a
speed-up with a cost had to be at least 5% faster end to end. A fixed bar turned down a clear
small gain from a well-kept crate, and said nothing about how heavy the cost was, so the cost is
now weighed on its own.
Confirmation
Speed-up PRs show hyperfine -N numbers; the bench job fails on +1% instructions, and
scaling on a path that turns quadratic.
Revisit if
The parser or compiler changes enough that PGO could differ, or UTF-8 validation or file reading takes a share of a run large enough for a faster version to measure beyond noise.