Skip to content

perf(esp32): measure and cut instruction-cache stalls on the show() hot path (IPC ~0.41 on C6) (#5375) #5422

Not planned

Description

@zackees

Sub-issue of #5375 (meta), part of #5356. Found while measuring #5382 (PR #5419).

Problem

On the ESP32-C6, the legacy RMT show() path retires about 9,870 instructions in about 24,250 real CPU cycles (counter-off runs read with esp_cpu_get_cycle_count, 35 LEDs, 160 MHz). That is an IPC of about 0.41, which is low for this single-issue in-order RV32 core; tight in-cache code runs closer to 1.

#5382 is direct evidence that the time goes to fetch stalls, not to executed work. The change removed 6.4% of instructions (10,542 → 9,870), yet real cycles rose 9.5% (22,156 → 24,256). Mean show() went from 137.4 to 149.7 µs. The result reproduced to within 0.1 µs over two alternating BEFORE/AFTER pairs (table in #5419).

The pattern of fewer instructions, more cycles and determinism is what flash-XIP instruction-cache misses look like:

  • the code moved, so its cache-line or MMU-page footprint changed;
  • the frame's hot path (CFastLED::show → SlimBridgeController encode → ChannelManager enqueue/show/poll → ChannelEngineRMTImpl::beginTransmission → rmt_transmit) runs from flash;
  • only the ISR callbacks and the RMT encoder are FL_IRAM.

Fix

  1. Measure first. Count cache misses on the C6 to confirm or rule this out.
    • Option: an AutoResearch mode that runs show() twice back to back and times the second, warm call against the first.
    • Option: count instruction-cache miss events with Espressif's PCER event bits, if the C6 exposes them.
    • Report cycles/show for a cold call vs a warm call.
  2. If the misses are real: place the per-frame hot path (the functions above, which are small) in IRAM via FL_IRAM, or group them into one contiguous section so they share cache lines. Keep it within the C6/C3/H2 IRAM budget, and leave Xtensa and other platforms unaffected.
  3. Accept on real cycles per show() (and µs), since retired instructions won't change. Report the IRAM/flash cost from bash bloat esp32c6. The ESP32-S3 Blink image ratchet must not grow, unless the S3 gains too and that is claimed explicitly.

Notes

Activity

  1. added
    sanitized
    on Nov 22, 2026
  2. zackees commented on Nov 22, 2026

    @zackees
    MemberAuthor

    Step 1 measured: cold vs warm show() on the ESP32-C6. Mode: timingDriftTest coldWarm (PR #5426).

    Setup: legacy RMT, 35 LEDs (GRB), 160 MHz, serial 8C:BF:EA:CF:87:B4, firmware = master 2f24908a56 + #5426.

    Each pair runs as follows:

    1. Wait for ChannelManager READY.
    2. Empty the flash cache with the ROM call Cache_Invalidate_ICache_All() (the C6's one L1 cache serves both code and rodata from flash).
    3. Time show(): this is the cold sample.
    4. Wait for READY again, then time show(): this is the warm sample.

    Instructions come from fl::InstructionCounter. Cycles are real reads of the IDF cycle counter, taken in a separate counter-off run as in #5419. Each row is the mean of 64 pairs; there were 3 runs and the run-to-run spread was 0.3% or less.

    show(), legacy RMT, 35 LEDs Instructions Real cycles IPC Mean µs
    cold (cache just emptied) 14,108 156,482 0.09 970.2
    warm (immediately after) 8,866 19,796 0.45 122.5

    Same run, through the channel API (FastLED.add, GRB), alternated twice: cold 17,409 instructions, 206,648 cycles, IPC 0.08, 1,283.7 µs; warm 12,058 instructions, 36,655 cycles, IPC 0.33, 228.0 µs.

    On this build the latency gate (bash autoresearch esp32c6 --legacy --rmt) passes: 117.9 µs, 9,227 instr/show.

    What it shows

    • A truly cold show() costs about 8x a warm one: +136.7k cycles, +848 µs. Flash-XIP misses are real and large when the cache has been emptied. The cold call also retires about 5.2k more instructions. That's most likely the RMT refill/done ISR work that falls inside its much longer window, since the counter is per core rather than per thread.
    • Warm IPC is still 0.45, not about 1. The warm show() (19.8k cycles) matches the steady-state cycles/show of the fade-loop gate (18.9k estimated, IPC 0.49). So in normal operation, with show() every 5 ms, the hot path is already resident in the 32 KB cache. The low IPC perf(esp32): measure and cut instruction-cache stalls on the show() hot path (IPC ~0.41 on C6) (#5375) #5422 describes is not explained by instruction-fetch misses. This agrees with the esp-perf-counters skill note that moving the hot path to IRAM made no difference.
    • The C6's PCER has no cache-miss or fetch-stall event (bits 0-10 are cycles, instructions, load/jump hazards, idle, loads, stores, jumps, branches, taken branches, compressed), so the cold/warm gap is the fetch-cost measurement.

    Implication for step 2. IRAM placement of the hot path is not justified by steady-state data. It would only buy back the cold case: a first show() after a flash write, OTA or a large code sweep. That's a one-off of about 850 µs, not per frame. The roughly 11k warm stall cycles come from elsewhere. The likely candidates are uncached APB/peripheral register accesses in the RMT/GPIO/channel-manager path, load-use and taken-branch hazards, and critical sections. The next measurement that could justify a fix is PCER bits 2/3/5/9 (load hazards, jump hazards, loads, taken branches) on the warm show(), one event at a time per the skill's crash caveat, plus counting MMIO reads in beginTransmission/poll. I'd hold step 2 until that points at something.

    For #5378 (channel API, warm show(), 35 LEDs, alternated GRB/RGB/GRB/RGB; RGB is the identity order, which skips the reorder pass):

    Order Instructions, warm Cycles, warm Mean µs
    GRB 12,067 / 12,050 36,646 / 36,664 227.9 / 228.0
    RGB 11,477 / 11,477 35,926 / 35,932 223.4 / 223.4

    The remaining reorder pass costs 581 instructions (4.8% of channel-API show()), about 16.6 per pixel, and about 730 cycles (2%). The legacy path has no such pass. Posted on #5378 as well.

  3. zackees commented on Dec 3, 2026

    @zackees
    MemberAuthor

    Closing as measured/refuted per #5492 item #21: the step-1 cold vs warm benchmark above proved the hot path is already resident in the 32 KB L1 cache during steady-state show() (warm 122.5 µs / 0.45 IPC vs cold 970.2 µs / 0.09 IPC), so instruction-fetch misses do not explain steady-state IPC and IRAM placement is not justified.

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