Skip to content

Compare jiter with serde_json over the json-cases corpus - #266

Merged
samuelcolvin merged 10 commits into
mainfrom
json-cases-corpus-test
Aug 20, 2026
Merged

samuelcolvin merged 10 commits into
mainfrom
json-cases-corpus-test

Conversation

@samuelcolvin

Copy link
Copy Markdown
Member

Adds crates/jiter/tests/json_cases.rs, which parses every document in the json-cases corpus with both JsonValue::parse and serde_json, and requires the two to agree on whether it is valid JSON, on the value it decodes to, and on the error message when they reject it.

The places they legitimately differ are listed in known_difference() with a reason, and nothing else is allowed: jiter's 4300 digit cap on numbers, its recursion limit of 200 against serde_json's 128, and the two errors where the message matches but the position doesn't (serde-rs/json#1083, #130).

A second test covers allow_inf_nan, the one place jiter deliberately parses more than serde_json does: the NaN/Infinity extension must be purely additive, so everything serde_json accepts still decodes to the same value.

The corpus is a separate checkout whose cases/ is a build artifact, so both tests skip themselves when it isn't there; ../json-cases is used by default and JSON_CASES overrides it.

Claude-Session: https://claude.ai/code/session_01KLR7FhfVNh2Qmd8WS7NwHD

samuelcolvin and others added 2 commits August 19, 2026 18:11
Adds `crates/jiter/tests/json_cases.rs`, which parses every document in the
json-cases corpus with both `JsonValue::parse` and `serde_json`, and requires
the two to agree on whether it is valid JSON, on the value it decodes to, and
on the error message when they reject it.

The places they legitimately differ are listed in `known_difference()` with a
reason, and nothing else is allowed: jiter's 4300 digit cap on numbers, its
recursion limit of 200 against serde_json's 128, and the two errors where the
message matches but the position doesn't (serde-rs/json#1083, #130).

A second test covers `allow_inf_nan`, the one place jiter deliberately parses
more than serde_json does: the NaN/Infinity extension must be purely additive,
so everything serde_json accepts still decodes to the same value.

The corpus is a separate checkout whose `cases/` is a build artifact, so both
tests skip themselves when it isn't there; `../json-cases` is used by default
and `JSON_CASES` overrides it.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KLR7FhfVNh2Qmd8WS7NwHD
`cases.json` records, under `similar`, the groups of documents that are one
another's re-spellings — the same document re-indented, or with its strings
escaped differently. `similar_cases_agree` holds jiter to what that promises:
every member of a group decodes to the same value, or, where the document is
malformed, they are all rejected (only the error position may vary). Each group
is checked with `allow_inf_nan` off and on, which is also what makes the
`python-inf-nan` groups compare values rather than agree on rejecting them.

418 groups, 863 documents.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UFzFCpVcjJDsbtgQZNvhFo
@codspeed

codspeed Bot commented Aug 19, 2026 •

Copy link
Copy Markdown

Merging this PR will improve performance by 18.23%

⚡ 2 improved benchmarks
✅ 56 untouched benchmarks
🆕 13 new benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ true_array_jiter_value 15 µs 12.4 µs +21.05%
⚡ true_object_jiter_value 27 µs 23.4 µs +15.47%
🆕 json_cases_all_jiter_value N/A 263.8 ms N/A
🆕 json_cases_arrays_jiter_value N/A 118.3 ms N/A
🆕 json_cases_constants_jiter_value N/A 307.8 µs N/A
🆕 json_cases_deep_jiter_value N/A 54.3 ms N/A
🆕 json_cases_error_jiter_value N/A 33.3 ms N/A
🆕 json_cases_escapes_jiter_value N/A 14.2 ms N/A
🆕 json_cases_floats_jiter_value N/A 36 ms N/A
🆕 json_cases_ints_jiter_value N/A 103.5 ms N/A
🆕 json_cases_non-ascii_jiter_value N/A 9.7 ms N/A
🆕 json_cases_numbers_jiter_value N/A 106.6 ms N/A
🆕 json_cases_objects_jiter_value N/A 153.9 ms N/A
🆕 json_cases_strings_jiter_value N/A 179 ms N/A
🆕 json_cases_whitespace_jiter_value N/A 68.6 ms N/A

Tip

Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.


Comparing json-cases-corpus-test (22de423) with main (67d11d5)

Open in CodSpeed

@codecov

codecov Bot commented Aug 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

`json_cases.rs` in benches/ reads every case into memory and parses the lot in
one iteration, swallowing the errors: a mixed workload of ~2800 documents, most
of them small and over half malformed, where every benchmark in main.rs is a
single well formed document of one shape. It is the only one that measures the
error paths.

The corpus is timed as a whole and then once per `tags` entry of cases.json,
which is what says roughly where a change landed — the string scanner, number
decoding, the recursion, the whitespace loop or the failure path. Category would
have been the wrong axis: `deep-complete/` holds the deep documents and their
`--indented` twins, and timing those measures whitespace.

The index loader moves to tests/corpus/mod.rs, shared by the test and the
benchmark. The tests now require the corpus rather than skipping when it is
missing, so a checkout without it can't quietly pass the suite; JSON_CASES_SKIP
opts out, and CI builds the corpus for `test` and skips it for
`test-latest-deps`, which is about resolving dependencies fresh. The benchmark
still registers nothing when the corpus is absent, so `cargo bench` and the
CodSpeed job are unaffected.

The clone URL in the docs pointed at pydantic/json-cases, which 404s; the repo
is public at samuelcolvin/json-cases.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UFzFCpVcjJDsbtgQZNvhFo
Comment thread .github/workflows/ci.yml Resolved
samuelcolvin and others added 4 commits August 20, 2026 06:14
Tracking the default branch means a corpus commit can turn this repository's CI
red without a change here; the pin makes the corpus move a deliberate bump.
fa6c17f is the tip of main today, the commit that added `size` and `tags`.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UFzFCpVcjJDsbtgQZNvhFo
CI checks the corpus out at $GITHUB_WORKSPACE/json-cases, which puts its own
crate inside this workspace; cargo then refuses to build it.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UFzFCpVcjJDsbtgQZNvhFo
The bench job now checks out and builds json-cases too, so the json_cases
benchmarks register there instead of being silently absent. The pin moves to a
workflow level JSON_CASES_REF so the bench job and the test job can't drift onto
different revisions of the corpus — which would make the benchmark numbers
incomparable as well as the tests.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UFzFCpVcjJDsbtgQZNvhFo
They were far longer than what they introduce; the commands they spelled out are
in benches/README.md and in the message the tests panic with.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UFzFCpVcjJDsbtgQZNvhFo
@samuelcolvin
samuelcolvin force-pushed the json-cases-corpus-test branch from 3ca76ae to 986e44c Compare August 20, 2026 04:26
They are the local comparison baseline; CodSpeed tracks jiter's own history, and
serde_json is pinned in Cargo.lock, so instrumenting it buys a line that doesn't
move and doubles the run. `cfg(codspeed)`, which cargo-codspeed sets, compiles
them out the way monty leaves its CPython benchmarks out of CI.

Registered under codspeed: main.rs 44 benchmarks instead of 60, json_cases 13
instead of 26. A plain `cargo bench` still runs both halves.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UFzFCpVcjJDsbtgQZNvhFo
@samuelcolvin

Copy link
Copy Markdown
Member Author

@codspeedbot explain why the benchmarks keep jumping around by as much as 17%!

@codspeed

codspeed Bot commented Aug 20, 2026 •

Copy link
Copy Markdown

@codspeedbot explain why the benchmarks keep jumping around by as much as 17%!

Dug into the runs. The instrument isn't noisy — two separate main runs with the same binary on the same runner CPU gave 14.9506 µs and 14.9478 µs for true_array_jiter_value, a 0.02% difference. So the swings are real signal about something outside your diff. There are two separate things going on, and the one behind the 17% is not the one CodSpeed's banner is pointing at.

1. The 17% and 10% come from commit 5f0b0bf, not from the runner

5f0b0bf compiles the serde benchmarks out of main.rs under cfg(codspeed). That changes the bench binary, and comparing the two runs on either side of it — both on the same CPU, no environment difference reported at all — reproduces exactly the report's numbers:

Benchmark 3ca76ae 5f0b0bf Change
true_array_jiter_value 14.9 µs 12.7 µs +16.8%
short_numbers_jiter_value 155.4 µs 140.6 µs +10.47%
pass2_jiter_value 15.6 µs 17.8 µs −12.25%

The flamegraphs say where it went. true_array_jiter_value grows a Vec<JsonValue>, so RawVec::grow_one → finish_grow → libc realloc:

  • before: realloc subtree = 6.2 µs of 14.9 µs (41.6%)
  • after: realloc subtree = 4.0 µs of 12.7 µs (31.5%)

That 2.2 µs drop is the entire 2.1 µs "improvement". jiter's own frames barely move. Criterion runs every benchmark in one process, so dropping 16 serde benchmarks from main.rs changes the heap history before each jiter benchmark, and realloc then takes a different path (extend in place vs. copy, pages already faulted). Same parser, different allocator luck.

This is worth knowing but not worth reverting: it's a one-time re-baseline. Once this merges, main measures the new binary and comparisons are clean again. Just don't read the current +17% as jiter getting faster.

2. The background ±10% is the runner CPU

ubuntu-latest alternates between an AMD EPYC 7763 and an AMD EPYC 9V74, and Simulation derives its cache model from the physical CPU. Sharpest illustration in this PR: 986e44c only trims module comments in three bench/test files — no src/, no main.rs, no Cargo.lock — and its report still showed pass2_jiter_value regressing 10.13%, purely because the base ran on the 7763 and the head on the 9V74. As a control, true_array_serde_value (unchanged code, pinned dependency) sits at 23.516 / 23.441 / 23.457 µs across three main runs on the same CPU, then drops to 21.350 µs on the other one.

Dependency drift, the third cause we hit on #264, is gone now that Cargo.lock is committed.

What would actually fix this

  • The CPU flip: Macro Runners are dedicated bare-metal machines, so every run lands on the same hardware. That removes the ±10% floor entirely and is the only real fix for it.
  • The allocator coupling: CODSPEED_EXCLUDE_ALLOCATIONS: "true" on the CodSpeed action subtracts allocator frames from the reported value (docs). It would have flattened all three of the moves above. The tradeoff matters for jiter specifically though: a chunk of your real optimisation work is avoiding allocations, and that work would stop showing up in the numbers. Worth a deliberate decision rather than a default.

I haven't changed anything in the PR — say the word and I'll wire up either of those.

Summary
  • Warming up the performance wizard...
  • Investigating benchmark variance — Identified two causes: alternating runner CPU on ubuntu-latest, and the bench binary change in 5f0b0bf shifting libc realloc cost
  • Handling comment — Comment handling finished successfully

Learnings updated. See what CodSpeed remembers about this repository.

Was this response helpful? React with 👍 or 👎 to help us improve CodSpeedBot.

@samuelcolvin
samuelcolvin merged commit 0fc00a4 into main Aug 20, 2026
29 checks passed
@samuelcolvin
samuelcolvin deleted the json-cases-corpus-test branch August 20, 2026 04:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant