Skip to content

LPC845 bloat: RPC dispatch surface is 12.6 KB (Rpc::handle 3.1K + Remote::processRpc 3.0K + TypedRpcBinding<sig>::invokeWithReturn 2.2K/sig) #3081

Closed

Description

@zackees

Symptom

On LPC845 LowMemory + FASTLED_LPC_RX_SCT_WS2812=1 (the build that overflows 64 KB FLASH by 11 KB per meta #3079), the JSON-RPC dispatch surface is the single largest .text cluster. Real arm-none-eabi-nm --print-size figures:

Symbol Bytes
fl::Rpc::handle(fl::json const&) 3,124
fl::Remote::processRpc(fl::json const&) 2,996
fl::TypedRpcBinding<fl::string(int,int,int,int)>::invokeWithReturn 2,196
fl::detail::TypedSchemaGenerator<fl::string(int,int,int,int)>::params 856
fl::TypedRpcBinding<int(int)>::invokeWithReturn 610
fl::JsonArgConverter<int(int)>::convertArg<0u> 512
fl::JsonArgConverter<fl::string(int,int,int,int)>::convertArg<0u> 512
Plus three unordered_map<string, RpcEntry>::rehash_inline_no_resize instantiations ~1,800
Total ~12,600 bytes (~17% of .text)

A single 4-int-argument RPC method (pinToggleRx) lifts a TypedRpcBinding<string(int,int,int,int)>::invokeWithReturn of 2,196 bytes, plus a sibling 856-byte TypedSchemaGenerator<...>::params template. The pattern multiplies per registered method signature; on LPC8xx where only 3-4 methods are registered, this is ~12 KB of dispatch overhead vs ~3 KB of actual method-body code.

Hypothesis

fl::Rpc::handle and fl::Remote::processRpc look like they over-inline the JSON-walking + method-lookup + error-formatting + response-emit pipeline into a single monolithic function. On a 64 KB target with no I-cache, that monolith is paid in flash even when each per-method instantiation duplicates similar bytes.

TypedRpcBinding<Sig>::invokeWithReturn is a function template: each unique Sig registered emits a fresh ~600-2,200 byte clone. On targets like ESP32 with 4 MB flash this is irrelevant; on LPC845 with 64 KB it is a budget breaker.

Possible directions

  1. De-template the binding glue. Move the type-erased dispatch path through a single non-template function that takes a (void* binding, json const& args, ResponseSend& resp) -> void thunk, with the per-signature glue reduced to a tiny ~50-byte trampoline that argument-extracts then calls the actual user function. Saves ~80% of TypedRpcBinding::invokeWithReturn per signature.
  2. Split fl::Rpc::handle and fl::Remote::processRpc into outline + slow paths. The hot path (method-name lookup + dispatch) stays inline; error formatting, schema-help, and async-ack handling outline into separate translation units that get DCE'd when not reachable.
  3. Merge the three unordered_map<string, ...>::rehash_inline_no_resize instantiations — they differ only in the value type (RpcEntry vs AsyncRequest vs shared_ptr<StringHolder>). A single non-template rehash that takes a per-entry-stride parameter would save ~1.2 KB.
  4. Schema generation behind a build flag. TypedSchemaGenerator<...>::params is 856 bytes per signature; it only matters for runtime introspection (the __rpc_schema discovery method). Gate it behind FASTLED_RPC_SCHEMA=1 so LowMemory builds drop it entirely.

Repro

Same as meta #3079.

arm-none-eabi-nm --print-size --size-sort --demangle firmware.elf | \
  awk '$3=="t" || $3=="T"' | tail -25

fl::Rpc::handle and fl::Remote::processRpc are the top two .text symbols today.

Acceptance

  • fl::Rpc::handle drops below 1.5 KB
  • fl::Remote::processRpc drops below 1.5 KB
  • Per-signature TypedRpcBinding<Sig>::invokeWithReturn drops below 500 bytes for a 4-int signature
  • Total RPC-dispatch .text mass on LPC845 LowMemory + RX_SCT_WS2812 build drops below 5 KB (from ~12.6 KB today)
  • No regression in RPC dispatch semantics or async-ack behavior on ESP32 / Teensy

Related

Activity

  1. zackees commented on Jun 15, 2026

    @zackees
    MemberAuthor

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

  2. zackees commented on Jun 15, 2026

    @zackees
    MemberAuthor

    Investigation findings (2026-06-15)

    Source survey

    TypedRpcBinding<R(Args...)>::invokeWithReturn is defined as a class-template member function in src/fl/remote/rpc/typed_rpc_binding.h:75. For each registered RPC method signature, the compiler emits a fresh instantiation containing inlined:

    1. Converter::convert(jsonArgs) — JsonArgConverter<R(Args...)> template that walks the json args and tuples up converted C++ values.
    2. fl::make_tuple(...) + fl::tuple<...> constructors for the C++ value tuple.
    3. detail::TypeToJson<R>::convert(returnValue) — converts the C++ return value back to json.
    4. The actual mFunction(...) call through fl::function<R(Args...)>'s variant.

    For the fl::string(int,int,int,int) signature (used by pinToggleRx), the resulting invokeWithReturn is 2,196 bytes. The 4-int variant is 0.6 KB; the string-return + tuple machinery dominates.

    fl::Rpc::handle(fl::json const&) at 3,124 bytes is the table-lookup + dispatch + error-formatting + response-emit pipeline rolled into a single function. fl::Remote::processRpc(fl::json const&) at 2,996 bytes is the JSON-RPC envelope handler (id/jsonrpc-version validation + async vs sync dispatch + ACK emit).

    Why this is hard

    TypedRpcBinding is the central abstraction. Every registered RPC method routes through it. De-templating it would mean:

    • A type-erased void invoke_thunk(void* binding_state, const json& args, ResponseSend& resp) that gets stored alongside method names in the dispatch table
    • Per-signature glue reduced to a ~50-byte function that knows how to extract args from the json and call the user function

    The risk: the dispatch table currently relies on compile-time type information to safely route a json to the right fl::function<Sig>. Type-erasing it means re-introducing the runtime type-check that the templates were designed to avoid. It's a real engineering trade-off, not a clear win.

    Scope honesty

    This is genuine API-surface design work. It cannot be done as a single drive-by PR. Estimated scope: 2-3 weeks of careful design + implementation + cross-platform CI verification (the RPC path is used on ESP32 + Teensy + LPC + WASM hosts).

    Marking as scoped for design discussion before PR work. Recommend deferring until #3076 lands first — if dropping the float variant alternative saves the 8 KB soft-FP cascade, the LPC845 LowMemory build may fit without touching the RPC dispatch at all, and this issue becomes "nice to have" rather than blocker.

  3. moved this from Triage to Done in FastLED Trackeron Jun 15, 2026
  4. added a commit that references this issue on Jun 15, 2026
    81bcaf3
  5. added a commit that references this issue on Jun 15, 2026
    e18aa2b
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