Repository navigation
Description
Goal
Find a "massive" (≥2×) speedup for the PARLIO bit-matrix transpose (wave8Transpose_8/_16, wave3Transpose_8/_16), called ~768×/frame (16×256). Brainstorm any approach, research online, benchmark on a real ESP32-P4.
TL;DR
The unrolled-naive bit-by-bit transpose is near-optimal on the ESP32-P4 (in-order RV32). Five algorithm classes were evaluated; every structured alternative is slower, for one root reason: structured bit-tricks are dependency chains (or use emulated u64) that stall an in-order single-issue core, while the naive's independent shift/or ops pipeline well. A transpose-algorithm change will not yield ≥2×. The real ≥2× levers are WaveN (#2523, ~2× fewer ticks→fewer transposes) and dual-core encode (#2527, ~1.8×) — pursue those.
ISA probe (gates the most promising HW path)
ESP32-P4 build -march = rv32imafc_zicsr_zifencei_xesppie.
- No
Zbb/Zbkb/Zba/Zbs→ the RISC-V bit-manipzip/unzip(bit interleave — ideal for transpose),brev8,packare unavailable (would be illegal instructions). The single biggest potential HW accelerator is not present. xesppie= Espressif's custom 128-bit PIE/SIMD. It exists but is not exposed by the toolchain/FastLED (fl::simdRISC-V backend is scalar-only); using it needs hand-written inline asm (out of scope here).
Measured on ESP32-P4 (RV32, 360 MHz, fair same-build A/B, FORCE_INLINE+IRAM+O3, 768 calls/frame)
| Approach | 16-lane µs | 8-lane µs | vs naive | status |
|---|---|---|---|---|
| Unrolled naive (current prod) | 6595 | 3336 | 1.00× | measured ✅ |
| u32 Hacker's-Delight 8×8 | (two-pass below) | 4680 | 0.71× (slower) | measured ✅ |
| Two-pass (16 = two 8×8 HD u32) | 10524 | — | 0.63× (slower) | measured ✅ |
| u64 SWAR delta-swap | 16208 | — | 0.41× (slower) | measured ✅ |
End-to-end encode_us (full show() encode, naive in prod): 4-lane 3650 µs · 8-lane 7208 µs · 16-lane 14611 µs (Wave8, use_wave3=0). The 8-lane dropped from 8183→7208 µs (1.14×) when _8 switched HD→naive (#2532).
Candidate menu (brainstorm) + verdict
| # | Approach | Verdict | Why |
|---|---|---|---|
| 0 | Unrolled naive | winner | independent ops, no dep chain, no u64 |
| A | Fuse expand+transpose (skip the Wave8Byte buffer) | projected ~neutral | the small per-call Wave8Byte[16] local is already register/stack-resident under O3+inline; the transpose ops dominate, not the buffer round-trip |
| C | Spread-and-OR column LUT (extend kTranspose4_16_LUT to 8/16) |
projected slower | 128-byte output assembled by OR-ing 16 per-lane contributions = far more memory traffic than the naive's ~independent bit ops |
| D | RISC-V Zbb/Zbkb zip/unzip/brev8 |
dead | not in the P4 -march (probe above) |
| E | ESP32-P4 PIE/SIMD (custom 128-bit) | deferred | not exposed; needs inline asm (out of scope) |
| F | Restructure upstream layout so no transpose is needed | open / bigger refactor | only path that eliminates the op; needs channel-manager/DMA-format changes (separate from the transpose kernel) |
| H | Dual-core split | promising (~1.8×) | orthogonal, multiplicative — tracked in #2527 |
| I | transposition.h transpose_wave8byte_parlio_template<N> |
projected slower | uses u64 packing (same emulated-u64 failure mode as the measured SWAR, 2.4× slower) |
| — | 64-bit-multiply transpose8; monolithic delta-swap | dead | no fast u64 mul on RV32; dependency chain |
Projected entries are reasoned, not yet device-measured — happy to confirm any on the P4 if wanted, but the in-order-RV32 dependency-chain / emulated-u64 / memory-traffic reasoning predicts they lose, consistent with the 4 measured negatives.
Conclusion & recommendation
- Do not pursue a faster transpose algorithm — the naive is at the efficiency frontier for this op on the P4, and the one HW accelerator that could change that (Zbb/Zbkb) isn't in the silicon.
- For the massive win, combine:
- WaveN (PARLIO clockless: encode/DMA is Wave8-bound — add intermediate WaveN (e.g. Wave4) to halve encode time + DMA RAM #2523) — Wave4 instead of Wave8 ≈ 2× fewer symbols → ~2× less transpose + expansion + DMA. Biggest lever; needs RX signal validation.
- Dual-core encode (Wave8 opt: dual-core encode on ESP32-P4 #2527) — ~1.8×, multiplicative with WaveN.
- Optional smaller item: layout restructure (F) to elide the transpose entirely — larger refactor, separate investigation.
Online research (sources)
- Hacker's Delight §7-3 (bit-matrix transpose), Andrei Gudkov "What it takes to transpose a matrix", dsnet/matrix-transpose, RISC-V scalar bit-manip (FPRox), riscv-crypto Zbkb (zip/unzip/brev8), Espressif ESP32-P4 product page.
Activity
zackees commentedon May 29, 2026
UPDATE — earlier "naive is near-optimal" conclusion was WRONG. A u32 spread-LUT is ~2× faster (bit-correct).
I projected the LUT approaches as dead-ends; that was a mistake. Benchmarked empirically on the real ESP32-P4 and the u32 "spread LUT" beats the naive ~2× and is bit-exact:
Why it wins (and why it's different from the dead-ends)
acc = OR over 8 lanes of spreadLUT[laneByte] << lane, wherespreadLUT[v]is a precomputed u32 that pre-positions the 8 pulse bits ofvinto 8 separate bytes. It's native u32, all-independent ops, no dependency chain, no emulated u64, and the 2 KB table stays cache-resident. That's exactly the op profile the in-order RV32 schedules best — unlike the HD delta-swap / two-pass / u64-SWAR (dependency chains) and the u64-pack template (emulated u64), which all lost. The lesson: the winning shape is independent table-lookup + shift + OR-reduce, not bit-twiddling.New tooling:
bash autoresearch esp32p4 --transpose 16(or 8)Added a
--transpose Nautoresearch flag + abenchTransposedevice RPC that A/Bs naive vs spread-LUT on-chip with a bit-exact gate, so this is now a one-command, repeatable benchmark. (commit onfeat/parlio-pingpong-overlap.)ESP32-P4 PIE (xesppie) SIMD — explored
esp.vzip.8,esp.vunzip.16,esp.andq…) but there is no bit-movemask / bit-extract, the key primitive a SIMD bit-transpose needs.esp.vld.128.ip/esp.vst.128.ipinline asm assembles and executes correctly (a 128-bit load→store smoke test passed,pie_smoke=1). So the load/store foundation works; a full PIE transpose would require reverse-engineering a bit-extract instruction — not worthwhile vs the spread-LUT's clean ~2×.Next
Ship the spread-LUT into the production wave8/wave3
transpose_8/_16(with a C++11-valid table), RX-validate the WS2812 signal, and merge. This stacks with WaveN (#2523) and dual-core (#2527).perf(wave): spread-LUT transpose — ~2x transpose, ~1.24x encode on ES…
zackees commentedon May 29, 2026
✅ Shipped — PR #2534 merged to master
The spread-LUT transpose is now the production wave8/wave3 8- and 16-lane transpose.
End-to-end on the ESP32-P4 (RV32), bit-exact:
encode_us(256 LEDs)Correctness: bit-identical to the naive (host equivalence + known-answer + round-trip tests, on-device
mismatch=0). Since the naive was the proven-correct production transpose, the WS2812 output is byte-for-byte unchanged — no signal regression possible.Tooling:
bash autoresearch esp32p4 --transpose 16(or 8) is the repeatable on-device A/B harness (committed on the investigation branch).Stacks with: WaveN (#2523, ~2× via fewer ticks) and dual-core (#2527, ~1.8×) — those are multiplicative with this transpose win.
Closing the loop: the earlier "transpose algorithm is near-optimal" call was wrong; empirically benchmarking the spread-LUT (independent table-lookup + shift + OR-reduce) found the ~2× that the dependency-chain/u64 candidates couldn't.
zackees commentedon May 29, 2026
Done. The exhaustive on-device benchmark is complete and the winner shipped: the spread-LUT transpose (#2534) — transpose 1.98×/1.90× (16/8-lane), full encode 1.24×/1.23×, bit-exact on all P4 revisions. Confirmed dead-ends: structured SWAR/HD bit-tricks lose on in-order RV32, and PIE can't do a bit-transpose (#2535). The ≥2× encode path (now expansion-bound, not transpose-bound) is tracked separately in #2523 (WaveN) and #2527 (dual-core).