Skip to content

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

Closed

Description

@zackees

Phase 1 of #2815 — Tiered wait + NetworkDetector-gated yield

Tactical, no-API-change first phase of the channel-manager wait redesign. Eliminates the unconditional ≥1 ms FreeRTOS-tick floor by inserting a 250 µs micro-spin before the cooperator-friendly deep yield, and gates the deep yield on runtime radio activity via the existing fl::NetworkDetector.

Design rationale and the multi-strategy comparison live in #2815. This issue is the implementation spec.

Scope (one PR)

Files modified

  1. src/fl/channels/manager.cpp.hpp — replace the body of ChannelManager::waitForCondition (lines 364-388).
  2. src/fl/channels/driver.cpp.hpp — replace the body of IChannelDriver::waitForCondition (lines 11-35) with the identical tiered logic. Extract a shared helper (e.g. fl::detail::tieredWaitYield(elapsed_us) in src/fl/channels/detail/wait_yield.h) to keep them lock-step.
  3. src/platforms/esp/32/util/network_detector.{h,cpp.hpp} — promoted target for fl::NetworkDetector. Move from src/platforms/esp/32/drivers/rmt/rmt_5/network_detector.{h,cpp.hpp}. Leave a forwarding shim at the old path so RMT5-internal callers compile unchanged.
  4. src/FastLED.h — declare CFastLED::setWaitSpinBudgetUs(u32) per the Public Settings Pattern. New compile-time macro FASTLED_WAIT_SPIN_BUDGET_US (default 250).
  5. src/FastLED.cpp.hpp — implement setWaitSpinBudgetUs. Static storage for the runtime budget.

Files deleted / cleaned

  • The #if defined(FL_IS_ESP_32P4) carve-out in both wait sites is removed. On P4, NetworkDetector::isAnyNetworkActive() returns false via weak-symbol fallback (no WiFi/BT components linked), so the runtime gate selects the fast path automatically — same effect, single code path.

Tiered wait pseudocode

// Shared helper, called by both ChannelManager::waitForCondition and
// IChannelDriver::waitForCondition.
template<typename Condition>
bool tieredWait(Condition condition, u32 timeoutMs) {
    const u32 start_ms = millis();

    // Tier 1: instant non-blocking check (already-READY case).
    if (condition()) return true;

    // Tier 2: bounded microsecond spin — catches DMA tail without
    // paying the FreeRTOS tick floor.
    const u32 spin_budget_us = fl::detail::getWaitSpinBudgetUs();  // 250 default
    const u32 spin_start_us = fl::micros();
    while ((fl::micros() - spin_start_us) < spin_budget_us) {
        if (condition()) return true;
    }

    // Tier 3: cooperator-friendly deep yield. NetworkDetector gates this
    // at runtime — when no radio is up, drop the tick floor entirely.
#if FL_IS_ESP32
    const bool radio_up = fl::NetworkDetector::isAnyNetworkActive();
#else
    constexpr bool radio_up = false;  // bare-metal: no radio cooperator needed
#endif

    while (!condition()) {
        if (timeoutMs > 0 && (millis() - start_ms >= timeoutMs)) {
            FL_ERROR("waitForCondition timeout");
            return false;
        }
        if (radio_up) {
            task::run(250, task::ExecFlags::SYSTEM);  // vTaskDelay(>=1 tick) — keeps WiFi/lwIP cooperator alive
        } else {
            task::run(0, task::ExecFlags::SYSTEM);    // fl::yield() — no tick floor
        }
    }
    return true;
}

Public API surface

Per the project's Public Settings Pattern (CLAUDE.md → agents/docs/cpp-standards.md), the runtime knob lives on CFastLED:

// src/FastLED.h
class CFastLED {
public:
    // Set the micro-spin budget used by waitForCondition before falling
    // through to the cooperator yield. Default: FASTLED_WAIT_SPIN_BUDGET_US (250).
    void setWaitSpinBudgetUs(fl::u32 budget_us) FL_NOEXCEPT;
};

FASTLED_WAIT_SPIN_BUDGET_US may be #defined before #include <FastLED.h> to change the compile-time default.

Test plan

Host-side (tests/test_channel_wait_tiered.cpp, new file):

  • Drive tieredWait with a condition() lambda that flips true after a configurable simulated delay.
  • Assert: condition flipping at 100 µs → caught in Tier 2, returns within 200 µs.
  • Assert: condition flipping at 5 ms → reaches Tier 3, returns within 6 ms (no tick-floor in host build).
  • Assert: timeout path returns false after timeoutMs.

On-device (extension to examples/AutoResearch/AutoResearch.ino, no new .ino — project policy):

  • Add a --timing-drift test mode that mirrors the [Bug] Compounded timing inaccuracy on master compared to 3.10.3 #2420 reporter's sketch (35-LED WS2812B, paced sequence with target wall time).
  • Report per-frame FastLED.show() latency and total sequence wall time.
  • Run via bash autoresearch <board> --timing-drift.
  • Acceptance: sequence completes within ±10 ms of expected (2500 ms target → 2490–2510 ms).

Acceptance criteria

  1. ESP32-S3 single-strip, no WiFi.begin() → FastLED.show() for 35-LED WS2812B completes within ~1.2× transmit time (≤1.3 ms wall, vs current ~2 ms).
  2. ESP32-S3 with WiFi.begin() connected → no lwIP throughput regression vs current master (manual re-validation of the ESP32-S3: I2S driver starves websockets, SPI output incorrect, Audio driver FPS impact #2254 websocket scenario).
  3. ESP32-P4 PARLIO 12×256 → no regression vs current task::run(0) fast-path performance.
  4. The [Bug] Compounded timing inaccuracy on master compared to 3.10.3 #2420 reporter's sketch produces a 2500 ms sequence within ±10 ms.
  5. NetworkDetector builds and links correctly from src/platforms/esp/32/util/network_detector.h; old rmt/rmt_5/network_detector.h is a forwarding shim that still compiles.
  6. FL_IS_ESP_32P4 carve-out is removed from both waitForCondition sites.
  7. Teensy 4.x build: no regression; task::run(SYSTEM) falls through to yield() as today.
  8. AVR / RP2040 builds: no regression.

Out of scope (Phase 2+)

  • Per-driver waitDone() virtual and ISR-Take fast path → Phase 2.
  • Multi-driver fan-in (shared sem or task notification) → Phase 3.
  • Decoupled async show() → Phase 4.

Cross-references

Activity

  1. moved this from Triage to Done in FastLED Trackeron Jun 5, 2026
  2. added a commit that references this issue on Jun 5, 2026
    3bc0147
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

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions