Skip to content

perf: optimize runtime SPIBusManager software GPIO writer #5049

Closed

Description

@zackees

Context

Two source investigation rounds at 94dab9c for #5024; no builds or tests run. src/platforms/shared/spi_manager.cpp.hpp:192-200 dispatches SOFT_SPI to softwareSPIWrite. This is reachable through FASTLED_FORCE_SOFTWARE_SPI (:602-608) and hardware fallback (:1035). softwareSPIWrite(:1039-1075) constructs runtime Pin objects, branches once per data bit and calls hi/lo for each edge. src/platforms/generic_pin.h:73-80 implements runtime hi/lo with port read/modify/write. This independent runtime loop is not GenericSoftwareSPIOutput and receives none of its new pin policies. Stub/WASM deliberately compile the function to no-op.

Proposal

Resolve safe runtime GPIO write operations once per transfer and use branchless per-pin data updates for platforms with verified atomic/masked registers. Keep a conservative generic fallback for unknown hardware, preserve runtime pin selection and avoid port-snapshot corruption under interrupts. Do not assume all Pin backends have ESP-style W1TS/W1TC.

Acceptance criteria

  • RED -> GREEN tests invoke actual SPIBusManager register/initialize/transmit SOFT_SPI path, including forced and hardware-fallback selection, rather than only a helper. All 256 values, consecutive/zero-length transfers, MSB order, clock idle/final edge and unrelated GPIO interference are checked.
  • On actual supported ESP32/ESP8266/CI13XX configurations compile runtime path and save code generation proving selected optimized operations avoid per-bit dispatch/data branches and unsafe port RMW; test GPIO16, bank boundaries, invalid pins and conservative unknown backend fallback. Real upstream core builds are required; stub no-op success is not evidence.
  • Preserve/document timing and interrupt constraints; this path currently contains no programmable clock delay, so do not silently invent a rate guarantee. Any measured physical speedup requires AutoResearch/fbuild board evidence.
  • Restore the old runtime writer as a mutation: the focused operation/RMW assertion must fail and return GREEN after restoration. Flags/metadata-only edits must fail. Use shared exhaustive gate test: gate software SPI changes on exhaustive production-path and mutation evidence #5031.

Decisions

  • One child for this independent runtime implementation; generic template platform children cannot close it.
  • Duplicate exact metadata query softwareSPIWrite returned no issues. No unscreened text was ingested.

Open questions

Choose the smallest runtime backend operation resolver that preserves generic Pin compatibility. Next step is a focused production-transmit RED trace/disassembly; no physical speedup is asserted.

Activity

  1. added
    sanitized
    on Nov 5, 2026
  2. zackees commented on Nov 7, 2026

    @zackees
    MemberAuthor

    #5125 covers the ESP32/ESP8266 writer only, so this stays open. Still unmet: RED/GREEN through the real SPIBusManager path (softwareSPIWrite is a no-op on stub and WASM builds), the restore-old-writer mutation, a CI13XX build (no board entry), tests or saved code generation for GPIO16, bank boundaries, invalid pins and unknown-backend fallback, an unrelated-GPIO trace, and gate #5031. Pacing is tracked in #5124.

  3. zackees commented on Nov 22, 2026

    @zackees
    MemberAuthor

    Closing with an accepted limitation.

    Fix: #5125 (06335a6). The runtime software-SPI writer in SPIBusManager::softwareSPIWrite (src/platforms/shared/spi_manager.cpp.hpp:1041) resolves each pin once per transfer to the ESP32/ESP8266 atomic W1TS/W1TC registers (src/platforms/shared/spi_bitbang/runtime_software_spi.h:64-74). It then writes those registers directly instead of dispatching per bit through Pin, and leaves other pins untouched.

    Proven by: tests/platforms/shared/spi_bitbang/runtime_software_spi.cpp.

    Accepted limitations:

    • No failing-then-passing test through the real SPIBusManager path, because softwareSPIWrite is a no-op on stub and WASM builds. The restore-old-writer mutation is part of the same gap.
    • No CI13XX build: there is no CI13XX board entry in ci/boards.py.
    • No tests or saved codegen for GPIO16, bank boundaries, invalid pins and unknown-backend fallback, and no unrelated-GPIO trace. ESP8266 hardware for these is not attached here.
    • Pacing is tracked separately in SPIBusManager software SPI ignores requested_speed_hz #5124.

    The #5031 binding is replaced by a rescoped host-side gate, tracked in #5031.

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

    sanitized

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions