Repository navigation
feat(channels): pre-bind legacy clockless to BusTraits singleton (Phase 5b of #2428) - #2441
Conversation
…se 5b of #2428) Phase 5b lays the foundation for the binary-bloat fix from #2420 / #2421. Adds an opt-in ManagerChannelManager-bypass for legacy `addLeds<>`-style controllers, gated behind `FASTLED_DISABLE_LEGACY_DRIVER_REGISTRY=1` so the default behavior is unchanged. Mechanism: 1) `Channel::setDriver(shared_ptr<IChannelDriver>)` - new protected setter that records a pre-bound driver AND sets `mDriverPreBound = true`. 2) `Channel::showPixels()` - when `mDriverPreBound` is true, uses the cached `mDriver` directly and bypasses `ChannelManager::selectDriverForChannel()`. When false (the default for `Channel::create(cfg)`-created channels), the existing per-frame manager re-selection runs as before -- preserves the "swap drivers at runtime by changing priorities" contract that the `Channel API: Non-affinity channel re-binds when driver priorities swap` test in fastled_core.cpp depends on. 3) Per-platform legacy clockless controllers pre-bind their default Bus's driver in their constructor, gated by the new opt-in macro: - `src/platforms/esp/32/drivers/rmt/rmt_5/idf5_clockless.h` -> Bus::RMT - `src/platforms/stub/clockless_channel_stub.h` -> Bus::STUB Naming `BusTraits<Bus::X>::instancePtr()` here is the ODR-use that lets the linker keep ONLY the platform-default driver TU when the user enables the macro. Combined with future per-driver opt-in aggregator gating (Phase 5c), this delivers the binary-size fix from #2420. 4) `FASTLED_DISABLE_LEGACY_DRIVER_REGISTRY` is now defined centrally in `fl/channels/bus.h` (defaulting to 0) so all consumers see the same value. Phase 5c will: - Modify `_build.cpp.hpp` aggregators to gate per-driver subdir includes behind the macro, so unused driver TUs aren't even compiled. - Wire up other ESP32 legacy clockless paths (RMT4, ClocklessSPI fallback, ClocklessBlocking fallback). - Measure binary size on the #2420 reproducer (expect ~783 KB -> ~344 KB on NEOPIXEL ESP32-S3). Verification: - bash lint clean. - bash test --cpp full suite has the same pre-existing test-order flakes as master HEAD (`fastled_core: Channel API: Non-affinity channel re-binds` and a few stl tests). All affected tests pass when run in isolation. None of the failures are caused by this PR. - bash test fastled_core --debug --clean: passes 46/46 in isolation, proving the manager re-selection contract is preserved. - New mDriverPreBound flag is initialized to false by default, so `Channel::create(cfg)` behavior is byte-for-byte identical to pre-PR. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
📝 WalkthroughWalkthroughThis PR introduces an opt-in compile-time toggle and pre-binding infrastructure to allow legacy clockless controllers to bind their drivers at construction instead of selecting drivers dynamically at each showPixels() call. When enabled, the flag causes platform-specific controllers to pre-bind via a new Channel::setDriver() method, bypassing ChannelManager selection. ChangesLegacy Driver Registry Pre-Binding
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes Possibly related issues
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 |
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/fl/channels/channel.cpp.hpp (1)
316-331:⚠️ Potential issue | 🟡 Minor | ⚡ Quick winPre-bound channels are silently broken after
removeFromDrawList().
Channel::removeFromDrawList()at line 499 unconditionally doesmDriver.reset(). For a pre-bound channel (mDriverPreBound == true), the nextshowPixels()will hit line 322-323, get a nulldriverfrommDriver.lock(), and bail out at line 328 with the generic "No compatible driver found" error — even though the BusTraits singleton is still very much alive.Two follow-ups worth considering:
- In
removeFromDrawList(), skip themDriver.reset()whenmDriverPreBoundis true (or re-pre-bind on next add).- When
mDriverPreBound && !driver, log a more specific error (e.g. "pre-bound driver weak_ptr expired") so users opting intoFASTLED_DISABLE_LEGACY_DRIVER_REGISTRY=1get an actionable message instead of being told no compatible driver exists.This is unlikely to bite typical
addLeds<>users (they don't churn the draw list) but it's a real latent bug for the opt-in path.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/fl/channels/channel.cpp.hpp` around lines 316 - 331, The pre-bound driver gets dropped by Channel::removeFromDrawList() via mDriver.reset(), causing showPixels() to observe mDriverPreBound==true but mDriver.lock() returning null and emit a misleading "No compatible driver found" error; fix by (1) changing Channel::removeFromDrawList() to skip mDriver.reset() when mDriverPreBound is true (so pre-bound BusTraits singletons remain bound), and (2) in the showPixels()/driver-selection block (the code using mDriverPreBound, mDriver.lock(), and ChannelManager::selectDriverForChannel), detect the case where mDriverPreBound is true but lock() returned null and emit a specific error message like "pre-bound driver weak_ptr expired" (instead of the generic "No compatible driver found") so users get an actionable hint.
🧹 Nitpick comments (4)
src/platforms/stub/clockless_channel_stub.h (2)
49-50: 💤 Low valueUnusual line break in constructor declaration.
The opening
FL_NOEXCEPT {is split awkwardly onto its own line:ClocklessController() FL_NOEXCEPT {This appears to be a stray formatting artifact from the diff — the rest of this file (and
idf5_clockless.h) keeps the noexcept specifier on the same line. Worth normalizing.♻️ Proposed fix
- ClocklessController() - FL_NOEXCEPT { + ClocklessController() FL_NOEXCEPT {🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/platforms/stub/clockless_channel_stub.h` around lines 49 - 50, Normalize the constructor declaration for ClocklessController by moving the FL_NOEXCEPT specifier onto the same line as the constructor signature so it matches the rest of the file and idf5_clockless.h; update the ClocklessController() FL_NOEXCEPT { split to a single-line declaration "ClocklessController() FL_NOEXCEPT {" to remove the stray line break and keep formatting consistent.
54-63: ⚖️ Poor tradeoffArchitectural inconsistency with
ClocklessIdf5's pre-bind path.
ClocklessIdf5(inidf5_clockless.h) inherits fromfl::Channeland uses the newChannel::setDriver()API that flipsmDriverPreBoundtotrue, so itsshowPixels()takes a hard fast-path that skipsChannelManagerentirely.This stub controller, however, does not inherit from
fl::Channel— it inherits fromCPixelLEDControllerand maintains its ownmDriverweak_ptr (line 42). Assigning to that localmDriverhere doesn't set any "pre-bound" flag, soshowPixels()(lines 71-84) still has the fallback toChannelManager::selectDriverForChannel()if the weak_ptr ever expires. In practice theBusTraits<Bus::STUB>::instancePtr()singleton lives forever, so the fallback is unreachable, but the code shapes are now noticeably divergent for what the comment claims is the same Phase 5b pre-bind mechanism.Worth either (a) refactoring this stub controller to inherit from
ChannellikeClocklessIdf5so both share the pre-bound path, or (b) adding a comment clarifying that the stub's "pre-binding" is purely caching and the manager fallback is intentionally retained.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/platforms/stub/clockless_channel_stub.h` around lines 54 - 63, The stub controller’s assignment of BusTraits<Bus::STUB>::instancePtr() to its local mDriver (in the CPixelLEDController-derived stub) does not flip Channel::mDriverPreBound like Channel::setDriver() does in ClocklessIdf5, creating an architectural mismatch where showPixels() still retains the ChannelManager::selectDriverForChannel() fallback; either refactor the stub to inherit from fl::Channel and call Channel::setDriver() so mDriverPreBound is set and the pre-bound fast-path is consistent with ClocklessIdf5, or, if keeping CPixelLEDController, add a clear comment near the stub’s mDriver assignment and in showPixels() stating this is only a cached pointer (singleton-backed) and that the manager fallback is intentionally retained.src/fl/channels/channel.h (2)
208-211: 💤 Low valueConsider passing the shared_ptr by const reference.
setDrivertakesfl::shared_ptr<IChannelDriver>by value but only assigns it tomDriver(aweak_ptr), so the strong reference is unnecessarily incremented and decremented. Aconst fl::shared_ptr<IChannelDriver>&would avoid the temporary refcount churn. Minor since it's called once per controller construction.♻️ Proposed refactor
- void setDriver(fl::shared_ptr<IChannelDriver> driver) FL_NOEXCEPT { + void setDriver(const fl::shared_ptr<IChannelDriver>& driver) FL_NOEXCEPT { mDriver = driver; mDriverPreBound = true; }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/fl/channels/channel.h` around lines 208 - 211, The setter setDriver currently takes fl::shared_ptr<IChannelDriver> by value causing an unnecessary increment/decrement of the shared_ptr refcount; change its parameter to const fl::shared_ptr<IChannelDriver>& and assign that to mDriver (a weak_ptr), leaving the rest (setting mDriverPreBound = true) unchanged — update the declaration and definition of setDriver and ensure references to IChannelDriver, mDriver, and mDriverPreBound are used accordingly.
196-211: 💤 Low valueNo way to undo pre-binding once set.
setDriver()only flipsmDriverPreBoundtotrue; there's no symmetricclearDriver()or reset path. If a future caller (or test fixture) needs to revert a pre-bound channel back to the manager-selection path, they cannot. Given this is a protected hook used only at construction today, it's acceptable, but consider documenting this one-way contract or adding a reset method when Phase 5c lands.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/fl/channels/channel.h` around lines 196 - 211, setDriver() makes pre-binding one-way by setting mDriver and mDriverPreBound=true with no way to revert; add a symmetric reset/clear API (e.g., clearDriver() or setDriver(nullptr)) that resets mDriver (release/reset the weak_ptr) and sets mDriverPreBound=false so the Channel can return to manager selection, and update any uses/tests and documentation to reflect the new reversible contract; reference setDriver, mDriver, and mDriverPreBound when making the change.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/fl/channels/bus.h`:
- Around line 18-34: The header must document and enforce that
FASTLED_DISABLE_LEGACY_DRIVER_REGISTRY be set consistently at build-system scope
(not per-TU) because it changes template constructor bodies (e.g.,
ClocklessIdf5::ClocklessIdf5() and ClocklessController::ClocklessController()),
so update the comment to require using a -D flag or project-wide user config and
add a small compile-time guard: introduce a companion build-only marker macro
(e.g., FASTLED_DISABLE_LEGACY_DRIVER_REGISTRY_FROM_BUILD) that build systems set
alongside FASTLED_DISABLE_LEGACY_DRIVER_REGISTRY, and emit a `#pragma` message or
`#warning` when FASTLED_DISABLE_LEGACY_DRIVER_REGISTRY is defined without that
build marker to tell users to move the define to their build system; also
document the required practice in the header near the
FASTLED_DISABLE_LEGACY_DRIVER_REGISTRY definition.
---
Outside diff comments:
In `@src/fl/channels/channel.cpp.hpp`:
- Around line 316-331: The pre-bound driver gets dropped by
Channel::removeFromDrawList() via mDriver.reset(), causing showPixels() to
observe mDriverPreBound==true but mDriver.lock() returning null and emit a
misleading "No compatible driver found" error; fix by (1) changing
Channel::removeFromDrawList() to skip mDriver.reset() when mDriverPreBound is
true (so pre-bound BusTraits singletons remain bound), and (2) in the
showPixels()/driver-selection block (the code using mDriverPreBound,
mDriver.lock(), and ChannelManager::selectDriverForChannel), detect the case
where mDriverPreBound is true but lock() returned null and emit a specific error
message like "pre-bound driver weak_ptr expired" (instead of the generic "No
compatible driver found") so users get an actionable hint.
---
Nitpick comments:
In `@src/fl/channels/channel.h`:
- Around line 208-211: The setter setDriver currently takes
fl::shared_ptr<IChannelDriver> by value causing an unnecessary
increment/decrement of the shared_ptr refcount; change its parameter to const
fl::shared_ptr<IChannelDriver>& and assign that to mDriver (a weak_ptr), leaving
the rest (setting mDriverPreBound = true) unchanged — update the declaration and
definition of setDriver and ensure references to IChannelDriver, mDriver, and
mDriverPreBound are used accordingly.
- Around line 196-211: setDriver() makes pre-binding one-way by setting mDriver
and mDriverPreBound=true with no way to revert; add a symmetric reset/clear API
(e.g., clearDriver() or setDriver(nullptr)) that resets mDriver (release/reset
the weak_ptr) and sets mDriverPreBound=false so the Channel can return to
manager selection, and update any uses/tests and documentation to reflect the
new reversible contract; reference setDriver, mDriver, and mDriverPreBound when
making the change.
In `@src/platforms/stub/clockless_channel_stub.h`:
- Around line 49-50: Normalize the constructor declaration for
ClocklessController by moving the FL_NOEXCEPT specifier onto the same line as
the constructor signature so it matches the rest of the file and
idf5_clockless.h; update the ClocklessController() FL_NOEXCEPT { split to a
single-line declaration "ClocklessController() FL_NOEXCEPT {" to remove the
stray line break and keep formatting consistent.
- Around line 54-63: The stub controller’s assignment of
BusTraits<Bus::STUB>::instancePtr() to its local mDriver (in the
CPixelLEDController-derived stub) does not flip Channel::mDriverPreBound like
Channel::setDriver() does in ClocklessIdf5, creating an architectural mismatch
where showPixels() still retains the ChannelManager::selectDriverForChannel()
fallback; either refactor the stub to inherit from fl::Channel and call
Channel::setDriver() so mDriverPreBound is set and the pre-bound fast-path is
consistent with ClocklessIdf5, or, if keeping CPixelLEDController, add a clear
comment near the stub’s mDriver assignment and in showPixels() stating this is
only a cached pointer (singleton-backed) and that the manager fallback is
intentionally retained.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 046bbe63-21cc-43aa-bb9a-73bd5d8321c2
📒 Files selected for processing (5)
src/fl/channels/bus.hsrc/fl/channels/channel.cpp.hppsrc/fl/channels/channel.hsrc/platforms/esp/32/drivers/rmt/rmt_5/idf5_clockless.hsrc/platforms/stub/clockless_channel_stub.h
| // FASTLED_DISABLE_LEGACY_DRIVER_REGISTRY: opt-in macro that, when set to 1, | ||
| // disables the legacy `initChannelDrivers()` auto-registration AND switches | ||
| // per-platform legacy clockless controllers (stub `ClocklessController`, | ||
| // ESP32 `ClocklessIdf5`, etc.) to pre-bind directly to their default | ||
| // `BusTraits<Bus::X>::instancePtr()` -- bypassing `ChannelManager` entirely. | ||
| // | ||
| // This is the toggle that lets `--gc-sections` drop unreferenced driver TUs | ||
| // (the binary-size fix from #2420). Default 0 for backward compatibility; | ||
| // users opting in must call `fl::enableDrivers<fl::Bus::X...>()` explicitly | ||
| // for any non-default driver they need at runtime. | ||
| // | ||
| // Defined here (early-included by every channels TU) so all consumers see the | ||
| // same value -- the macro is checked from per-platform legacy clockless | ||
| // headers as well as channel_manager_esp32.cpp.hpp. | ||
| #ifndef FASTLED_DISABLE_LEGACY_DRIVER_REGISTRY | ||
| #define FASTLED_DISABLE_LEGACY_DRIVER_REGISTRY 0 | ||
| #endif |
There was a problem hiding this comment.
ODR risk: macro must be defined consistently across all translation units.
FASTLED_DISABLE_LEGACY_DRIVER_REGISTRY gates inline code inside templated classes (ClocklessIdf5::ClocklessIdf5() and stub ClocklessController::ClocklessController()). If different TUs include those headers with different macro values (e.g., user #defines it in one .cpp after a system include but not another, or only some build files set -DFASTLED_DISABLE_LEGACY_DRIVER_REGISTRY=1), the resulting template instantiations will differ in their constructor bodies — a classic ODR violation that the linker will silently merge into one definition.
Recommend documenting (and ideally enforcing) that this macro must be set as a build-system-level definition (e.g., a -D flag or a project-wide FastLED_user.h) and not via per-TU #define. A #pragma message/#warning if the user defines it after <FastLED.h> would also help.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@src/fl/channels/bus.h` around lines 18 - 34, The header must document and
enforce that FASTLED_DISABLE_LEGACY_DRIVER_REGISTRY be set consistently at
build-system scope (not per-TU) because it changes template constructor bodies
(e.g., ClocklessIdf5::ClocklessIdf5() and
ClocklessController::ClocklessController()), so update the comment to require
using a -D flag or project-wide user config and add a small compile-time guard:
introduce a companion build-only marker macro (e.g.,
FASTLED_DISABLE_LEGACY_DRIVER_REGISTRY_FROM_BUILD) that build systems set
alongside FASTLED_DISABLE_LEGACY_DRIVER_REGISTRY, and emit a `#pragma` message or
`#warning` when FASTLED_DISABLE_LEGACY_DRIVER_REGISTRY is defined without that
build marker to tell users to move the define to their build system; also
document the required practice in the header near the
FASTLED_DISABLE_LEGACY_DRIVER_REGISTRY definition.
Summary
Phase 5b of #2428. Lays the foundation for the binary-bloat fix from #2420 / #2421. Adds an opt-in
ChannelManager-bypass for legacyaddLeds<>-style controllers, gated behindFASTLED_DISABLE_LEGACY_DRIVER_REGISTRY=1so default behavior is unchanged.Mechanism
1.
Channel::setDriver(shared_ptr<IChannelDriver>)— new protected setter that records a pre-bound driver AND setsmDriverPreBound = true.2.
Channel::showPixels()— whenmDriverPreBoundis true, uses the cachedmDriverdirectly and bypassesChannelManager::selectDriverForChannel(). When false (the default forChannel::create(cfg)-created channels), the existing per-frame manager re-selection runs as before — preserves the swap drivers at runtime by changing priorities contract that theChannel API: Non-affinity channel re-binds when driver priorities swaptest depends on.3. Per-platform legacy clockless controllers pre-bind their default Bus's driver in their constructor, gated by the new opt-in macro:
src/platforms/esp/32/drivers/rmt/rmt_5/idf5_clockless.h→Bus::RMTsrc/platforms/stub/clockless_channel_stub.h→Bus::STUBNaming
BusTraits<Bus::X>::instancePtr()here is the ODR-use that lets the linker keep ONLY the platform-default driver TU when the user enables the macro. Combined with future per-driver opt-in aggregator gating (Phase 5c), this delivers the binary-size fix from #2420.4.
FASTLED_DISABLE_LEGACY_DRIVER_REGISTRYdefined centrally infl/channels/bus.h(default 0) so all consumers see the same value.Phase 5c (next, not in this PR)
_build.cpp.hppaggregators by the macro so unused driver TUs aren't compiled.Verification
bash lintclean.bash test fastled_core --debug --clean— passes 46/46 in isolation, proving the manager re-selection contract is preserved.bash test --cppfull suite shows pre-existing test-order flakes inherited from master HEAD (fastled_core: Channel API: Non-affinity channel re-bindsand a few stl tests). All affected tests pass when run in isolation. Verified by stashing my changes and re-running on bare master HEAD — samefastled_corefailure shows up. None of the failures are caused by this PR.mDriverPreBounddefault-initialized tofalse, soChannel::create(cfg)behavior is byte-for-byte identical to pre-PR.Test plan
FASTLED_DISABLE_LEGACY_DRIVER_REGISTRY=1and measure binary size.Related
🤖 Generated with Claude Code
Summary by CodeRabbit
Release Notes