Skip to content

singleton: sweep the safe <=2-hit files (Tier 1e, #3488) - #3756

Merged
zackees merged 1 commit into
masterfrom
chore/3488-singleton-sweep
Jul 29, 2026
Merged

zackees merged 1 commit into
masterfrom
chore/3488-singleton-sweep

Conversation

@zackees

@zackees zackees commented Jul 29, 2026 •

Copy link
Copy Markdown
Member

Part of #3481. Addresses the safe portion of #3488 — deliberately not all of it, see below.

Moves five namespace-scope variables behind Singleton<T> so --gc-sections can drop the storage together with its accessor when a sketch never reaches the code.

File Variable(s)
fl/test/fltest.cpp.hpp sCurrentTestSkipped + sSkipReason → SkipState
fl/sensors/pir.cpp.hpp g_counter → PirNameCounter
platforms/wasm/audio_input_wasm.cpp.hpp g_wasmAudioInput → holder
platforms/shared/coroutine_context.cpp.hpp sCoroutinePlatformInstance folded into the CoroutineGlobals singleton already in that file, rather than adding a second one

SingletonElisionChecker: 39 → 34.

Two behaviour notes

  • pir.cpp.hpp increments the counter at two sites, the second reachable only when the first returned non-zero. Both now hit the same singleton field, so the existing (slightly odd) numbering is preserved exactly rather than quietly normalised.
  • audio_input_wasm.cpp.hpp had its extern "C" JS callback reaching directly at fl::g_wasmAudioInput even though fl::wasm_get_audio_input() already existed. The callback now goes through that accessor.

Deliberately NOT migrated

These are the rest of the ≤2-hit set. Each is a worse candidate than it looks, and the reason matters: Singleton<T>::instance() lazily constructs behind an if (!ptr) check, so it is not free and not ISR-safe.

  • platform_time_lpc.cpp.hpp — g_millis is volatile and written by the SysTick ISR; g_systick_started likewise. Routing an ISR-updated counter through a lazily-constructing accessor adds a branch and a construction race. Belongs to the ISR tier (singleton: migrate remaining ISR-touching drivers — LPUART, RMT5 peripheral, LCD_CAM, MCPWM (Tier 2c) #3491).
  • avr/attiny/clockless_blocking.cpp.hpp — gTimeErrorAccum256ths sits in an ATtiny bit-banging path where the extra indirection risks the very timing the file exists to hold.
  • gpio_isr_rx/mcpwm_timer.cpp.hpp — MCPWM_TIMER_TAG is a const char* log tag. That's constant-splitting work (lint: split integer-constant globals into PreferConstexprChecker #3483), not Singleton work.
  • FastLED.cpp.hpp — pSmartMatrix is public API surface, explicitly out of scope per the issue.
  • asio/http/server.cpp.hpp — s_esp_request_queue / s_esp_httpd are FreeRTOS handles touched from the httpd task; same safety question as the LPC pair.

I'd rather leave #3488 partially open with these documented than migrate them and introduce an ISR race to close a lint count.

Validation

  • 357/357 on a clean build. Worth noting the fltest.cpp.hpp change is to the test framework itself, so the suite exercises the reset and read paths on all 357 cases.
  • wasm compile clean, with an #error negative control confirming audio_input_wasm.cpp.hpp is actually in that build (2 hits) — a green compile of a file that isn't compiled proves nothing.
  • bash lint green.

Not covered: skipTest()'s write path. The FL_SKIP test that would exercise it is #if 0 upstream, so those two assignments are compiled but never executed by CI. I left it disabled rather than flipping it on, since the surrounding comment says enabling it perturbs suite results.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved reliability of PIR sensor naming across repeated initialization.
    • Stabilized test skipping and result tracking.
    • Improved coroutine platform registration and fallback behavior.
    • Improved WASM audio input instance handling, including safe cleanup and sample delivery.

Part of #3481. Addresses the safe portion of #3488.

Moves five namespace-scope variables behind Singleton<T> so --gc-sections
can drop the storage together with its accessor when a sketch never reaches
the code:

- fl/test/fltest.cpp.hpp    sCurrentTestSkipped + sSkipReason -> SkipState
- fl/sensors/pir.cpp.hpp    g_counter -> PirNameCounter
- platforms/wasm/audio_input_wasm.cpp.hpp  g_wasmAudioInput -> holder
- platforms/shared/coroutine_context.cpp.hpp  sCoroutinePlatformInstance
  folded into the CoroutineGlobals singleton that already existed in that
  file, rather than adding a second one

SingletonElisionChecker: 39 -> 34.

Two behaviour notes:

- pir.cpp.hpp increments the counter at two sites, and the second increment
  is reachable only when the first returned non-zero. Both now hit the same
  singleton field, so the (slightly odd) existing numbering is preserved
  exactly rather than quietly normalised.
- audio_input_wasm.cpp.hpp had its extern "C" JS callback reaching directly
  at fl::g_wasmAudioInput even though fl::wasm_get_audio_input() already
  existed. The callback now goes through that accessor.

Deliberately NOT migrated, with reasons -- these are the rest of the <=2-hit
set, and each is a worse candidate than it looks:

- platform_time_lpc.cpp.hpp  g_millis (volatile, written by the SysTick
  ISR) and g_systick_started. Singleton<T>::instance() lazily constructs
  behind an `if (!ptr)` check, so routing an ISR-updated counter through it
  adds a branch and a construction race. Belongs to the ISR tier (#3491).
- avr/attiny/clockless_blocking.cpp.hpp  gTimeErrorAccum256ths sits in an
  ATtiny bit-banging path where the extra indirection risks the timing the
  file exists to hold.
- gpio_isr_rx/mcpwm_timer.cpp.hpp  MCPWM_TIMER_TAG is a const char* log tag,
  which is constant-splitting work (#3483), not Singleton work.
- FastLED.cpp.hpp  pSmartMatrix is public API surface, explicitly out of
  scope for this issue.
- asio/http/server.cpp.hpp  s_esp_request_queue / s_esp_httpd are FreeRTOS
  handles touched from the httpd task; same ISR/task-safety question as the
  LPC pair.

Validated: 357/357 on a clean build (the fltest change is the test framework
itself, so the suite exercises the reset and read paths on every one of the
357 cases); wasm compile clean, with an #error negative control confirming
audio_input_wasm.cpp.hpp is actually in that build rather than assuming a
green compile covered it; lint green.

Not covered: skipTest()'s write path. The FL_SKIP test that would exercise
it is #if 0 upstream, so the two assignments inside skipTest() are compiled
but never executed by CI. Left disabled rather than enabled here, since the
comment says enabling it perturbs suite results.

Co-Authored-By: Claude <[email protected]>
@coderabbitai

coderabbitai Bot commented Jul 29, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: d74272c1-e0af-44a0-8699-42bcf5af5c21

📥 Commits

Reviewing files that changed from the base of the PR and between 6d18081 and 79a0071.

📒 Files selected for processing (4)
  • src/fl/sensors/pir.cpp.hpp
  • src/fl/test/fltest.cpp.hpp
  • src/platforms/shared/coroutine_context.cpp.hpp
  • src/platforms/wasm/audio_input_wasm.cpp.hpp

📝 Walkthrough

Walkthrough

The pull request replaces several file-scope globals with fl::Singleton-backed state for PIR naming, test skipping, coroutine platform registration, and WASM audio input tracking.

Changes

Singleton-backed state migration

Layer / File(s) Summary
PIR naming and test skip state
src/fl/sensors/pir.cpp.hpp, src/fl/test/fltest.cpp.hpp
PIR naming counters and test skip state are stored through singleton-backed structures, with test execution and skip accounting updated accordingly.
Coroutine platform registration
src/platforms/shared/coroutine_context.cpp.hpp
The registered coroutine platform pointer is moved into CoroutineGlobals, while the null-platform fallback remains unchanged.
WASM audio input ownership
src/platforms/wasm/audio_input_wasm.cpp.hpp
WASM audio input construction, destruction, lookup, and sample forwarding use singleton-backed instance storage.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related issues

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly reflects the main change: migrating safe files to Singleton-backed storage.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch chore/3488-singleton-sweep

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@zackees
zackees merged commit 92697f9 into master Jul 29, 2026
13 checks passed
@fastled-project-sync fastled-project-sync Bot moved this from Triage to Done in FastLED Tracker Jul 29, 2026
@zackees
zackees deleted the chore/3488-singleton-sweep branch August 21, 2026 19:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant