Repository navigation
Description
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-sizedata 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
- 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_MEMORYgate that drops the variant to a 2-element {free_fn, inlined_lambda} would save ~40% of the per-signature cost. - 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. - Erase the lambda-id template parameter from
destroy_lambda<Lambda>— each unique lambda lifts a newdestroy_lambdainstantiation. 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::functiontotal.textcontribution drops below 3 KB on LPC845-BRK low-memory build - No regression in
fl::functionsemantics (copy/move/destroy correctness) on ESP32 / Teensy where the full variant stays available - Guard test for the new size budget added under CI
Related
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
zackees commentedon Jun 15, 2026
Tracked under meta #3079 (LPC845 64 KB flash bloat hunt) alongside three sibling bloat sub-issues and the FastLED/fbuild#594 tooling-gap dependency.
[-]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[/+]zackees commentedon Jun 15, 2026
Issue body and title revised 2026-06-15 with real
nm --print-sizedata (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.zackees commentedon Jun 15, 2026
Investigation findings (2026-06-15)
Source survey
fl::function<Sig>(insrc/fl/stl/function.h) is type-erased over a 5-alternativefl::variant: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 acrossfl::function<void()>,fl::function<void(fl::json const&)>,fl::function<fl::json(fl::json const&)>, andfl::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)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
#if !FL_PLATFORM_HAS_LARGE_MEMORYgate that drops the variant tovariant<FreeFunctionCallable, InlinedLambda>. Two alternatives instead of five — eliminates ~60% of the codegen tax for free.fl::functionfrom 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._fn<T>instantiations.Scope assessment
Path 1 is the cleanest small fix in this entire meta. It is one
#ifblock infunction.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.
perf(fl::function): collapse callable variant to {free_fn, inlined_la…
perf(rpc/lpc): gate schema generator + enable RX_SCT_WS2812 default o…
perf(fl::function): collapse callable variant to {free_fn, inlined_la…
perf(fl::function): Option B refactor -- single SBO + invoker + manag…