Skip to content

perf(channels): Phase 2 of #2815 - waitDone() virtual + per-driver ISR fast path #2819

Closed

Description

@zackees

Phase 2 of #2815 — waitDone() virtual + per-driver ISR fast path

Once Phase 1 (#2818) lands the tiered-spin safety net, Phase 2 collapses the single-driver wait path to a direct ISR-driven semaphore Take, bypassing both the spin tier and the deep yield entirely. Target: ≥83% of FastLED sketches (single-driver per examples/ survey) wake on ISR within ~µs of DMA completion, with no tick floor and no microsecond spin.

Design rationale and the multi-strategy comparison live in #2815.

Scope (one PR, depends on #2818)

IChannelDriver API additions

// src/fl/channels/driver.h
class IChannelDriver {
public:
    /// Block until this driver is READY, or timeoutMs elapses.
    /// Default delegates to waitForReady() — preserves existing slow loop
    /// for drivers that haven't overridden.
    virtual bool waitDone(u32 timeoutMs) FL_NOEXCEPT {
        return waitForReady(timeoutMs);
    }
};

Per-driver overrides (one-line thin wrappers for 5 of 7; ~20 LOC each for 2)

Driver Underlying primitive (already exists) File Effort
PARLIO parlio_tx_unit_wait_all_done(unit, ms) src/platforms/esp/32/drivers/parlio/parlio_peripheral_esp.cpp.hpp:334 thin wrapper
RMT5 rmt_tx_wait_all_done(channel, ms) src/platforms/esp/32/drivers/rmt/rmt_5/rmt5_peripheral_esp.cpp.hpp thin wrapper
LCD_SPI / LCD_CLOCKLESS xSemaphoreTake(mCompleteSem, ticks) src/platforms/esp/32/drivers/lcd_spi/lcd_spi_peripheral_esp.cpp.hpp:452 thin wrapper
I2S_SPI xSemaphoreTake(mCompleteSem, ticks) src/platforms/esp/32/drivers/i2s_spi/i2s_spi_peripheral_esp.cpp.hpp:556 thin wrapper
SPI clockless spi_device_get_trans_result(handle, &t, ms) src/platforms/esp/32/drivers/spi/spi_peripheral_esp.cpp.hpp:283 thin wrapper
I2S LCD_CAM currently polls mBusy with vTaskDelay(1) src/platforms/esp/32/drivers/i2s/i2s_lcd_cam_peripheral_esp.cpp.hpp:278 add binary sem, give-in-ISR, take-in-wait (~20 LOC)
LCD_RGB currently polls mBusy with task::run(250, SYSTEM) src/platforms/esp/32/drivers/lcd_cam/lcd_rgb_peripheral_esp.cpp.hpp:248 add binary sem, give-in-ISR, take-in-wait (~20 LOC)

Teensy / bare-metal overrides

Teensy drivers (ObjectFLED, FlexIO, OctoWS2811 path) already set a volatile completion flag in their DMA-done ISR. waitDone override is a busy-poll on that flag:

bool waitDone(u32 timeoutMs) FL_NOEXCEPT override {
    u32 start = millis();
    while (!mDmaComplete) {
        if (timeoutMs && (millis() - start) >= timeoutMs) return false;
        yield();
    }
    return true;
}

No FreeRTOS dependency. Future enhancement: replace yield() with a __WFE/__SEV block-until-interrupt for low-power (see #2815 "Future / orthogonal work").

Manager-side fast-path

// src/fl/channels/manager.cpp.hpp
bool ChannelManager::waitForReady(u32 timeoutMs) FL_NOEXCEPT {
    // Pre-pass: classify drivers via cheap poll().
    IChannelDriver* active[8];
    size_t n = 0;
    for (const auto& e : mDrivers) {
        if (!e.enabled) continue;
        auto s = e.driver->poll();
        if (s.state == DriverState::READY) continue;
        if (s.state == DriverState::ERROR) return false;
        if (n < 8) active[n++] = e.driver.get();
    }

    if (n == 0) return true;
    if (n == 1) return active[0]->waitDone(timeoutMs);  // FAST PATH — direct ISR-driven Take

    // N > 1 — fall through to Phase-1 tiered wait until Phase 3 lands.
    return waitForCondition(/* same as today */, timeoutMs);
}

Test plan

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

  • Mock IChannelDriver with a waitDone override that returns after a configurable delay.
  • Assert: 1 active mock → manager bypasses tiered loop, returns within mock's delay + 50 µs.
  • Assert: 2 active mocks → manager falls through to tiered wait, returns when both done.
  • Assert: default waitDone (no override) delegates to legacy waitForReady.

On-device (extend examples/AutoResearch/AutoResearch.ino):

  • --timing-drift mode from Phase 1 should improve further: single-driver 35-LED WS2812B FastLED.show() latency drops from "transmit + ε" to "transmit + ε (ISR-bounded)".
  • New --multi-driver mode: register two drivers (e.g. RMT5 + PARLIO on ESP32-S3 alternatives, or LCD_SPI + RMT5 on ESP32-S3), kick off concurrent transmits, verify aggregate wait still works via the tiered fallback.
  • Measure ESP32-S3 with WiFi connected: confirm websocket throughput unchanged from ESP32-S3: I2S driver starves websockets, SPI output incorrect, Audio driver FPS impact #2254 baseline.

Acceptance criteria

  1. IChannelDriver::waitDone(u32 timeoutMs) exists as a virtual; default delegates to waitForReady.
  2. All 7 ESP32 drivers override waitDone as semaphore-backed wait (or thin wrapper over IDF semaphore-backed primitives).
  3. Teensy 4 drivers (ObjectFLED at minimum) override waitDone as flag-polling.
  4. ChannelManager::waitForReady short-circuits to active[0]->waitDone(timeoutMs) when exactly one driver is BUSY.
  5. ESP32-S3 single-strip 35-LED WS2812B show() wall time ≤ 1.1 ms (Phase 1 acceptance was ≤ 1.3 ms — Phase 2 tightens by skipping the 250 µs spin).
  6. ESP32-S3 with WiFi connected → no regression vs Phase 1.
  7. Multi-driver case (N≥2) on ESP32-S3 falls through to Phase 1 tiered wait — no regression.
  8. Teensy 4 ObjectFLED: no regression; waitDone busy-poll matches current behavior.
  9. RMT4 path (legacy IDF v4 platforms): default waitDone delegates to existing waitForReady — no regression.

Out of scope (Phase 3+)

Cross-references

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions