Skip to content

singleton: migrate fl/audio/detector state (Tier 1c) #3486

Closed as not planned
Closed as not planned

Description

@zackees

Part of #3481 (Tier 1c).

Task

Migrate the mutable state (not the constants — those belong to #3483) in src/fl/audio/detector/* to fl::Singleton<T>.

Files touched

Approach

  1. Confirm which hits are true mutable state vs. compile-time constants (some equalizer/frequency_bands hits will vanish once lint: split integer-constant globals into PreferConstexprChecker #3483 lands; only the truly-mutable ones remain here).
  2. Wrap the remaining mutable state in a state struct per detector, access via fl::Singleton<DetectorState>::instance().
  3. bash test --cpp audio to exercise the audio pipeline.
  4. bash bloat esp32dev --examples Blink — confirm audio state is now elided from a non-audio sketch.

Acceptance criteria

  • Detector .cpp.hpp files clean of SingletonElisionChecker warnings.
  • Audio tests pass.
  • Elision proven — Blink flash size drops by the audio-state footprint after this lands.

Activity

  1. added
    type: feature
    priority: medium
    area: core
    on Jul 1, 2026
  2. zackees commented on Jul 28, 2026

    @zackees
    MemberAuthor

    Worked this in #3754, but the outcome is different from what the issue asks for: all 11 flagged sites in fl/audio/detector turned out to be false positives. I'd suggest not closing this as "migrated."

    Breakdown of the 11:

    • 9 were compile-time constants (kBassStart..kTrebleEnd, kNumBands, kFFTMinFreq, kFFTMaxFreq). Anonymous namespace, never address-taken, constant-folded to zero storage at -O1+. Nothing to migrate.
    • 2 were genuinely mutable (sVibeFFTCount, sFrequencyBandsFFTCount). I did implement the fl::Singleton wrap for these, then measured it and reverted it.

    Why the wrap was reverted:

    1. It saves nothing. ci/boards.py:795 already passes -ffunction-sections -fdata-sections, so a file-scope static int gets its own .bss section and --gc-sections already drops it when the detector isn't linked.
    2. It costs bytes when the detector is used. 4 B becomes aligned storage + a T* (8-12 B), and each of the six access sites gains a null-check plus a cold placement-new tail (~+100-140 B flash, Thumb-2 -Os).
    3. It introduces an init data race. Singleton<T>::instance() hand-rolls if (!ptr) { ptr = new (&storage) T(); }, which bypasses the C++11 magic-static guard. The plain static int = 0 is constant-initialized with no init window. On ESP32, Vibe::update() on the audio task and getPrivateFFTCount() from loop() can both first-touch, both placement-new, and the second construction silently re-zeroes the counter.

    The real bug was in the checker: its constexpr-triviality test used str::parse, and Rust's numeric parsers reject both the 0x/0b prefixes and the C++ u/l/f suffixes. ci/scan_singleton_elision.py already had a correct tokenizer (TRIVIAL_RHS_RE); #3754 ports it. Tree-wide violations drop 76 -> 39, all false positives — src/platforms/arm/lpc/rx_sct_capture.cpp.hpp alone had 41 hex register masks flagged and is now clean.

    Suggested re-scoping for the #3481 family

    The general rule this points to:

    fl::Singleton elision pays off for objects with dynamic initialization — those register .init_array entries that pull in the constructor and resist --gc-sections. It does nothing for constant-initialized PODs, which -fdata-sections already makes collectible, and for those it is a net size increase plus a loss of race-free init.

    If that holds up, the sibling issues targeting trivial scalars (parts of #3488's ≤2-hit sweep, and some hits inside #3487 / #3490 / #3491) want re-scoping too, and the checker arguably should stop flagging constant-initialized POD scalars outright. I did not make that policy change unilaterally in #3754 — it affects the whole program and seemed worth your call first.

    One more thing worth deciding separately: the checker's message tells authors that the rule "Applies to ... FL_PROGMEM data alike — put the value inside a class as a member." Moving an FL_PROGMEM/.rodata table into a Singleton<T> member relocates it from flash to RAM, since the aligned buffer is .bss populated by placement-new. On AVR that trades free flash for scarce SRAM. That guidance predates this PR and I left it alone.

  3. zackees commented on Jul 28, 2026

    @zackees
    MemberAuthor

    Closing as not-planned. #3754 is merged (6d18081f63), but it fixes the checker rather than performing this migration — the task as written should not be done.

    Recap of why:

    • 9 of the 11 hits were compile-time constants in anonymous namespaces, constant-folded to zero storage at -O1+. Nothing to migrate; they're now constexpr to state the intent.
    • 2 were genuinely mutable (sVibeFFTCount, sFrequencyBandsFFTCount). The fl::Singleton wrap was implemented, measured, and reverted: it saves 0 bytes (-fdata-sections already makes a file-scope static int collectible), costs ~+100–140 B flash and +4–8 B RAM when the detector is linked, and introduces an init data race because Singleton<T>::instance() hand-rolls if (!ptr) and bypasses the C++11 magic-static guard. Both now carry FL_LINT_ALLOW_GLOBAL with that rationale inline.

    The real defect was the checker's constexpr-triviality test using str::parse, which rejects 0x/0b prefixes and u/l/f suffixes. Tree-wide violations dropped 76 → 39, all false positives.

    Reopen if you'd rather re-scope this to the checker-policy change (skipping constant-initialized POD scalars outright) — I deliberately left that call to you, and I'm auditing the sibling issues under #3481 for the same pattern.

  4. moved this from Triage to Done in FastLED Trackeron Jul 28, 2026
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