Skip to content

Meta: migrate namespace-scope globals to fl::Singleton<T> for --gc-sections elision #3481

Closed

Description

@zackees

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/.bss per 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)

Tier 1 — mechanical migrations (safe, ~2 days total)

Tier 2 — ISR-touching drivers (piggyback on other PRs)

Tier 3 — ratchet closure

Untouchable (out of scope)

  • FastLED global instance in src/FastLED.cpp.hpp — public API surface every sketch calls, must stay as CFastLED 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

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: corepriority: mediumtype: feature

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions