Skip to content

perf(fl::function): Option B refactor -- single SBO + invoker + manager (#3235 Tier 1B, -9.2 KB Teensy 4) - #3238

Merged
zackees merged 1 commit into
masterfrom
feat/fl-function-option-b-single-invoker-3235
Jun 18, 2026
Merged

zackees merged 1 commit into
masterfrom
feat/fl-function-option-b-single-invoker-3235

Conversation

@zackees

@zackees zackees commented Jun 18, 2026 •

Copy link
Copy Markdown
Member

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 via HeapHolder<F> + shared_ptr<F>.

Numbers

Target Flash before Flash after Δ RAM before RAM after Δ
Teensy 4 / AutoResearch (Large-memory) 357,376 B 348,160 B −9,216 B (-2.6%) 112,576 B 110,528 B −2,048 B
LPC845 / AutoResearch (Low-memory) 49,396 B 48,260 B −1,136 B (-2.3%) 9,984 B 9,984 B unchanged

fl::function symbol mass on LPC845: 3,164 B → 2,302 B (−862 B from variant collapse, rest is downstream DCE).

What changed

  • Storage = variant<5 alts> → single Callable { Invoker; Manager; char[SBO]; } struct.
  • Variant's per-(T × op) thunk explosion (5 × 4 × N) collapses to 1 invoker + 1 manager per Func.
  • Trivially-copyable callables share a per-size memcpy manager (linker collapses many F's into one body).
  • Non-trivially-copyable callables get a per-F non_trivial_manager with placement-new + destructor.
  • Member-fn constructors (function(R(C::*)(Args...), C*)) survive as thin MemberWrapper<C> structs stored in the SBO.
  • Heap fallback for over-SBO lambdas: wrap in HeapHolder<F> (= shared_ptr<F> inside the SBO).
  • FL_FUNCTION_FULL_VARIANT macro is gone — both Low-memory and Large-memory builds share Option B.
  • Template parameter renamed F → Func to avoid colliding with Arduino's F() 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 / destroy
  • function(R(*)(Args...)) — free function
  • function(F) — lambda / functor (SFINAE-excluded for fn-ptrs and member-ptrs)
  • function(R(C::*)(Args...), C*) — non-const member function
  • function(R(C::*)(Args...) const, const C*) — const member function
  • operator() / operator bool / clear() / operator== / operator!=

Test plan

  • LPC845 (Low-memory) build → succeeded, savings as reported
  • Teensy 4 (Large-memory) build → succeeded, savings as reported
  • CI: full matrix
  • Host functional test (tests/fl/stl/functional.cpp) — pre-existing master test runner flakiness affects this; will be picked up in CI

Related

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Refactor

    • Substantially redesigned the fl::function class implementation with revised callable storage mechanism and lifecycle management.
  • New Features

    • Member-function constructors for fl::function are now unconditionally available instead of being conditionally compiled.

…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
@coderabbitai

coderabbitai Bot commented Jun 18, 2026 •

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 23c7c049-1716-4548-bd90-7d409c274980

📥 Commits

Reviewing files that changed from the base of the PR and between f787011 and ea23baa.

📒 Files selected for processing (1)
  • src/fl/stl/function.h

📝 Walkthrough

Walkthrough

fl::function<R(Args...)> in src/fl/stl/function.h is rewritten from a fl::variant-based type-erasure to a single small-buffer-optimized design. Storage is a fixed 64-byte SBO byte buffer with two function pointers: an Invoker for calling and a multiplexed Manager for destroy/copy/move. Over-SBO callables fall back to a HeapHolder via fl::shared_ptr stored inside the same buffer. Member-function constructors are now unconditionally available.

Changes

fl::function SBO rewrite

Layer / File(s) Summary
SBO size config, includes, and is_function_pointer trait
src/fl/stl/function.h
Header drops the fl/stl/variant.h include, sets FASTLED_INLINE_LAMBDA_SIZE to 64 bytes, and adds an explicit R(*)(Args...) specialization to is_function_pointer.
Core SBO storage: Manager, Invoker, HeapHolder, init_with, reset
src/fl/stl/function.h
Replaces the variant-based model with a fixed SBO byte buffer plus enum-multiplexed Manager and typed Invoker. Handles R=void null path via null_return_impl, trivial vs. non-trivial copy/move manager paths, an over-SBO HeapHolder using fl::shared_ptr, and init_with/reset dispatch.
Public constructors, member-function wrappers, copy/move, and operators
src/fl/stl/function.h
Member-function constructors for mutable and const pointer targets are now unconditionally available as wrapper structs placed in SBO. Copy/move constructors and assignments delegate to Manager. operator(), operator bool, and comparison operators reflect the mHasValue-based empty state.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related issues

Possibly related PRs

  • FastLED/FastLED#3084: Modifies the same fl::function<R(Args...)> implementation in src/fl/stl/function.h, including the member-function constructor gating under FL_FUNCTION_FULL_VARIANT that this PR removes.
  • FastLED/FastLED#3088: Modifies FASTLED_INLINE_LAMBDA_SIZE and SBO sizing logic in src/fl/stl/function.h, the same configuration this PR consolidates to 64 bytes.

Poem

🐇 Hop, hop — no more variant maze,
A tidy byte buffer now holds my phase.
Manager, Invoker, SBO in line,
HeapHolder catches what won't fit fine.
Sixty-four bytes, one path to call —
This fluffy rabbit rewrote it all! 🌟

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/fl-function-option-b-single-invoker-3235

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant