Repository navigation
perf(fl::function): Option B refactor -- single SBO + invoker + manager (#3235 Tier 1B, -9.2 KB Teensy 4) - #3238
Conversation
…er (closes #3235 Tier 1B) Replaces the previous 5-alternative `fl::variant` storage (heap-fallback + free-fn + inlined-lambda + non-const member + const member) with a single small-buffer-optimized storage plus two function pointers: - `Invoker`: per-Func; knows how to call the stored callable with (Args...). - `Manager`: per-Func (or shared `trivial_manager<sizeof(Func)>` for trivially-copyable callables); multiplexes copy / move / destroy through an Op enum. The variant's per-(T, op) thunk explosion -- 5 alternatives × 4 ops × N signatures = ~25 small functions per Sig at ~80-120 B each -- collapses to one invoker + one manager pointer per Func instance. Trivially-copyable captures (the common case) share a per-size `memcpy` manager so the linker collapses many F's into one body. ## Member-function support preserved The `function(R(C::*)(Args...), C*)` and const variant constructors remain in the public API. They route through thin `NonConstMemberWrapper<C>` and `ConstMemberWrapper<C>` structs stored in the SBO -- the legacy `MemberCallableBase` / `NonConstMemberCallable` / `ConstMemberCallable` class hierarchy and its virtual dispatch are gone. ## Heap fallback retained Lambdas larger than `FASTLED_INLINE_LAMBDA_SIZE` (default 64 B) still compile -- they get wrapped in a `HeapHolder<Func>` struct that stores `fl::shared_ptr<Func>` in the SBO. This preserves the legacy "big lambda lives on the heap" semantics without re-introducing the variant. Tradeoff investigation tracked separately at #3237 (HeapHolder's shared_ptr vs unique_ptr-deep-copy vs single-threaded-refcount options). ## Verification LPC845-BRK / AutoResearch (Low-memory): flash 49,396 B -> 48,260 B (-1,136 B) RAM 9,984 B -> 9,984 B (unchanged) Teensy 4.0 / AutoResearch (Large-memory): flash 357,376 B -> 348,160 B (-9,216 B saved, -2.6%) RAM 112,576 B -> 110,528 B (-2,048 B saved) `fl::function`-related symbol mass on LPC845 dropped from 3,164 B (post- #3084) to 2,302 B (-862 B specifically from the variant collapse, the rest is downstream DCE). The Low-memory `FL_FUNCTION_FULL_VARIANT` macro is no longer needed -- both tiers share the same Option B implementation. The Low-memory "no-op heap-fallback" branch from #3077/#3084 is replaced by the real `HeapHolder` path; behavior is now uniform across all targets. ## Rename: F -> Func template parameter Renamed the template parameter throughout to `Func` because Arduino's `Arduino.h` (LPC8xx core, AVR core) defines `F()` as a PROGMEM-string preprocessor macro that collides with `F` used as a template parameter name. Same root cause as #3086's m0clockless_c.h fix. Refs #3235 Tier 1B, #3237
|
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 (1)
📝 WalkthroughWalkthrough
Changesfl::function SBO rewrite
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related issues
Possibly related PRs
Poem
✨ 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 |
Refs #3235 Tier 1B.
Summary
First sub-issue PR against the type-erasure burn-down meta. Replaces
fl::function<R(Args...)>'s 5-alternative variant storage with a single SBO + invoker + manager pointer pair. Per-Sig codegen tax collapses from ~25 thunks to 1 invoker + 1 manager pointer. Member-function support preserved via SBO-stored wrappers; heap fallback retained viaHeapHolder<F>+shared_ptr<F>.Numbers
fl::functionsymbol mass on LPC845: 3,164 B → 2,302 B (−862 B from variant collapse, rest is downstream DCE).What changed
Storage = variant<5 alts>→ singleCallable { Invoker; Manager; char[SBO]; }struct.memcpymanager (linker collapses many F's into one body).non_trivial_managerwith placement-new + destructor.function(R(C::*)(Args...), C*)) survive as thinMemberWrapper<C>structs stored in the SBO.HeapHolder<F>(=shared_ptr<F>inside the SBO).FL_FUNCTION_FULL_VARIANTmacro is gone — both Low-memory and Large-memory builds share Option B.F→Functo avoid colliding with Arduino'sF()PROGMEM macro (same root cause as fix(avr/math): #undef Arduino F() macro before fl::math polynomial templates (unbreak master Uno build) #3086).Behavior preservation
All public API stays identical:
function()/ copy / move / destroyfunction(R(*)(Args...))— free functionfunction(F)— lambda / functor (SFINAE-excluded for fn-ptrs and member-ptrs)function(R(C::*)(Args...), C*)— non-const member functionfunction(R(C::*)(Args...) const, const C*)— const member functionoperator()/operator bool/clear()/operator==/operator!=Test plan
tests/fl/stl/functional.cpp) — pre-existing master test runner flakiness affects this; will be picked up in CIRelated
🤖 Generated with Claude Code
Summary by CodeRabbit
Refactor
fl::functionclass implementation with revised callable storage mechanism and lifecycle management.New Features
fl::functionare now unconditionally available instead of being conditionally compiled.