Skip to content

feat(function): add FL_FUNCTION_NO_HEAP_FALLBACK opt-out macro (closes #3237) - #3269

Merged
zackees merged 1 commit into
masterfrom
feat/3237-function-no-heap-fallback
Jun 19, 2026
Merged

zackees merged 1 commit into
masterfrom
feat/3237-function-no-heap-fallback

Conversation

@zackees

@zackees zackees commented Jun 19, 2026 •

Copy link
Copy Markdown
Member

Summary

Adds the opt-out macro proposed in the #3237 investigation comment. Memory-constrained users who cannot tolerate the HeapHolder<F> + shared_ptr<F> over-SBO heap fallback can #define FL_FUNCTION_NO_HEAP_FALLBACK before including FastLED headers; the over-SBO path becomes a static_assert(false) that points to the offending call site with remediation guidance.

The default behaviour (heap fallback via shared_ptr<F>) is preserved when the macro is not defined.

Why this resolves #3237

The investigation concluded:

  1. Keep shared_ptr<F> as the default heap-fallback primitive -- it preserves the "copy doesn't allocate" semantics that users expect, the refcount cost is invisible on single-core MCUs, and the over-SBO path is rarely hit by FastLED's own code (audit found ~55 distinct fl::function<> signatures across ~163 sites, dominated by under-SBO captures).
  2. Add an opt-out for users who explicitly want compile-time heap-free guarantees -- this PR.

This satisfies the issue's acceptance bullet:

Add a FL_FUNCTION_NO_HEAP_FALLBACK macro (or document why not) so memory-constrained users can opt out.

How it works

When the macro is defined, the over-SBO init_with_impl(Func&&, false_type) overload swaps its body for a FL_STATIC_ASSERT(sizeof(FBare) <= kSboSize, "...") that fires at compile time with a message naming the three remediation paths:

  1. Reduce the captured state.
  2. Bump FASTLED_INLINE_LAMBDA_SIZE for the build.
  3. Undef FL_FUNCTION_NO_HEAP_FALLBACK to re-enable the heap fallback.

When the macro is not defined, the existing heap-fallback body runs as before.

Test plan

  • bash test function -- the new over-SBO test verifies the default heap-fallback path:
    • Construct fl::function<int()> from a lambda capturing a 100+ B struct.
    • Confirm it works (heap fallback fires).
    • Copy the fl::function -- both copies see the same captured payload (shared_ptr refcount, not deep-copy).
    • Rebind the original -- the copy is unaffected (sharing-by-refcount semantics preserved).
  • bash test --cpp -- 330/330 unit tests pass.
  • bash lint --cpp -- 0 violations.
  • The FL_FUNCTION_NO_HEAP_FALLBACK negative branch is not directly runtime-testable (it fires at compile time); the static_assert wording is reviewed in the source.

Closes #3237

Files

  • src/fl/stl/function.h -- adds the macro documentation block + the #ifdef branch in init_with_impl(Func&&, false_type).
  • tests/fl/stl/function.cpp -- adds the over-SBO round-trip test.

Refs #3237, #3244 (Phase 3 burn-down), #3235 (Option B fl::function refactor).

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Added FL_FUNCTION_NO_HEAP_FALLBACK macro to enforce stricter compile-time validation of lambda captures. When defined, oversized captures trigger compilation errors instead of automatic heap allocation, giving developers tighter control over memory usage.
  • Tests

    • Added test coverage for large lambda captures and memory sharing semantics.

…#3237)

Adds the opt-out macro proposed in the #3237 investigation comment.
Memory-constrained users who cannot tolerate the `HeapHolder<F>` +
`shared_ptr<F>` over-SBO heap fallback can `#define
FL_FUNCTION_NO_HEAP_FALLBACK` before including FastLED headers; the
fallback path becomes a `static_assert(false)` that points to the
over-SBO call site with explicit remediation guidance (reduce capture,
bump FASTLED_INLINE_LAMBDA_SIZE, or undef the macro to re-enable the
heap fallback).

The default behaviour (heap fallback via `shared_ptr<F>`) is preserved
when the macro is not defined -- per the #3237 audit and decision:

  #3237 (comment)

The investigation concluded that `shared_ptr<F>` is the correct
primitive for the default heap fallback (it preserves
copy-doesn't-allocate semantics, the refcount cost is invisible on
single-core MCUs, and the over-SBO path is rarely hit by FastLED's
own code). Adding the opt-out lets users who want compile-time
heap-free guarantees turn the path off without changing the default
for everyone else.

Test:
- `tests/fl/stl/function.cpp` adds an over-SBO capture round-trip test
  that confirms the default heap-fallback path works AND that
  `fl::function` copy/move semantics are preserved across the heap
  boundary (rebinding the original after a copy doesn't affect the
  copy -- the captured F was shared by refcount, not by reference).
- The static_assert-driven negative branch is not directly
  runtime-testable (it fires at compile time); manual verification of
  the static_assert wording is captured in the issue body.

All 330 host unit tests pass. Lint clean.

Closes #3237.

Refs #3244 (Phase 3 burn-down), #3235 (Option B fl::function refactor).

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@coderabbitai

coderabbitai Bot commented Jun 19, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: d935bd4c-c3f9-4499-9c34-bf225d0d0586

📥 Commits

Reviewing files that changed from the base of the PR and between 5dd070a and 6214902.

📒 Files selected for processing (2)
  • src/fl/stl/function.h
  • tests/fl/stl/function.cpp

📝 Walkthrough

Walkthrough

Adds a FL_FUNCTION_NO_HEAP_FALLBACK preprocessor macro to src/fl/stl/function.h. When defined, the over-SBO initialization path in fl::function emits a static_assert instead of wrapping the callable in a heap-backed HeapHolder/shared_ptr. A new test verifies the existing heap-fallback copy semantics when the macro is absent.

Changes

fl::function heap-fallback opt-out

Layer / File(s) Summary
FL_FUNCTION_NO_HEAP_FALLBACK macro guard
src/fl/stl/function.h
Adds a documentation comment block for the new macro and inserts an #ifdef FL_FUNCTION_NO_HEAP_FALLBACK guard inside init_with_impl's over-SBO branch; when defined, a static_assert fires with a fix suggestion instead of allocating a HeapHolder; #endif resumes the existing shared_ptr fallback.
Over-SBO heap-fallback copy test
tests/fl/stl/function.cpp
Adds FL_TEST_CASE with a LargeCapture struct padded to exceed FASTLED_INLINE_LAMBDA_SIZE, exercises invocation, copy construction, and rebind to confirm that copies share the original captured state and are unaffected by later reassignment of the original.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • FastLED/FastLED#3084: Directly modifies src/fl/stl/function.h's over-SBO heap-backed path, collapsing allocation under low-memory compile conditions — the same code region this PR guards with FL_FUNCTION_NO_HEAP_FALLBACK.
  • FastLED/FastLED#3088: Adjusts FASTLED_INLINE_LAMBDA_SIZE in src/fl/stl/function.h, directly affecting the SBO threshold that FL_FUNCTION_NO_HEAP_FALLBACK's static_assert references.
  • FastLED/FastLED#3238: Refactors the same HeapHolder/shared_ptr over-SBO fallback in src/fl/stl/function.h that this PR optionally suppresses via the new macro.

Poem

🐇 Hop, hop, the heap may sleep,
A macro guard, no allocs to keep.
static_assert shall ring the bell,
"Your capture's large!" it dares to tell.
But fear not — undefine and roam,
The shared_ptr finds its foam-y home. 🌿

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: adding the FL_FUNCTION_NO_HEAP_FALLBACK opt-out macro to fl::function. It is concise, directly related to the changeset, and includes the issue number being closed.
Linked Issues check ✅ Passed The PR fully addresses the acceptance criteria from issue #3237: it implements the FL_FUNCTION_NO_HEAP_FALLBACK macro providing compile-time opt-out for heap-free guarantees, and includes comprehensive testing of heap fallback and copy semantics.
Out of Scope Changes check ✅ Passed All changes in the PR are directly scoped to the macro implementation and corresponding test coverage. No extraneous modifications to unrelated functionality or codepaths are present.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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/3237-function-no-heap-fallback

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.

@zackees
zackees merged commit 4492212 into master Jun 19, 2026
11 checks passed
@fastled-project-sync fastled-project-sync Bot moved this from Triage to Done in FastLED Tracker Jun 19, 2026
@zackees
zackees deleted the feat/3237-function-no-heap-fallback branch August 21, 2026 19:15
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.

fl::function: investigate heap-allocation tradeoffs of the HeapHolder<F> + shared_ptr fallback (Option B follow-on)

1 participant