Skip to content

LPC845 bloat: fl::function variant emits 6.3 KB of copy/move/destroy clones across RPC dispatch signatures #3077

Closed

Description

@zackees

2026-06-15 revision: original numbers in this issue came from a flawed map-parser methodology (see retraction comment on #3075 / #3078). Issue body now reflects real arm-none-eabi-nm --print-size data on the ELF. Substance is confirmed but smaller than originally reported. The fix is still worth doing — 6.3 KB is ~57% of the 11 KB overage.

Symptom

The variant-based callable inside fl::function<Sig> instantiates a per-alternative copy/move/destroy clone for each of its 5 alternatives (shared_ptr<CallableBase>, FreeFunctionCallable, InlinedLambda, NonConstMemberCallable, ConstMemberCallable). On LPC845 LowMemory + FASTLED_LPC_RX_SCT_WS2812=1, the total fl::function-related symbol mass measured by arm-none-eabi-nm --print-size is 6,260 bytes (~8.2% of .text).

Single largest individual contributor: fl::function<void(fl::json const&)>::InlinedLambda::invoke_lambda<...> instantiations, the biggest at 0x172 = 370 bytes each. Across the JSON-RPC dispatch table (at least two fl::function signatures: void(), void(json const&), plus void(ResponseSend&, json const&)), the per-alternative copy/move/destroy _fn<T> clones accumulate.

Why this is surprising

fl::function is supposed to be an SBO-friendly type-erased callable. The variant approach gives strict-aliasing-safe storage but pays per-alternative codegen taxes. On a Cortex-M0+ with no fast indirect dispatch, 6 KB for callable storage across the RPC dispatch table is the wrong size class.

Possible directions

  1. Collapse alternatives on Low-memory targets. On LPC8xx the AutoResearch RPC dispatch only ever stores function pointers (FreeFunctionCallable) and stateful inlined lambdas (InlinedLambda). The two MemberCallable variants and the shared_ptr heap fallback are dead code on this target. A #if !FL_PLATFORM_HAS_LARGE_MEMORY gate that drops the variant to a 2-element {free_fn, inlined_lambda} would save ~40% of the per-signature cost.
  2. Move copy/move/destroy through a single dispatch vtable instead of variant per-alternative _fn<T> instantiations. The current pattern emits one fn per (operation x alternative) — at least 15 small functions per signature when only 1 is reached at runtime.
  3. Erase the lambda-id template parameter from destroy_lambda<Lambda> — each unique lambda lifts a new destroy_lambda instantiation. Combined with the 3-4 lambdas Remote constructs, this is a measurable code-size tax.

Repro

Same as parent meta #3079. After link, use the canonical nm command:

arm-none-eabi-nm --print-size --demangle firmware.elf | grep 'fl::function' | \
  awk '{s += strtonum("0x"$2)} END {print s}'

Today: 6,260 bytes total.

Acceptance

  • fl::function total .text contribution drops below 3 KB on LPC845-BRK low-memory build
  • No regression in fl::function semantics (copy/move/destroy correctness) on ESP32 / Teensy where the full variant stays available
  • Guard test for the new size budget added under CI

Related

Activity

  1. zackees commented on Jun 15, 2026

    @zackees
    MemberAuthor

    Tracked under meta #3079 (LPC845 64 KB flash bloat hunt) alongside three sibling bloat sub-issues and the FastLED/fbuild#594 tooling-gap dependency.

  2. changed the title [-]LPC845 bloat: fl::function<void(fl::json const&)> variant emits ~13 KB of copy/move/destroy clones per signature[/-] [+]LPC845 bloat: fl::function variant emits 6.3 KB of copy/move/destroy clones across RPC dispatch signatures[/+] on Jun 15, 2026
  3. zackees commented on Jun 15, 2026

    @zackees
    MemberAuthor

    Issue body and title revised 2026-06-15 with real nm --print-size data (6.3 KB total across all signatures, not 13 KB per signature). Now part of revised meta #3079 sub-issue set: {#3076 soft-FP, #3077 variant, #3081 RPC dispatch, #3082 JSON codec}. Two earlier sub-issues #3075 #3078 closed as filed-in-error.

  4. zackees commented on Jun 15, 2026

    @zackees
    MemberAuthor

    Investigation findings (2026-06-15)

    Source survey

    fl::function<Sig> (in src/fl/stl/function.h) is type-erased over a 5-alternative fl::variant:

    fl::variant<
        shared_ptr<CallableBase>,    // heap-allocated callable
        FreeFunctionCallable,        // function pointer
        InlinedLambda,               // SBO inlined lambda
        NonConstMemberCallable,      // bound member function (non-const)
        ConstMemberCallable          // bound member function (const)
    >
    

    fl::variant<> emits a per-(T, operation) function table: copy_fn, move_fn, destroy_fn, copy_construct_fn for each alternative, plus the visit_fn dispatcher per visitor. With 5 alternatives × ~4 operations × N signatures, the codegen tax compounds.

    The nm-measured total fl::function-related symbol mass on the LPC845 LowMemory build is 6,260 bytes across fl::function<void()>, fl::function<void(fl::json const&)>, fl::function<fl::json(fl::json const&)>, and fl::function<void(fl::ResponseSend&, fl::json const&)>.

    Where the LowMemory build actually stores callables

    Audit of fl::Remote::Remote() and the AutoResearchLowMemory RPC registrations:

    • Remote::Remote(...) constructs InlinedLambda instances (the [this, ...](json const&) { ... } closures)
    • RPC method registration stores function pointers (FreeFunctionCallable) for free-function-style handlers and InlinedLambda for stateful ones
    • No MemberCallable instances are constructed in the LPC8xx path
    • No shared_ptr<CallableBase> heap fallback paths are reachable (would require an oversized lambda — InlinedLambda has SBO sizing that fits all current closures)

    So on the LowMemory build, 3 of the 5 alternatives are dead code but still emit copy_fn / move_fn / destroy_fn instantiations.

    Possible directions, with risk assessment

    1. #if !FL_PLATFORM_HAS_LARGE_MEMORY gate that drops the variant to variant<FreeFunctionCallable, InlinedLambda>. Two alternatives instead of five — eliminates ~60% of the codegen tax for free.
      • Risk: any code path that constructs fl::function from a &Foo::bar (member function) would fail to compile on Low-memory targets. Need to audit ESP32/Teensy/WASM call sites for member-function constructions to confirm none are reachable from the LowMemory build.
    2. Move copy/move/destroy through a single dispatch vtable instead of variant per-alternative _fn<T> instantiations.
      • Risk: rewrites the variant<> machinery itself — invasive.

    Scope assessment

    Path 1 is the cleanest small fix in this entire meta. It is one #if block in function.h, plus an audit of member-function call sites. Estimated scope: 1 PR, 1 day of work, low cross-platform risk if the audit is clean.

    However, the savings (~3 KB) alone won't clear the 11 KB overage. Recommend this PR be done alongside #3076 (or after #3076 lands) — between them they would cover ~11 KB which matches the overage.

    Marking as ready for PR once paired with another sub-issue's fix. Not a candidate to ship alone.

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