Skip to content

fix(parlio): size DMA ring and TX descriptors to the frame (#5161) - #5232

Merged
zackees merged 1 commit into
masterfrom
fix/5161-parlio-heap
Nov 9, 2026
Merged

zackees merged 1 commit into
masterfrom
fix/5161-parlio-heap

Conversation

@zackees

@zackees zackees commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

Summary

On ESP32-C6 a WS2812B strip is driven by PARLIO (priority 4, above RMT), not RMT, and PARLIO uses GDMA. Most of the first-show() heap reported in #5161 came from two oversized PARLIO allocations:

  1. The DMA ring was always 3 buffers, each sized for the whole frame. calculateRingBufferCapacity() sizes one buffer to hold the full frame whenever it fits (any strip below ~87 KB of encoded data per buffer), and calculateBufferByteCount() then always sends that frame from slot 0 as a single transfer. Slots 1 and 2 were allocated but never written. The engine now allocates one slot when the frame fits, and the full ring only when it streams. The grow path reallocates when the slot count goes up, even if the streaming capacity per buffer is smaller.
  2. The TX unit was created with max_transfer_size = 65534. IDF sizes its DMA descriptor lists from this value. The largest single transmit() is one ring buffer, so the clockless paths now pass the ring-buffer capacity, capped at 65534. Ring sizing moves ahead of parlio_new_tx_unit() so the value is known when the unit is created.

ChannelDriverPARLIO sizes the ring from a capped LED count (FL_ESP_PARLIO_MAX_LEDS_PER_CHANNEL, bytes/3). So beginTransmission() now checks the real per-lane frame against the same fit test that calculateBufferByteCount() uses. If the frame does not fit, it allocates the missing slots before encoding, and longer strips stream as before.

SPI mode keeps the 3-slot ring and the 65534 ceiling. Its sizing uses the wave8 calculator, so it should not rely on the single-buffer fit test.

Evidence

ESP32-C6 (no PSRAM), 12 x WS2812B on GPIO 5. Values are heap_caps_get_free_size(MALLOC_CAP_8BIT) deltas from the issue's probe sketch, in bytes:

build addLeds first show next 20 shows total
3.10.3 (RMT) 216 1240 0 1456
master (RED) 828 3668 24 4520
ring fix only 828 2868 24 3720
this PR (GREEN) 828 2484 16 3328

Per-allocation breakdown of master's first show(), from an instrumented build that hooks FastLED's allocator and walks frame pointers:

  • parlio_new_tx_unit() (IDF): 1720 B. This PR cuts it by about 384 B.
  • DMA ring: 3 x 384 B. This PR leaves 1 x 384 B.
  • FastLED objects: about 430 B (ChannelDriverPARLIO transmission state 152, ring-buffer holder 128, plus small vectors).

The addLeds 828 B (ChannelData 208, PARLIO driver 148, manager driver-list 72, callback 12, plus allocations that are not in FastLED) is unchanged.

Wire check: bash autoresearch esp32c6 --parlio: TEST PARLIO lanes=1 leds=100 PASS, RESULT PASS 2 test(s) (RX loopback through the normal LED API) on the final code.

Host tests: bash test parlio_driver (and --debug) pass. The new ringBufferCount test fails on RED (a mutant that always returns 3 makes it fail). The new engine tests cover three cases: growing from one slot to the streaming ring (max_transfer_size tracks the small frame), and a 40000-LED frame sent after sizing for 12 LEDs. A mutant that skips ensureRingSlotsFor() makes the test binary fail.

Limits

  • The total is still about 1.9 KB above 3.10.3. Most of the gap is the IDF PARLIO TX unit (about 1.3 KB after this PR) compared with 3.10.3's single RMT channel. Making C6 prefer RMT for short strips is a driver-priority policy change and is not in this PR.
  • The TX unit is still deleted and recreated on every show(). The existing "reinitialize for clean DMA state" path does this; it is heap-neutral but costs time. Not changed here.
  • Already true before this PR and not changed here: the ring capacity can exceed the 65534-byte max_transfer_size cap for very large streaming frames. On the PSRAM-fallback path the TX unit keeps the larger max_transfer_size (that only wastes heap).
  • The addLeds overhead (channel manager, ChannelData, driver singletons) is unchanged.

Part of #5161

Summary by CodeRabbit

  • New Features
    • PARLIO transmissions now adapt buffer usage to frame size, using a single buffer when the frame fits and additional buffers for larger frames.
    • Large frames can be streamed across buffer slots, including when they exceed the capacity available at initialization.
  • Bug Fixes
    • Transfers now respect the available buffer capacity and peripheral size limit, with a fallback to a smaller memory allocation when needed.

A frame that fits in one ring buffer is always sent from slot 0, yet the
engine allocated three full-frame DMA buffers and created the TX unit with
max_transfer_size=65534. Allocate one slot when the frame fits (growing to
the full ring on demand), and pass the ring capacity as max_transfer_size.
ESP32-C6, 12 WS2812B: first show 3668 -> 2484 B, total 4520 -> 3328 B.
@zackees
zackees enabled auto-merge November 9, 2026 08:51
@github-actions github-actions Bot added the sanitized Title, body and conversation screened; review text requires separate screening. label Nov 9, 2026
@fastled-project-sync fastled-project-sync Bot moved this to Triage in FastLED Tracker Nov 9, 2026
@coderabbitai

coderabbitai Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Explain Change →

Caution

Review failed

The pull request is closed.

Walkthrough

The PARLIO buffer calculator now selects one or three ring slots based on frame size and a 128-byte safety margin. The engine configures ring capacity and transfer limits from that sizing, and can add slots before transmitting larger frames.

Changes: PARLIO ring-buffer sizing and streaming

Layer / File(s) Summary
Ring sizing and initialization
src/platforms/esp/32/drivers/parlio/parlio_buffer_calc.h, src/platforms/esp/32/drivers/parlio/parlio_engine.h, src/platforms/esp/32/drivers/parlio/parlio_engine.cpp.hpp, tests/platforms/esp/32/drivers/parlio/parlio_driver.cpp
The calculator applies a 128-byte margin when selecting one slot or the full ring. Engine initialization sizes and allocates the configured slots, uses a capacity-based transfer limit capped at 65,534 bytes, and retains the full ring for SPI initialization. Tests cover slot selection near the fit boundary.
Transmission-time slot expansion
src/platforms/esp/32/drivers/parlio/parlio_engine.cpp.hpp, tests/platforms/esp/32/drivers/parlio/parlio_driver.cpp
Before transmission, the engine adds ring slots when the lane stride exceeds one-slot capacity. Allocation failure stops transmission with an error. Tests cover large streamed frames and frames larger than the initialization size.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant ParlioEngine
  participant ParlioBufferCalculator
  participant DMARing
  participant PARLIO
  ParlioEngine->>ParlioBufferCalculator: Calculate frame capacity and ring-buffer count
  ParlioBufferCalculator-->>ParlioEngine: Return capacity and ring-buffer count
  ParlioEngine->>DMARing: Allocate configured ring slots
  ParlioEngine->>PARLIO: Configure maximum transfer size
Loading
Merge Risk: Low · up to a1acf

The memory-saving change has bounded remaining concerns: allocation failure can leave a mixed frame incomplete, and the new streaming test accepts transfers real hardware rejects. Address failure cleanup, test enforcement, and the calculator’s repository-contract violations; the broader large-frame limitation is unchanged.

- [ ] Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@zackees
zackees added this pull request to the merge queue Nov 9, 2026
@github-actions github-actions Bot added sanitized Title, body and conversation screened; review text requires separate screening. and removed sanitized Title, body and conversation screened; review text requires separate screening. labels Nov 9, 2026
@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Memory usage (this PR vs. its merge base)

Change in examples/Blink per target. The left number is this PR's image flash / attributed RAM in bytes; the colored delta is the change from the merge base (red ↑ is larger, green ↓ is smaller, Same is unchanged).

Target FLASH [bytes] RAM [bytes]
attiny85 2,460 (Same) 136 (Same)
esp32dev 287,795 (Same) 18,747 (Same)
esp32s3 320,539 (Same) 22,499 (Same)

@github-actions github-actions Bot added sanitized Title, body and conversation screened; review text requires separate screening. and removed sanitized Title, body and conversation screened; review text requires separate screening. labels Nov 9, 2026
Merged via the queue into master with commit 30e66a2 Nov 9, 2026
134 of 135 checks passed
@fastled-project-sync fastled-project-sync Bot moved this from Triage to Done in FastLED Tracker Nov 9, 2026
@github-actions github-actions Bot added security: unscreened-content and removed sanitized Title, body and conversation screened; review text requires separate screening. labels Nov 9, 2026
@zackees
zackees deleted the fix/5161-parlio-heap branch November 17, 2026 07:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant