Skip to content

Add record-level JSON streaming (StreamReader and StreamWriter) - #68

Closed
clockblocker wants to merge 4 commits into
mainfrom
json-streaming
Closed

clockblocker wants to merge 4 commits into
mainfrom
json-streaming

Conversation

@clockblocker

@clockblocker clockblocker commented Nov 27, 2026 •

Copy link
Copy Markdown
Owner

Summary

Adds opt-in record-level JSON streaming (<rfl/json/stream.hpp> + compiled engine in src/rfl/json/stream.cpp) with rfl::json::StreamReader<T, Ps...> and rfl::json::StreamWriter<T, Ps...> for NDJSON / JSON Lines, RFC 8142 JSON Text Sequences (\x1e), and top-level JSON arrays, without buffering an entire document in memory.

Reconciled Review Fixes (Round 1 B1–B8 + Final Sign-Off Blockers)

  • True O(R) streaming + zero stale-pointer UB (f7f5d1ef):
    • Replaced cached pointers across refills with relative offsets (scan_offset) so compaction at EOF can never invalidate pointers (verified under ASAN + UBSAN).
    • Implemented an incremental JSON value boundary scanner (StreamReaderEngine::scan_value) tracking strings/escapes, comments, container depth, \x1e boundaries, and NDJSON newlines so yyjson_read_opts runs once per complete record — O(R) even on 4 KiB / 1-byte unbuffered streams and zero 4 KiB copy overhead in json_array mode.
    • Fixed exact max_record_bytes + \r\n lookahead edge case.
  • Engine extraction & ergonomics:
    • Extracted non-template StreamReaderEngine (RFL_API) into src/rfl/json/stream.cpp (wired via src/reflectcpp_json.cpp) to keep template instantiations tiny.
    • Dropped unused write.hpp include; StreamWriter::write() checks fail() || bad() (clearing stale eofbit) and preserves sticky finish() errors; updated docs/content/supported_formats/json.md & README.md.

Real-World Guinea-Pig Proof

  • cppcheck (compile_commands.json, 50,000 entries / 31 MB, -O2 median): StreamReader (json_array mode) vs picojson DOM — 2.06× faster end-to-end (1,375 ms -> 666 ms) & 2.48× lower peak RSS (130.8 -> 52.7 MiB); parse-only 5.86× faster (777 -> 133 ms) & 17.4× lower peak RSS (83.5 -> 4.8 MiB, O(1) memory).
  • osmium-tool (120,000 GeoJSON/GeoJSONSeq features, SHA-256 identical): StreamWriter (json_seq / ndjson) + rfl::json::Raw — 1.40×–1.70× faster (307 -> 220 ms, 490 -> 288 ms with attributes).

Verification

  • ASAN + UBSAN (-fsanitize=address,undefined): test_stream 17/17 PASSED (0.60s, 0 findings).
  • Full JSON suite: 334/334 PASSED (3.07s).

@clockblocker

Copy link
Copy Markdown
Owner Author

Signed-off branch is preserved on fork and indexed in #116 (Easy & Landable — Parked for After Main Effort) so the open PR list tracks only the active 18-PR queue + live upstream PRs.

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