Skip to content

LPC845 bloat: JSON tokenizer/builder/ieee754 path is 5.4 KB (JsonBuilder::on_token 2.3K + JsonTokenizer::parse 1.35K + kPow10Mant LUT 824) #3082

Description

@zackees

Symptom

The JSON tokenizer + builder + ieee754 number formatter pipeline accounts for ~5.4 KB of .text on the LPC845 LowMemory + FASTLED_LPC_RX_SCT_WS2812=1 build. Real arm-none-eabi-nm --print-size figures:

Symbol Bytes
fl::(anonymous namespace)::JsonBuilder::on_token 2,340
fl::(anonymous namespace)::JsonTokenizer::parse 1,350
fl::(anonymous namespace)::JsonTokenizer::parse (specialization) 1,350 (overlap — same fn or constprop clone)
fl::ieee754_format_decimal 594
fl::json_value::parse2 522
fl::ieee754_parse_decimal 482
fl::(anonymous namespace)::kPow10Mant LUT 824 (rodata)
Total active .text + rodata ~5,400 bytes (~7% of .text)

JsonBuilder::on_token at 2,340 bytes is the 4th-largest single function in the entire binary. It's the SAX-style visitor that converts the tokenizer's token stream into the fl::json_value AST.

Hypothesis

JsonBuilder::on_token is a switch over ~10 token types, each branch doing a different fl::json_value construction (string, number, array-open, object-open, etc.). With current inlining the switch unrolls into a long sequence of variant<...>::emplace<T> instantiations, each pulling in the variant's per-alternative copy_fn / destroy_fn. The 2.3 KB single-function size suggests heavy template duplication of the variant write paths.

JsonTokenizer::parse at 1.35 KB is a state-machine over characters. That's defensible — a well-tested JSON tokenizer of that size is not unreasonable — but the 824-byte kPow10Mant LUT only makes sense if ieee754_parse_decimal is reachable (it is, for parsing JSON numbers). If LowMemory + RX-only builds never see a float JSON number, the LUT + parse routine should drop.

Possible directions

  1. Outline the variant-emplace paths in JsonBuilder::on_token. The branch bodies are small per se; the multiplier is the per-alternative codegen tax inside variant<>::emplace. Replace with a single void emit_value(json_value& dst, TokenType tag, span<char> raw) non-template function that dispatches internally. Estimated save: 1-1.5 KB.
  2. Gate ieee754_* behind a "JSON has floats" build flag. The LPC8xx RPC contract is integer-only (echo: int → int, pinToggleRx: 4× int, ws2812SctTest: 4× int). If we tell the codec at compile time that no float values are expected, kPow10Mant (824 B), ieee754_format_decimal (594 B), and ieee754_parse_decimal (482 B) all drop. Saves ~1.9 KB.
  3. Hand-write a smaller tokenizer for the LowMemory codec. The full RFC-compliant tokenizer is overkill for a 3-method RPC surface. A 200-300 byte hand-coded parser that accepts only {"jsonrpc":"2.0","id":N,"method":"foo","params":[1,2,3,4]} would beat the 1,350-byte general tokenizer by 1 KB.

Note on safety

This is the JSON path used by fl::Remote for RPC dispatch. Any change must keep the parser strictly safe against malformed input — buffer overruns from a malformed RPC request would be a remote-code-execution class bug. Tests must cover fuzz-shaped inputs.

Repro

Same as meta #3079.

Acceptance

  • fl::JsonBuilder::on_token drops below 1 KB on LPC845 LowMemory build
  • fl::ieee754_format_decimal + fl::ieee754_parse_decimal + kPow10Mant LUT either all drop entirely (if the int-only-codec flag is implemented) or stay sized as today (if not)
  • Total JSON-codec .text mass on LPC845 LowMemory + RX_SCT_WS2812 build drops below 2 KB
  • Fuzz test added that exercises malformed RPC input against the new parser path

Related

Activity

  1. zackees commented on Jun 15, 2026

    @zackees
    MemberAuthor

    Tracked under meta #3079 alongside #3076 (soft-FP), #3077 (fl::function variant), #3081 (RPC dispatch). Depends on FastLED/fbuild#594 for proper bloat iteration.

  2. zackees commented on Jun 15, 2026

    @zackees
    MemberAuthor

    Investigation findings (2026-06-15)

    Active call graph from nm output

    The reachable JSON codec on the LPC845 LowMemory + RX_SCT_WS2812 build is:

    fl::Rpc::handle → fl::Remote::processRpc → fl::JsonBuilder::on_token  ↑ build-side
                                             → fl::JsonTokenizer::parse   ↑ parse-side
                                             → fl::SerializerVisitor      ↑ serialize-side
                                             → fl::json_value::parse2
    

    fl::SerializerVisitor is what couples the JSON codec to the soft-FP cascade (#3076) — for the float alternative of json_value::variant_t it calls fl::ieee754_format_decimal which links the entire float path.

    Specific symbols and call graph

    2,340  fl::JsonBuilder::on_token         <- the SAX visitor that builds json_value AST
    1,350  fl::JsonTokenizer::parse          <- character state machine
      594  fl::ieee754_format_decimal        <- float -> decimal string
      824  fl::kPow10Mant LUT                <- pow-of-10 table for ieee754_format_decimal
      522  fl::json_value::parse2
      482  fl::ieee754_parse_decimal         <- decimal string -> float
    

    Easy wins (and a hard one)

    Easy:

    1. Both ieee754_format_decimal and ieee754_parse_decimal are only reachable through the float alternative of json_value::variant_t. If LPC845 bloat: 8.3 KB of soft-FP / integer-div leaks past the no-soft-FP invariant on LowMemory + RX_SCT_WS2812 #3076 lands path support TLS3001 #1 (drop the float alternative on Low-memory), these and the kPow10Mant LUT drop together — ~1.9 KB free with no JSON codec changes needed.
    2. JsonTokenizer::parse at 1.35 KB is small enough to consider hand-replacing on Low-memory builds. A minimal RFC-7159 subset tokenizer that accepts only {"id":N,"method":"foo","params":[1,2,3,4]} would fit in ~300 bytes.

    Hard:
    3. JsonBuilder::on_token at 2,340 bytes — this is the one that needs the SAX visitor outlining or a hand-rewritten minimal builder. The branch bodies are inlined variant<>::emplace<T> paths, each instantiating the variant's per-alternative copy_fn/destroy_fn. Refactoring touches the variant API.

    Dependency on #3076

    If #3076 is fixed via "drop the float alternative" (path #1 in that issue), this issue's ieee754_format_decimal + ieee754_parse_decimal + kPow10Mant LUT problem goes away automatically. The remaining bloat would just be JsonBuilder::on_token at 2.3 KB and JsonTokenizer::parse at 1.35 KB.

    If #3076 is fixed differently, this issue's full scope (~5.4 KB) remains.

    Recommendation

    Work #3076 first. Re-measure this issue after. The target may shrink from ~3 KB to ~1 KB worth of remaining fixable scope.

  3. added 2 commits that reference this issue on Jun 15, 2026
    8e850f0
    12b06fd
  4. moved this from Triage to Done in FastLED Trackeron Jun 15, 2026
  5. added 4 commits that reference this issue on Jun 15, 2026
    81bcaf3
    1b1b403
    e0a75e7
    51323eb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions