Skip to content

PARLIO Wave8 encode optimization: SWAR transpose rejected (RV32 2.4x slower) — options menu #2524

Closed
9 of 9 issues completed

Description

@zackees

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:

  1. 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.
  2. 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.
  3. 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).
  4. 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.
  5. 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.
  6. 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.
  7. 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.
  8. 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.
  9. 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

Sub-issues

Sub-issues

9 of 9 Issues completed
Loading 9 sub-issues

Activity

  1. zackees commented on May 29, 2026

    @zackees
    MemberAuthor

    Options → sub-issues

    Each optimization path above is now tracked as a sub-issue of this one:

    # Option Sub-issue
    1 Reduce ticks-per-bit (WaveN) #2523
    2 Combined expand+transpose LUT #2525
    3 Optimize the Wave8 LUT expansion #2526
    4 Dual-core encode #2527
    5 ISR stream-encoding #2518
    6 Dirty-frame caching #2528
    7 Unroll transpose loop + micro-opts #2529
    8 u32-friendly bit transpose #2530
    9 ESP32-P4 SIMD/PIE extensions #2531

    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.

  2. zackees commented on May 29, 2026

    @zackees
    MemberAuthor

    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:

    Lanes unrolled naive structured winner
    16 6595 µs two-pass (u32 8×8) 10524 µs naive 1.6×
    8 3336 µs Hacker's-Delight u32 8×8 4680 µs naive 1.4×

    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/wave3 transpose_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).

  3. zackees commented on May 31, 2026

    @zackees
    MemberAuthor

    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.

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

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions