Repository navigation
feat(stl): JsonStreamWriter — stream JSON without an N-sized buffer (#3588 follow-up) - #3604
Merged
Merged
Conversation
…3588 follow-up) The #3588 root-cause fix (make_shared null-construction under OOM) stops the heap CORRUPTION, but building a large JSON-RPC response as an fl::json object and then serializing it to an fl::string still allocates two buffers proportional to the whole response. On classic ESP32 with WiFi up (free heap ~30 KB) a `help` listing of ~200 methods, or a big runSingleTest result, can still exhaust the heap during construction. JsonStreamWriter emits a JSON document incrementally to a byte sink (serial, socket, …) through a fixed 128-byte scratch buffer and a fixed-depth nesting stack — it performs NO heap allocation of its own, so an arbitrarily large document is produced with a bounded, tiny footprint (peak memory = one small buffer, not the document). It handles commas, object key/value alternation, JSON string escaping, and numbers/bools/null. fl::JsonStreamWriter w(sink); w.beginObject(); w.key("result"); w.beginArray(); for (m : methods) { w.beginObject(); w.member("name", m.name); ... w.endObject(); } w.endArray(); w.endObject(); // dtor flushes Host-tested (escaping, nesting, null, and a 1000-element array verified to stream through the fixed buffer with no single write exceeding kBufSize). Integration path (separate PR): wire the RPC transport to expose a raw byte sink so handlers like `help` can stream their response directly instead of returning a fully-built fl::json — the writer is the mechanism; the RPC-protocol plumbing is a focused follow-up that needs on-device protocol verification. Co-Authored-By: Claude Fable 5 <[email protected]>
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (4)
📝 WalkthroughWalkthroughAdds a new ChangesJSON Stream Writer
Estimated code review effort: 2 (Simple) | ~15 minutes Sequence Diagram(s)sequenceDiagram
participant Caller
participant JsonStreamWriter
participant ScratchBuffer
participant Sink
Caller->>JsonStreamWriter: beginObject()
JsonStreamWriter->>ScratchBuffer: putc('{')
Caller->>JsonStreamWriter: key("name")
JsonStreamWriter->>ScratchBuffer: putEscaped("name") + ':'
Caller->>JsonStreamWriter: value("Alice")
JsonStreamWriter->>ScratchBuffer: putEscaped("Alice")
Caller->>JsonStreamWriter: value(42)
JsonStreamWriter->>ScratchBuffer: itoa64 digits
ScratchBuffer->>ScratchBuffer: buffer full?
ScratchBuffer->>Sink: flush() writes buffered bytes
Caller->>JsonStreamWriter: endObject()
JsonStreamWriter->>ScratchBuffer: putc('}')
Caller->>JsonStreamWriter: (destructor)
JsonStreamWriter->>Sink: final flush()
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
This was referenced Jul 5, 2026
eelstretching
pushed a commit
to eelstretching/FastLED-pico
that referenced
this pull request
Jul 11, 2026
…astLED#3588 follow-up) (FastLED#3604) The FastLED#3588 root-cause fix (make_shared null-construction under OOM) stops the heap CORRUPTION, but building a large JSON-RPC response as an fl::json object and then serializing it to an fl::string still allocates two buffers proportional to the whole response. On classic ESP32 with WiFi up (free heap ~30 KB) a `help` listing of ~200 methods, or a big runSingleTest result, can still exhaust the heap during construction. JsonStreamWriter emits a JSON document incrementally to a byte sink (serial, socket, …) through a fixed 128-byte scratch buffer and a fixed-depth nesting stack — it performs NO heap allocation of its own, so an arbitrarily large document is produced with a bounded, tiny footprint (peak memory = one small buffer, not the document). It handles commas, object key/value alternation, JSON string escaping, and numbers/bools/null. fl::JsonStreamWriter w(sink); w.beginObject(); w.key("result"); w.beginArray(); for (m : methods) { w.beginObject(); w.member("name", m.name); ... w.endObject(); } w.endArray(); w.endObject(); // dtor flushes Host-tested (escaping, nesting, null, and a 1000-element array verified to stream through the fixed buffer with no single write exceeding kBufSize). Integration path (separate PR): wire the RPC transport to expose a raw byte sink so handlers like `help` can stream their response directly instead of returning a fully-built fl::json — the writer is the mechanism; the RPC-protocol plumbing is a focused follow-up that needs on-device protocol verification. Co-authored-by: Claude Fable 5 <[email protected]>
This was referenced Jul 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The #3588 root-cause fix (#3603, make_shared null-construction under OOM) stops the heap corruption, but building a large JSON-RPC response as an
fl::jsonobject and serializing it to anfl::stringstill allocates two buffers proportional to the whole response. Ahelplisting of ~200 methods (or a bigrunSingleTestresult) can still exhaust the heap on classic ESP32 with WiFi up.JsonStreamWriteremits a JSON document incrementally to a byte sink through a fixed 128-byte scratch buffer and a fixed-depth nesting stack — no heap allocation of its own. An arbitrarily large document streams with a bounded, tiny footprint (peak memory = one small buffer, not the document). Handles commas, key/value alternation, string escaping, numbers/bools/null.Host-tested: escaping, nesting, null, and a 1000-element array verified to stream through the fixed buffer with no single write exceeding
kBufSize. Host 349/349, lint clean, esp32dev builds.Integration path (follow-up): wire the RPC transport to expose a raw byte sink so
help/large handlers stream directly instead of returning a fullfl::json. This primitive is the mechanism; the RPC-protocol plumbing needs on-device verification and lands separately.Refs #3588.
🤖 Generated with Claude Code
Summary by CodeRabbit
New Features
Tests