Repository navigation
Description
Context
The ESP32 PARLIO clockless encode (show()) is dominated by the Wave8 path. Measured on ESP32-P4, 16 lanes × 256 LEDs, PlatformIO -O2 (full investigation in #2522):
encode_us≈ 14.6 ms/frame; PSRAM is not the bottleneck (SRAM vs PSRAM = ×1.00 — the P4 cache hides it).- WS2812B_V5 timing isn't Wave3-eligible, so the driver uses Wave8 (8 ticks/bit → 98,880 DMA bytes/frame, 2.68× Wave3).
- On-device stage breakdown:
| stage | time/frame | share |
|---|---|---|
| wave8 16-lane transpose (isolated, 768 calls) | 6.6 ms | ~45% |
| gather + Wave8 LUT expansion + memcpy (remainder) | ~8.0 ms | ~55% |
So even a free transpose caps out at ~2× on the encode — the volume (ticks/bit) is the bigger structural cost.
Tried and rejected: SWAR bit-matrix transpose
Replaced the naive bit-by-bit wave8_transpose_16 (((lX>>b)&1)<<pos | …, ~31 ops/output byte) with an 8×8 SWAR delta-swap transpose (Hacker's Delight, u64). Correctness verified bit-identical (host unit test + on-device mismatch=0). But it regressed:
| target | naive | SWAR | result |
|---|---|---|---|
| x86_64 host | 62 ns/call | 69 ns/call | 0.90× |
| ESP32-P4 (RV32) | 6.6 ms/frame | 16.2 ms/frame | 0.41× — 2.4× slower |
Why: u64 delta-swaps (shift-by-7/14/28 crossing the 32-bit word boundary) explode into multi-instruction sequences on the 32-bit in-order RISC-V core, while the naive code is independent single-bit ops that clang schedules well. Do not pursue u64 SWAR.
Options to make the Wave8 encode faster (menu)
Roughly ordered by expected payoff / confidence:
- Reduce ticks-per-bit — WaveN (PARLIO clockless: encode/DMA is Wave8-bound — add intermediate WaveN (e.g. Wave4) to halve encode time + DMA RAM #2523). Highest-value, lowest-risk. WS2812B_V5 needs only 4 ticks (Wave4), not 8 → halves the transpose, expansion, memcpy, and DMA RAM (~2× overall). Needs RX-loopback signal validation.
- Combined expand+transpose LUT. Skip the intermediate
Wave8Byte: a LUT mapping source byte → already-transposed bit-plane contributions, accumulated per pulse. Removes the separate expansion pass (~part of the 55%) and may shrink the transpose. - Optimize the LUT expansion (~55% of encode). The Wave8 nibble-LUT expansion runs 16×768 times/frame; profile it in isolation and tighten (wider lookups, fewer stores, better inlining).
- Dual-core encode. ESP32-P4 is dual-core — encode the next frame on core 1 while core 0 drives DMA. Pairs with perf(parlio): umbrella for ESP32-P4 PARLIO output and show timing work #2518.
- ISR stream-encoding (perf(parlio): umbrella for ESP32-P4 PARLIO output and show timing work #2518). Don't reduce encode time — hide it behind the transmission / 20 µs DMA gap by encoding the next ring chunk in the TX ISR. Complementary to all of the above.
- Dirty-frame caching. Skip re-encoding lanes/pixels unchanged since the last
show()(dirty tracking); big win for mostly-static content, zero for full-motion. - Genuinely unroll the transpose symbol loop. The current "fully unrolled" comment is inaccurate — the outer 8-symbol loop is a real loop. Unroll it, add
__restrict, verify IRAM placement actually applies, align I/O buffers. - u32-friendly bit transpose. A 32-bit SWAR / byte-shuffle that avoids the RV32 u64 penalty. Low confidence (x86 host also lost with native u64 — delta-swap is latency-bound vs the naive), but the only transpose-algorithm avenue left.
- ESP32-P4 SIMD/PIE extensions. Investigate whether the P4's vector/AI instruction extensions expose a usable byte-shuffle / bit-gather for the transpose.
Notes
- Investigation, micro-benchmark (
benchMem), and the SWAR before/after are in perf(parlio): investigate why wave3 encode measures ~6µs/LED (LUT vs bit-swizzle vs gather vs logging) #2522. WaveN is PARLIO clockless: encode/DMA is Wave8-bound — add intermediate WaveN (e.g. Wave4) to halve encode time + DMA RAM #2523. - The SWAR experiment code is being reverted (it's a confirmed regression); this issue preserves the finding so it isn't re-attempted blindly.
Activity
zackees commentedon May 29, 2026
Options → sub-issues
Each optimization path above is now tracked as a sub-issue of this one:
Recommended starting point: #2523 (WaveN) — biggest, lowest-risk win (~2× the whole encode + ~2× less DMA RAM). The SWAR u64 transpose was tried and rejected (2.4× slower on RV32); its experiment code has been reverted, the finding is preserved here.
zackees commentedon May 29, 2026
Transpose-algorithm result: unrolled naive is fastest (8-lane and 16-lane)
Implemented and benchmarked the two-pass 4→8→16 decomposition (16-lane = two 8×8 transposes; each 8×8 = two u32 halves merged) plus the existing u32 Hacker's-Delight 8×8, all no-SIMD, register-resident. Fair same-build A/B on ESP32-P4 (RV32), all FORCE_INLINE + O3, 768 calls/frame:
The fully-unrolled naive bit-by-bit transpose is fastest for both on the in-order RV32 (and on x86, 0.49× for the two-pass). The compiler schedules the independent shift/or ops better than the latency-bound delta-swap dependency chain. Bit-exact correctness verified on host across edge + 400 random rounds (new
tests/fl/channels/wave3.cpp+wave8.cpp).Shipped (commit on
feat/parlio-pingpong-overlap):wave8/wave3transpose_8: HD → unrolled naive (~1.4× faster on RV32).transpose_16: kept unrolled naive (the two-pass regressed it; reverted).This is the third structured bit-transpose to lose to the unrolled naive (after the u64 SWAR in this issue) — so #2530 (u32-friendly transpose) is effectively answered: a u32 two-pass was tried and is slower. The encode's real lever remains tick-count reduction (#2523).
perf(wave): use fastest (unrolled-naive) 8/16-lane transpose; add equ…
zackees commentedon May 31, 2026
Closing as informational/decision-log. Sub-options now tracked in their own issues (#2525, #2527, #2528, #2529, #2530, #2531). U32 two-pass also evaluated and rejected via #2530.