Repository navigation
Description
Summary
A debug-build hardware measurement put PARLIO show() "encode" at ~25 ms for 16×256 (4096 LEDs) ≈ 6 µs/LED. Expected for a LUT-based wave3 encoder hot in I-cache is ~1000 LEDs/µs ≈ 0.001 µs/LED — a ~6000× discrepancy. A -Og debug build is only ~3–5× slower than -O2, so optimization level alone cannot explain it. We need to find where the time actually goes, and specifically whether the bottleneck is the LUT lookup, the bit-transpose/swizzle, the outer populate loop / per-lane gather, logging, or something that isn't encode at all (e.g. the per-frame peripheral disable()/enable()).
Context: measured via measureParlio on ESP32-P4 (16×256). Build was build_type=debug, -Og, -DFASTLED_DEBUG=1, -DCORE_DEBUG_LEVEL=5, -DLOG_LOCAL_LEVEL=ESP_LOG_VERBOSE. Refs #2518, PR #2521.
What the code already does (narrows the suspects)
- The wave3 inner functions (
wave3_convert_byte_to_wave3byte,wave3Transpose_{2,4,8,16}insrc/fl/channels/detail/wave3.hpp) are alreadyFASTLED_FORCE_INLINE FL_IRAM FL_OPTIMIZE_FUNCTION, andFL_OPTIMIZE_FUNCTION = __attribute__((optimize("O3")))on GCC. So the transpose/LUT are O3 + inlined + in IRAM even in a debug build — they should NOT be slow regardless of build type. - The caller
ParlioEngine::populateDmaBuffer()(src/platforms/esp/32/drivers/parlio/parlio_engine.cpp.hpp, ~L795–1050) is not force-optimized. Its per-byte-position loop does, per output byte: a per-lane gathermScratchBuffer[lane*mLaneStride + startByte + byteOffset](up to 16 reads), a stack temp, the transpose call, and amemcpyof the transposed bytes. At-Ogthis outer loop is unoptimized. - No
FL_LOG_*/ESP_LOG*were found in the inner transpose loop, but logging elsewhere inshow()/beginTransmission()atCORE_DEBUG_LEVEL=5is not yet ruled out.
Hypotheses (ranked)
| # | Hypothesis | Predicted signature |
|---|---|---|
| H1 | Outer populateDmaBuffer loop + per-lane gather at -Og dominates (strided scratch reads, no vectorization, memcpy per position) |
Release build collapses encode; transpose micro-bench already fast |
| H2 | The 25 ms is NOT encode — it's the per-frame disable()/enable() or other show() overhead/logging |
Sub-timers show encode small, other stage large |
| H3 | Verbose logging somewhere in the show() path (CORE_DEBUG_LEVEL=5) |
LOG_NONE build collapses the time |
| H4 | Bit-swizzle (transpose) genuinely expensive at 16 lanes | Transpose micro-bench dominates even at O3 |
| H5 | LUT lookups (cache/latency) dominate | LUT-only micro-bench ≈ full transpose time |
Investigation plan
Phase 0 — clean re-measure (in progress): rebuild -O2, CORE_DEBUG_LEVEL=0, LOG_NONE, no FASTLED_DEBUG; rerun measureParlio 16×256. Decision: encode → ~1–3 ms ⇒ debug/logging artifact (H1/H3); still ~20 ms ⇒ genuine encoder cost (H4/H5).
Phase 1 — attribute the time: add micros() sub-timers in beginTransmission() around (a) the populate/encode loop, (b) disable()/enable(), (c) transmit() submit. Report each via measureParlio. (H2)
Phase 2 — separate LUT vs swizzle vs gather (the core question): add a host/device micro-benchmark RPC that runs, on a fixed buffer, N iterations of:
- gather only — read the strided scratch bytes, no transpose.
- LUT only —
wave3_convert_byte_to_wave3byteper lane, no swizzle write-out. - full transpose —
wave3Transpose_16(LUT + swizzle). - memcpy only — the output copy.
Report ns/byte for each.full − LUT= swizzle cost;LUT − 0= LUT cost; gather/memcpy isolate the caller overhead. This directly answers "is the LUT or the bit-swizzle the bottleneck."
Phase 3 — fix the hot path: based on Phase 2, apply FL_OPTIMIZE_FUNCTION/FASTLED_FORCE_INLINE to populateDmaBuffer (or restructure the gather to be sequential/SIMD-friendly), strip any hot-path logging, and re-measure.
Phase 4 — only if encode ≪ wire: ISR stream-encode (encode chunk N+1 in the txDone ISR during chunk N's transmit). Blocked by (a) the mandatory per-frame disable()/enable() serialization and (b) the ~20µs inter-chunk seam — both must be solved (see #2518).
Phase 5 — validate: release build, FPS up, RX-loopback mismatchedLeds==0 (needs GPIO5→6 jumper).
Deliverable
A definitive ns/byte breakdown (gather / LUT / swizzle / memcpy / logging / peripheral-reset) and a concrete fix that gets encode to its expected ~1000 LEDs/µs, enabling the ISR-streaming overlap design.
Activity
zackees commentedon May 28, 2026
Phase 0 result — NOT a debug/logging artifact; likely memory-bandwidth (PSRAM) bound
Rebuilt release (
-O2,CORE_DEBUG_LEVEL=0,LOG_LOCAL_LEVEL=ESP_LOG_NONE, noFASTLED_DEBUG) and re-ranmeasureParlio16×256 on ESP32-P4:-Og, verbose log)-O2, no log)Only ~16% faster — decisively not a logging/opt artifact. Real cost ≈ 5 µs/LED (≈5000× over the ~1000 LEDs/µs target).
This rules H4/H5 (LUT/swizzle compute) DOWN and promotes a new lead
__attribute__((optimize("O3"))) + always_inline + FL_IRAM, so-O2cannot speed them and they are almost certainly not the bottleneck.alloc_psram_bytes≈340 KBconfirms PSRAM, internal SRAM delta only ~11 KB). Non-cacheable PSRAM scattered byte/word writes are bandwidth-bound and-O2-immune — this fits the 16% observation perfectly.disable()/enable()serialization, tracked in perf(parlio): umbrella for ESP32-P4 PARLIO output and show timing work #2518).New top hypothesis (H6): output write to non-cacheable PSRAM dominates
~36,864 output bytes per frame written into non-cacheable DMA PSRAM; at hundreds of ns per uncached write this alone is ~15–20 ms. Compute (LUT+swizzle, in IRAM/O3) is likely a small fraction.
Revised next steps
beginTransmissioninto encode-loop vsdisable()/enable()vs submit. Confirm the ~21 ms lives in the encode/populate loop (not the peripheral reset).If H6 holds, the fix is "encode to SRAM, DMA from SRAM (or DMA-copy to PSRAM)", not touching the LUT/transpose at all — and once encode is fast, the ISR-streaming overlap (#2518) becomes worthwhile.
zackees commentedon May 28, 2026
Phase 1 attribution — the ~21ms splits cleanly (sub-timers in
beginTransmission)measureParlio16×256, release build, baseline (PSRAM DMA, with per-frame reset), with newbegin_us/encode_us/reconfig_ustimers:reconfig_us(per-framedisable()+enable())encode_us(populate/encode loop)begin_us(wholebeginTransmission)show − begin(FastLED.show()/channel-manager layer)show_avgConclusions
disable()/enable()reset is NOT the cost (32 µs). This kills the earlier "peripheral-reset serializes frames" hypothesis from perf(parlio): umbrella for ESP32-P4 PARLIO output and show timing work #2518 — confirmed independently by the earlier no-reset A/B (show unchanged at 19ms).__attribute__((optimize("O3"))) + always_inline + FL_IRAM, so the slowness is in what wraps them insidepopulateDmaBuffer:mScratchBuffer[lane*mLaneStride + startByte + byteOffset]— 16 scattered reads per byte-position. ≈19 µs/position is far too slow for compute; this smells like uncached/strided reads from a PSRAM source buffer (new hypothesis H7).FastLED.show()/the channel manager (16 channels) — its own follow-up.Next (Phase 2 micro-benchmark, now well-targeted)
Add an RPC that, on a fixed frame, times in isolation: (1) source gather only, (2) LUT only, (3) full
wave3Transpose_16, (4) memcpy only — each with the source in SRAM vs PSRAM. That pins the 14.6 ms to gather-bandwidth vs swizzle-compute and tells us whether the fix is "stage the source in SRAM" vs "optimize the transpose/outer loop." Also profile the ~6 msshow()/manager overhead.Instrumentation lives on branch
feat/parlio-pingpong-overlap(debug timers +parlioDebugTimings()accessor +measureParliofields).zackees commentedon May 28, 2026
Experiment series to pinpoint the PARLIO encode slowness
Established so far (16×256, ESP32-P4, release
-O2, no logging):show()= 21 ms = encode ≈14.6 ms + ≈6 ms above the engine (FastLED.show()/channel-manager) + reconfig 0.03 ms (negligible). LUT/transpose are alreadyO3+inline+IRAM. Moving the DMA output to SRAM saved only ~2 ms ⇒ the cost is read/loop side, not output writes. Leading hypothesis: the source/scratch buffer is in PSRAM and the 16-lane strided gather pays scattered-PSRAM latency.Each experiment lists hypothesis → method → decision. E1–E2 pin the root cause; E3–E6 preview fixes; E7 chases the separate 6 ms; E8–E9 finalize/validate.
beginTransmission, reportesp_ptr_external_ram(mScratchBuffer)+ the addr range; same for the DMA buffer. RPC field.benchEncodeRPC: on a fixed frame, time N iters of each stage separately — (1) gather-only, (2) +LUT (wave3_convert_byte_to_wave3byte/lane), (3) +swizzle (wave3Transpose_16→ stack temp), (4) write-only (bulk store 48B/pos), (5) full. Parameterize src∈{SRAM,PSRAM} × dst∈{SRAM,PSRAM}. Report ns/byte.swizzle = full−LUT,LUT = (2)−(1), etc.measureParlio; compareencode_us.memcpyto the PSRAM DMA buffer; measureencode_us+ copy time.esp_cache_msyncMALLOC_CAP_DMApath) + writeback once before TX; measure.FL_OPTIMIZE_FUNCTIONtopopulateDmaBuffer(inner transpose already O3); measure with src/dst held in SRAM (isolate compute from memory).mScratchBuffer). If that scratch is PSRAM, the manager's scattered write of it likely IS the 6 ms — and the same buffer the engine then scatter-reads.async_memcpy(GDMA) for SRAM→PSRAM so the copy overlaps the next encode.encode_us+pipelined_per_frame_us; validate WS2812 output via RX-loopbackmismatchedLeds==0(needs GPIO5→6 jumper).Expected outcome / done definition
A ns/byte breakdown (gather / LUT / swizzle / write × SRAM/PSRAM) that names the dominant term, the location of both the source and DMA buffers, an explanation for the ~6 ms manager cost, and a validated fix bringing
encode_ustoward the ~1000 LEDs/µs target so ISR stream-encoding (#2518) becomes worthwhile.Instrumentation already on branch
feat/parlio-pingpong-overlap(sub-timers,parlioDebugTimings,pipelined_per_frame_us, release/no-log P4 build). E1/E2 build on the samemeasureParlio/RPC harness.zackees commentedon May 28, 2026
Smoking gun found: encode is Wave8-bound, not PSRAM-bound
Ran the
benchMemmicro-benchmark (E1/E2) + aparlioDebugConfigaccessor on the ESP32-P4 (16 lanes × 256 LEDs, PlatformIO-O2, no logging). Results overturn the PSRAM hypothesis.1. PSRAM is NOT the bottleneck (hypothesis refuted)
benchMemallocates source+dest buffers in both SRAM and PSRAM and times the encode building blocks:memcpy→ PSRAM (36864 B)The ESP32-P4 L2 cache fully hides PSRAM latency for this access pattern. The proposed "SRAM scratch + bulk copy to PSRAM" comes out at ×0.98 (slightly worse, due to the extra copy). Do not pursue the SRAM-scratch path.
2. The real cost: the driver uses Wave8, not Wave3
parlioDebugConfigreports the runtime config of the realshow():vs the isolated wave3 transpose of the identical 16×256 frame: 3641 µs.
So Wave8 costs 2.68× more bytes and its transpose is 1.49× less efficient per byte → 4.0× total. Memory (
alloc_psram) is also ~2.68× bigger than it needs to be.3. Why Wave8 and not Wave3 — and it's correct
mUseWave3 = canUseWave3(timing). For WS2812B_V5 (T1=225, T2=355, T3=645 ns, period 1225):3-tick resolution genuinely cannot separate this timing's
T0HfromT1H, so the fallback is not a bug. But Wave8 (8 ticks/bit) is the maximum — the driver jumps straight to it. At 4 ticks the two levels separate cleanly:Conclusion / next step
The encode is bound by ticks-per-bit (output volume), not memory. The win is to encode at the minimum tick count that represents the timing (Wave4 for V5) instead of defaulting to Wave8 — cutting encode time and DMA RAM ~2×, validated by RX loopback for signal integrity. Filing a dedicated fix issue.
zackees commentedon May 31, 2026
Closing as research complete. Wave3 ns/byte breakdown captured; resulting encode optimizations shipped via #2553/#2555/#2557/#2559.