Repository navigation
Add a native JSON writer for IValue (byte-identical to serde_json) - #34
Conversation
`ijson::{to_writer, to_vec, to_string, write_json}` write an `IValue` as JSON text
by walking it directly instead of going through `Serialize`. The output is
byte-identical to serde_json's: every token is written through the same
`serde_json::ser::Formatter` call, in the same order, so numbers still go through
ryu/itoa and a custom formatter (indent, newline, space) sees the same calls.
- Strings are scanned for bytes to escape eight bytes at a time; a clean run is
written with one `write_string_fragment` call. Escapes use serde_json's table.
- Typed (homogeneous numeric) arrays are written from their slices.
- No per-value memory change: the writer only reads the value.
Tests: a seeded differential test against serde_json (both builds), an
every-byte escape test, and a `fuzz_json_writer` target. Benchmarks:
`serialise/native-compact` and `serialise/native-indent` rows next to the
existing serde_json rows in `benches/callpaths.rs`.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
AvivDavid23
left a comment
There was a problem hiding this comment.
Nice! one small comment, and two extras:
1:
The PR promises that every Formatter call matches serde_json's, but the diff tests and the fuzzer only compare output bytes with CompactFormatter/PrettyFormatter. A write_i64 vs write_u64 mix-up, or a different split of write_string_fragment calls, would produce the same bytes and pass. Could we add a small recording Formatter that logs (method, args) and assert the native and serde logs are equal, reusing it in fuzz_json_writer?
2:
The integer typed-array arms (I8..U64) don't seem to be tested directly. random_value almost never produces I16/U16/U32 storage, the unit tests have no U16 ([40000]), U32 ([3000000000]) or I8 ([-1, 2]) cases, and the ints literal mixes i64::MIN with u64::MAX, so I think it may be stored as a heterogeneous array — is that right? Could we add an explicit differential check over IArray::from(Vec<iN/uN>) for each width, including the edges (u64::MAX, i64::MAX as u64 + 1, i64::MIN)?
|
Addressed all three requests in
Independent CI ran the pristine unit and integration suites in both feature modes with serde_json 1.0.146 and 1.0.154. The writer fuzzer also completed 20,000 executions per mode with 1.0.146. Upstream build, bench compilation and all six fuzz jobs passed. The first memory gate observed 1,280 → 1,368 bytes on three |
What
A writer that serialises an
IValueto JSON text by walking it directly, instead of going throughSerialize:The output is byte-identical to
serde_json::to_writer/Serializer::with_formatteron the same value. Every token goes through the sameserde_json::ser::Formattermethod serde_json would call, in the same order and with the same arguments, so:f32/f16/bf16typed-array elements inser.rs;RedisJsonFormatterforINDENT/NEWLINE/SPACE.What changes is how the work is done:
",\, bytes below 0x20) eight bytes at a time. A clean run is written with onewrite_string_fragmentcall, and escapes are mapped through serde_json's own escape table.IValuebuilt per element.Serialize/Serializerindirection per value.Memory: nothing changes per value or per document; the writer only reads.
Why
Whole-document
JSON.GET($, or legacy GET with no path) serialises the full document. On RedisJSON 8.12.0,serde_json::ser::format_escaped_str, which scans one byte at a time, is about 35 % of server CPU samples forJSON.GET key $on a 38 KB document.Benchmarks
benches/callpaths.rsgainsserialise/native-compactandserialise/native-indentrows next to the existing serde_jsoncompact/indentrows. The two rows for a document produce the same bytes.cargo bench --bench callpaths --no-default-features -- serialise/, AWS Graviton4 (Neoverse-V2), pinned to one core, rustc 1.92, one run, criterion median, µs:A separate harness ran 3 interleaved rounds over 52 serialise cases on the same CPU, and every case was faster. The worst case was a synthetic string dense with escapes, at −6.3 %.
RedisJSON server (for context; it needs the follow-up call-site change below). RedisJSON 8.12.0 at this ijson base, with vs without the writer on the
JSON.GETpath. Server CPU per request, one pinned core, median of 3 interleaved runs:Server replies were byte-identical across 120 commands, and
used_memorywas unchanged.Other operations (guard). The writer touches no other code path, so I also A/B'd the parse, lookup, mutate, typed-array, clone/drop and CBOR benchmark groups, with the thread-safe string cache that RedisJSON uses (2 rounds, same CPU).
ijsonrows drifted less than the serde_json control rows, whose code is identical in both builds: 29 vs 52 rows moved by more than 3 %, with medians of +0.04 % and −0.08 %.typed sumi8/i16,typed parsei32/i64) and onelookupiterate row +20 %. Their code is unchanged, so I read this as code-layout noise, but that is not proven; they are worth a re-run if anyone sees a change there.Correctness
tests/json_writer_diff.rs:serde_jsonon random documents, covering objects, arrays, every number kind, typed arrays and escape-heavy strings, in compact and pretty modes;f16,bf16,f32,f64) against serde_json.fuzz/fuzz_targets/fuzz_json_writer.rs: fuzzes the writer against serde_json, and is added to the CI fuzz matrix.cargo test -- --test-threads=1passes in both the default andthread_safebuilds, as in CI.--test-threads=1in the default build.Not in this PR
ijson::write_jsonfrom theJSON.GETserialise path is a separate RedisJSON change, made after this lands and the pin is bumped.🤖 Generated with Claude Code
Note
Medium Risk
New serialization path must match serde_json on all value shapes (numbers, escapes, typed arrays); extensive differential/fuzz coverage mitigates but regressions would affect JSON.GET-style outputs once wired.
Overview
Adds a native JSON writer (
ijson::to_vec,to_writer,to_string,write_json) that walksIValuedirectly instead ofSerialize, while driving the sameserde_json::ser::Formattercalls so output stays byte-identical toserde_json. Optimizations include 8-byte SIMD-style escape scanning, typed-array slice serialization without per-elementIValues, and reuse offind_shortest_roundtrip_f64(nowpub(crate)) for f16/bf16 arrays.Testing & CI: differential integration tests (
tests/json_writer_diff.rs), a sharedrecording_formatterthat compares formatter call sequences, unit tests injson_writer.rs, a newfuzz_json_writertarget in the fuzz matrix, and serialise benchmark rowsnative-compact/native-indentbeside the existing serde_json paths.Reviewed by Cursor Bugbot for commit fde4ec8. Bugbot is set up for automated code reviews on this repo. Configure here.