Skip to content
jsonvet

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 for wasm32-unknown-unknown.
  • A release build that gets noticeably slower needs a gain users notice.
  • No unsafe in 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 unsafe the checked bytes cannot become a String, so each &str borrow validates the text again.
  • PGO (2026-10-19, fat LTO both ways): within σ on every scripts/bench.py case 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.
  • 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.