Repository navigation
feat(function): add FL_FUNCTION_NO_HEAP_FALLBACK opt-out macro (closes #3237) - #3269
Conversation
…#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]>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughAdds a Changesfl::function heap-fallback opt-out
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ 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 |
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_FALLBACKbefore including FastLED headers; the over-SBO path becomes astatic_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:
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 distinctfl::function<>signatures across ~163 sites, dominated by under-SBO captures).This satisfies the issue's acceptance bullet:
How it works
When the macro is defined, the over-SBO
init_with_impl(Func&&, false_type)overload swaps its body for aFL_STATIC_ASSERT(sizeof(FBare) <= kSboSize, "...")that fires at compile time with a message naming the three remediation paths:FASTLED_INLINE_LAMBDA_SIZEfor the build.FL_FUNCTION_NO_HEAP_FALLBACKto 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:fl::function<int()>from a lambda capturing a 100+ B struct.fl::function-- both copies see the same captured payload (shared_ptr refcount, not deep-copy).bash test --cpp-- 330/330 unit tests pass.bash lint --cpp-- 0 violations.FL_FUNCTION_NO_HEAP_FALLBACKnegative 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#ifdefbranch ininit_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::functionrefactor).🤖 Generated with Claude Code
Summary by CodeRabbit
New Features
FL_FUNCTION_NO_HEAP_FALLBACKmacro 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