Repository navigation
Description
Motivation
The Yves Bazin I2S driver (restored in src/platforms/esp/32/drivers/i2s/i2s_esp32dev.cpp.hpp) carries ~15 file-scope statics (gCallback, gCntBuffer, gDoneFilling, dmaBuffers[], gTX_semaphore, gI2S_intr_handle, i2s, timing tables, ...). Each lands in .data/.bss/.rodata unconditionally — the linker sees a definition at namespace scope with external/internal linkage and must emit storage even when the user program never calls into the I2S driver.
FastLED's convention is fl::Singleton<T> (see src/fl/stl/singleton.h), which hides storage inside a function-local static AlignedStorage storage; inside instance(). --gc-sections can then drop Singleton<T>::instance() AND its storage together when no code path references it. Same elision benefit for mutable state, const tables, and FL_PROGMEM tables.
A tree-wide scan (ci/scan_singleton_elision.py) finds 119 namespace-scope variable definitions across 38 files that violate this convention.
Top offenders
| Hits | File |
|---|---|
| 19 | src/platforms/esp/32/drivers/i2s/i2s_esp32dev.cpp.hpp |
| 14 | src/platforms/esp/32/drivers/channel_manager_esp32.cpp.hpp |
| 8 | src/platforms/arm/teensy/teensy4_common/drivers/objectfled/objectfled_diagnostics.cpp.hpp |
| 7 | src/platforms/arm/teensy/teensy4_common/drivers/flexio/flexio_driver.cpp.hpp |
| 6 | src/fl/audio/detector/equalizer.cpp.hpp |
| 5 | src/platforms/arm/teensy/teensy4_common/drivers/lpuart/lpuart_driver.cpp.hpp |
Elision win — back-of-envelope
- ~2 KB
.data/.bssper peripheral driver × 8 drivers × ~40% average sketch elision rate ≈ 8–12 KB freed on a typical ESP32 build - STL-adjacent state (allocator hooks, stream state): ~1–2 KB
- Small residue: ~1 KB
- Realistic total: 10–15 KB flash / 3–5 KB DRAM on ESP32; larger multiplier on AVR/ATtiny
Tiered rollout — sub-issues
Migration is deliberately incremental — big-bang refactor is too risky because ISR-touching driver code needs a careful Singleton<T>::instance()-once-then-cache-the-pointer pattern to avoid __cxa_guard overhead in interrupt context.
Tier 0 — tooling (do first, unblocks the ratchet)
- lint: wire SingletonElisionChecker into bash lint (warn-only) #3482 — Wire
SingletonElisionCheckerintobash lint(warn-only) - lint: split integer-constant globals into PreferConstexprChecker #3483 — Split integer-constant globals into
PreferConstexprChecker
Tier 1 — mechanical migrations (safe, ~2 days total)
- singleton: migrate fl/stl/allocator.cpp.hpp state (Tier 1a) #3484 —
fl/stl/allocator.cpp.hpp→ Singleton (1a) - singleton: migrate fl/stl/{ios,istream,ostream} state (Tier 1b) #3485 —
fl/stl/{ios,istream,ostream}.cpp.hpp→ Singleton (1b) - singleton: migrate fl/audio/detector state (Tier 1c) #3486 —
fl/audio/detector/*mutable state → Singleton (1c) - singleton: migrate objectfled_diagnostics snapshot state (Tier 1d) #3487 —
objectfled_diagnosticssnapshot buffer → Singleton (1d) - singleton: sweep small (<=2-hit) files (Tier 1e) #3488 — Sweep small ≤2-hit files (1e)
Tier 2 — ISR-touching drivers (piggyback on other PRs)
- singleton: migrate i2s_esp32dev driver state — 19 hits, ISR pattern (Tier 2a) #3489 —
i2s_esp32dev.cpp.hppdriver state (motivating case, 19 hits, 2a) - singleton: migrate flexio_driver state (Tier 2b) #3490 —
flexio_driver.cpp.hppdriver state (7 hits, 2b) - singleton: migrate remaining ISR-touching drivers — LPUART, RMT5 peripheral, LCD_CAM, MCPWM (Tier 2c) #3491 — Remaining ISR-touching drivers: LPUART, RMT5 peripheral, LCD_CAM, MCPWM (2c)
Tier 3 — ratchet closure
- lint: promote SingletonElisionChecker to hard-fail (Tier 3, ratchet) #3492 — Promote
SingletonElisionCheckerto hard-fail (after Tier 1+2 done) - lint: full libclang-based Rust AST subsystem for structural lints (deferred) #3493 — Full libclang-based Rust AST subsystem for structural lints (deferred)
Untouchable (out of scope)
FastLEDglobal instance insrc/FastLED.cpp.hpp— public API surface every sketch calls, must stay asCFastLED FastLED- Stub/WASM host-only entry points — only compiled in host-test builds, not on-device bloat
fl::Singleton<T>itself and its registry backing
Reference
- Scanner:
ci/scan_singleton_elision.py - Full violation dump:
singleton_elision_violations.json(locally) - Rust checker skeleton:
ci/lint_cpp_rs/src/checkers/singleton_elision.rs(not yet registered — lint: wire SingletonElisionChecker into bash lint (warn-only) #3482) - Pattern reference:
src/fl/stl/singleton.h - ISR-cache pattern reference: see singleton: migrate i2s_esp32dev driver state — 19 hits, ISR pattern (Tier 2a) #3489 for the canonical form Tier 2 issues follow
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
feat(lint): SingletonElisionChecker — land + drive to zero violations (…
singleton: sweep the safe <=2-hit files (Tier 1e)
singleton: sweep the safe <=2-hit files (Tier 1e) (#3756)