Skip to content

perf(channels): add Tier-2 microsecond spin to waitForCondition (Phase 1 of #2815, Closes #2818) - #2827

Merged
zackees merged 1 commit into
masterfrom
feat/channels-tiered-spin-2818
Jun 5, 2026
Merged

zackees merged 1 commit into
masterfrom
feat/channels-tiered-spin-2818

Conversation

@zackees

@zackees zackees commented Jun 5, 2026 •

Copy link
Copy Markdown
Member

Summary

Phase 1 of #2815 / Closes #2818. Builds on the already-merged #2817 by inserting a Tier-2 microsecond spin between the instant check and the cooperator yield.

The combined wait in ChannelManager::waitForCondition and IChannelDriver::waitForCondition now has three tiers:

Tier What Origin
1 Instant non-blocking condition() check (free fast path) This PR
2 Bounded microsecond spin (FASTLED_WAIT_SPIN_BUDGET_US, default 250 us) This PR
3 NetworkDetector-gated cooperator yield (fast yield when no radio, vTaskDelay when radio active) #2817 (already merged)

Tier 2 catches short DMA tails - APA102 small strips and WS2812B <=8 LEDs complete entirely inside the spin without paying the cooperator yield's per-iteration overhead. For the #2420 reporter's 35-LED WS2812B sketch (~1.05 ms transmit), Tier 2 + #2817's no-radio fast yield eliminate the residual drift around the transmit boundary.

Public API (Public Settings Pattern on CFastLED)

// Compile-time default
#define FASTLED_WAIT_SPIN_BUDGET_US 250   // before #include <FastLED.h>

// Runtime tuning
FastLED.setWaitSpinBudgetUs(N);
fl::u32 cur = FastLED.getWaitSpinBudgetUs();

// Disable spin tier entirely (restores pre-#2818 behavior)
FastLED.setWaitSpinBudgetUs(0);

Files

File Change
src/fl/channels/detail/wait_spin_budget.h NEW - declarations for fl::detail::{get,set}WaitSpinBudgetUs
src/fl/channels/detail/wait_spin_budget.cpp.hpp NEW - atomic storage (fl::atomic<u32>), default 250 us
src/fl/channels/detail/_build.cpp.hpp wire new cpp.hpp into unity build
src/fl/channels/manager.cpp.hpp insert Tier 1 + Tier 2 before existing Tier 3 yield loop
src/fl/channels/driver.cpp.hpp mirror change
src/FastLED.h setWaitSpinBudgetUs(N) / getWaitSpinBudgetUs() setters on CFastLED via forward shims
src/FastLED.cpp.hpp shim implementations forwarding to fl::detail::{get,set}WaitSpinBudgetUs
src/fl/net/network_detector.h small lint fixes from #2817 (IWYU pragma blocks around platform headers, FL_NOEXCEPT on deleted special members)

Test plan

Host

Hardware (to follow / Phase 2)

Cross-references

Phase 2 (#2819) - waitDone() virtual + per-driver ISR fast path - is a separate follow-up PR.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Added public API methods to configure channel wait spin budget (setWaitSpinBudgetUs() and getWaitSpinBudgetUs()). Default is 250 µs; set to 0 to disable spinning.
  • Performance

    • Enhanced channel wait-loop responsiveness with tiered-wait strategy: immediate condition check, bounded microsecond spin phase, then adaptive yield loop for longer waits.

…e 1 of #2815, Closes #2818)

Builds on #2817 (runtime NetworkDetector-gated yield) by inserting a
bounded microsecond spin tier *before* the cooperator yield. The
combined wait now has three tiers:

  Tier 1 - instant non-blocking condition check (free fast path)
  Tier 2 - bounded microsecond spin (FASTLED_WAIT_SPIN_BUDGET_US, default 250 us) [NEW]
  Tier 3 - NetworkDetector-gated cooperator yield (already merged via #2817)

Tier 2 catches short DMA tails (APA102 small strips, WS2812B <=8 LEDs)
without paying even the no-radio fast yield's per-iteration overhead.
For the #2420 reporter's 35-LED WS2812B sketch (~1.05 ms transmit), the
new tier eliminates the residual drift around the transmit boundary.

Public API on CFastLED (per the Public Settings Pattern):
- setWaitSpinBudgetUs(N) / getWaitSpinBudgetUs() runtime setter.
- FASTLED_WAIT_SPIN_BUDGET_US compile-time default (250 us).
- Set to 0 to disable the spin tier entirely.

Implementation lives in fl/channels/detail/wait_spin_budget.{h,cpp.hpp}
with atomic storage (fl::atomic<u32>) so concurrent reads in the wait
loop do not race against a writer in setWaitSpinBudgetUs.

Also fixes a small lint regression from #2817's fl/net/network_detector.h
(IWYU pragma blocks around platform headers, FL_NOEXCEPT on deleted
special members).

Closes #2818. Refs #2815.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@coderabbitai

coderabbitai Bot commented Jun 5, 2026 •

Copy link
Copy Markdown

Linter diff in the way? Review this PR in Change Stack to focus on meaningful changes and expand context only when needed.

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

Run ID: 0ed13533-1595-4d56-b5a1-e18a7ffefce0

📥 Commits

Reviewing files that changed from the base of the PR and between f6140e9 and 3bc0147.

📒 Files selected for processing (8)
  • src/FastLED.cpp.hpp
  • src/FastLED.h
  • src/fl/channels/detail/_build.cpp.hpp
  • src/fl/channels/detail/wait_spin_budget.cpp.hpp
  • src/fl/channels/detail/wait_spin_budget.h
  • src/fl/channels/driver.cpp.hpp
  • src/fl/channels/manager.cpp.hpp
  • src/fl/net/network_detector.h

📝 Walkthrough

Walkthrough

This PR introduces a tiered-wait spin budget mechanism that allows channel-manager and driver wait loops to spend up to 250 µs (by default, configurable) spinning before entering the FreeRTOS cooperator yield tier. The feature reduces latency for short-duration waits without incurring the minimum ≥1 ms FreeRTOS tick floor.

Changes

Tiered-wait spin budget

Layer / File(s) Summary
Spin budget storage and detail API
src/fl/channels/detail/wait_spin_budget.h, src/fl/channels/detail/wait_spin_budget.cpp.hpp, src/fl/channels/detail/_build.cpp.hpp
Defines header with fl::detail::getWaitSpinBudgetUs() and fl::detail::setWaitSpinBudgetUs(fl::u32) function declarations, implements atomic storage with configurable default (250 µs), and integrates into Unity build.
CFastLED public API and forward shims
src/FastLED.h, src/FastLED.cpp.hpp
Exposes setWaitSpinBudgetUs(fl::u32) and getWaitSpinBudgetUs() const as public inline methods on CFastLED, with private static shims forwarding to the detail namespace implementation.
Driver wait-loop tiered-wait integration
src/fl/channels/driver.cpp.hpp
Adds Tier 1 instant condition check and Tier 2 microsecond spin phase controlled by the budget, preserving timeout enforcement and existing wait-loop fallback.
Manager wait-loop tiered-wait integration
src/fl/channels/manager.cpp.hpp
Adds identical Tier 1 instant check and Tier 2 spin phase to ChannelManager::waitForCondition before the existing adaptive-yield loop, with timeout enforcement during spin.
IWYU pragmas and exception specifications
src/fl/net/network_detector.h
Wraps ESP32+RMT5 real-implementation include with IWYU "begin_keep/end_keep" pragmas; updates stub NetworkDetector deleted special members to explicitly carry FL_NOEXCEPT.

Sequence Diagram

sequenceDiagram
  participant Caller
  participant WaitLoop
  participant SpinBudget
  participant YieldLoop
  Caller->>WaitLoop: waitForCondition(condition, timeoutMs)
  WaitLoop->>WaitLoop: Tier 1: instant check condition()
  alt condition satisfied immediately
    WaitLoop-->>Caller: return true
  else
    WaitLoop->>SpinBudget: getWaitSpinBudgetUs()
    SpinBudget-->>WaitLoop: budget_us (default 250)
    WaitLoop->>WaitLoop: Tier 2: spin up to budget_us
    alt condition satisfied in spin phase
      WaitLoop-->>Caller: return true
    else
      alt timeout exceeded in spin phase
        WaitLoop-->>Caller: return false (timeout error)
      else
        WaitLoop->>YieldLoop: Tier 3: existing yield/cooperator loop
        YieldLoop-->>Caller: return true or false
      end
    end
  end
Loading

Estimated Code Review Effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Possibly Related Issues

  • FastLED/FastLED#2818: This PR is the direct implementation of Phase 1 of the tiered-wait + NetworkDetector-gated yield specified in that issue, adding the spin budget API and integrating it into both wait sites.
  • FastLED/FastLED#2420: The spin budget directly addresses the timing regression reported in this issue by reducing latency before the FreeRTOS tick floor.
  • FastLED/FastLED#2815: This PR implements the tiered-wait strategy and public API design rationale discussed in this umbrella perf-optimization issue.

Possibly Related PRs

  • FastLED/FastLED#2503: Modifies the same waitForCondition paths in driver and manager to adjust per-iteration task::run() yielding behavior on ESP32-P4.
  • FastLED/FastLED#2817: Updates the same driver and manager wait-loop implementations to integrate NetworkDetector::isAnyNetworkActive()-gated yield logic.

Poem

🐰 A spin that's fast, a budget wise,
No tick-floor tax before our eyes—
Two tiers twirl, then yield takes flight,
Fast replies when conditions bite! ✨

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 23.08% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Out of Scope Changes check ❓ Inconclusive One unrelated change detected: network_detector.h stub modifications adding FL_NOEXCEPT to deleted special members fall outside the stated scope of adding tiered-wait spin and are lint/hygiene fixes. Confirm whether the network_detector.h exception specification updates align with the PR intent or should be separated into a distinct cleanup commit.
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately identifies the main change: adding a Tier-2 microsecond spin to waitForCondition as Phase 1 of #2815, and correctly references the closed issue #2818.
Linked Issues check ✅ Passed The code changes implement all primary coding objectives from #2818: tiered wait logic in manager.cpp.hpp and driver.cpp.hpp, wait_spin_budget storage and API, runtime CFastLED accessors, compile-time FASTLED_WAIT_SPIN_BUDGET_US macro, and NetworkDetector integration.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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 feat/channels-tiered-spin-2818

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 and usage tips.

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.

perf(channels): Phase 1 of #2815 - tiered wait + NetworkDetector-gated yield

1 participant