Skip to content

perf(encode): bulk RGB encode for the channel API path, plus a channel-API show() measurement (#5399) - #5417

Merged
zackees merged 1 commit into
masterfrom
perf/5399-channel-bulk-rgb
Nov 22, 2026
Merged

zackees merged 1 commit into
masterfrom
perf/5399-channel-bulk-rgb

Conversation

@zackees

@zackees zackees commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Closes #5399. Refs #5375 / #5356.

Why

The channel API (FastLED.add() → Channel::encodeWS2812 → PixelIteratorAny → PixelIterator::writeWS2812()) still encoded plain RGB one pixel at a time. Each pixel took four type-erased calls (has, loadAndScaleRGB, stepDithering, advanceData) and three capacity-checked back_inserter appends.

What

  • PixelControllerVtable::loadScaledRGBBulk: one type-erased call per run of up to 32 pixels. It runs the per-pixel ScaledPixelIteratorRGB sequence (has, loadAndScaleRGB, stepDithering, advanceData) inline. It is bound through RawPremixBinder, so like loadRawBulk from perf(encode): bulk RGBW/RGBWW encode, types resolved once per strip (#5377) #5403 it is bound only for PixelController sources and not on AVR.
  • PixelIterator::loadScaledRGBBulk(out, max): the public wrapper. It applies the iterator's colour order over the run. Sources without the thunk (colour-managed, AVR) fall back to the per-pixel sequence.
  • writeWS2812() for RGB fills a 32-pixel stack run and appends it with one resize per run. RGBW/RGBWW (perf(encode): bulk RGBW/RGBWW encode, types resolved once per strip (#5377) #5403) are unchanged.
  • AutoResearch: timingDriftTest accepts "api": "channel", which registers the same 35-LED WS2812 strip (800 kHz, GRB) with FastLED.add() instead of addLeds. ci/autoresearch/test_timing_drift.py gains --api {legacy,channel} and prints instructions per show().

Equivalence test (written first; it passed on master)

tests/unit/fl/chipsets/encoders/pixel_iterator.hpp compares writeWS2812() with the per-pixel makeScaledPixelRangeRGB + encodeWS2812_RGB path byte for byte. It covers:

  • all 6 colour orders;
  • brightness 255, 128 and 37;
  • DISABLE_DITHER and BINARY_DITHER across dither frames;
  • lengths 1, 3, 32, 33 and 75;
  • both a PixelController<RGB, 1, 0xFFFFFFFF> source (the channel API's) and a PixelController<GRB> source;
  • appending after existing bytes.

A second case goes through PixelIteratorAny as the channel does. A deliberately broken permutation makes it fail.

Measurement (ESP32-C6, 160 MHz, 35 LEDs, RMT, fl::InstructionCounter)

Each column is timingDriftTest, run on a separate build of the same AutoResearch firmware. The only difference between the builds is pixel_iterator.h.

Path master this PR
Channel API, instructions per show() 26,878 15,951 / 15,945 / 15,972 (−41%)
Channel API, mean show() 297.7 µs 264–269 µs
Legacy RGB, instructions per show() (timingDrift) 12,845 / 12,833 / 12,834 13,655 / 13,638 / 13,637
Legacy RGB latency gate (bash autoresearch esp32c6 --legacy --rmt) 12,436 instr, 160.8 µs 12,840 / 12,827 instr, 220.5 µs (PASS, budget 280 µs)

Unexplained legacy delta. The legacy addLeds path doesn't reach any changed code: SlimBridgeController encodes directly and never constructs a PixelIterator. Even so, the legacy count rose by about 400–800 instructions and the wall time by about 60 µs. This reproduced across two separate flashes of this branch, and the cycles per show() rose from 25.7k to 35.3k. It looks like a code-layout effect. One possibility is that the counter includes ISR work, such as RMT refill interrupts, that falls inside a longer show() window. I haven't verified that, so please treat it as an open question rather than a result.

Validation

  • bash test encoders and bash test encoders --debug pass.
  • bash test --cpp: 377/377 unit and 97/97 examples pass.
  • bash lint passes.
  • bash autoresearch esp32c6 --legacy --rmt passes, latency gate included.
  • I didn't run bash bloat esp32s3 --build locally. Blink takes the legacy addLeds path, which doesn't construct a PixelIterator, so the new thunk isn't linked there. Hosted bloat CI is the check.

Follow-up (not in this PR)

The other ScaledPixelIteratorRGB encoders (APA102, SK9822, TM1908, UCS7604, HD108) still load per pixel. Making ScaledPixelIteratorRGB refill in runs needs a run buffer inside an iterator that is copied by value as a begin/end pair, and it changes how far ahead the shared cursor moves relative to the brightness adapter. That isn't clean enough to fold in here. These encoders can call PixelIterator::loadScaledRGBBulk() directly when they are measured.

Summary by CodeRabbit

  • New Features
    • Timing-drift measurements can now use either the legacy or channel-based API. Results identify the API used and may include instruction counts per show.
  • Improvements
    • RGB LED output is processed in batches, with existing color-order, brightness, and dithering behavior preserved.

…l-API show() measurement (#5399)

PixelIterator::writeWS2812() for plain RGB now loads, scales, dithers and
advances a run of up to 32 pixels in one type-erased call
(loadScaledRGBBulk) and applies the colour order over the run, instead of
four type-erased calls and three capacity-checked appends per pixel.
Byte-identical to the per-pixel ScaledPixelIteratorRGB path; a new host
test compares the two across all six colour orders, three brightness
scales, dithering on and off, and odd lengths.

AutoResearch timingDriftTest takes "api": "channel" to register the same
35-LED strip with FastLED.add(), and the host driver gains --api and
prints instructions per show().
@zackees
zackees enabled auto-merge November 22, 2026 06:11
@zackees

zackees commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Explain Change →

Caution

Review failed

The pull request is closed.

Note

.coderabbit.yaml has unrecognized properties

CodeRabbit is using all valid settings from your configuration. Unrecognized properties (listed below) have been ignored and may indicate typos or deprecated fields that can be removed.

Parsing warnings: 1

Validation error: Unrecognized key: "enabled" at "reviews.pre_merge_checks"
Configuration instructions

  • Please see the configuration documentation for more information.
  • You can also validate your configuration using the online YAML validator.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json
Walkthrough

The timing-drift tool now measures either the legacy or channel API. The WS2812 plain RGB path now encodes pixels in batches, with tests comparing its output against the per-pixel encoder.

Changes: Channel Timing and RGB Encoding

Layer / File(s) Summary
Channel-API timing measurement
ci/autoresearch/test_timing_drift.py, examples/AutoResearch/AutoResearchRemoteDriverMethods.cpp, examples/AutoResearch/AutoResearchTimingDrift.h
The CLI and RPC accept legacy or channel and report the selected API. The timing runner registers a channel with FastLED.add() for channel measurements, then removes it after measurement. The CLI also reports instruction counts when available.
Bulk RGB encoding and equivalence tests
src/fl/chipsets/encoders/pixel_iterator.h, tests/unit/fl/chipsets/encoders/pixel_iterator.hpp
The WS2812 plain RGB path loads and writes batches of up to 32 pixels, using a per-pixel fallback when no bulk loader is bound. Tests compare output length and bytes with the per-pixel encoder across color orders, brightness values, dither modes, and controller types.

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

Change: Refactor

Sequence Diagram(s)

sequenceDiagram
  participant TimingCLI as test_timing_drift.py
  participant TimingRPC as timingDriftTest handler
  participant DriftRun as timing_drift::run
  participant FastLED
  participant Measure as measureShows
  TimingCLI->>TimingRPC: send selected api
  TimingRPC->>DriftRun: call run with channel selection
  DriftRun->>FastLED: register strip with FastLED.add
  DriftRun->>Measure: measure registered strip
  Measure-->>DriftRun: return timing results
  DriftRun-->>TimingRPC: return results and selected api
  TimingRPC-->>TimingCLI: report timing results
Loading
sequenceDiagram
  participant WriteWS2812 as PixelIterator::writeWS2812
  participant WriteRGBBulk as PixelIterator::writeRGBBulk
  participant LoadBulk as PixelIterator::loadScaledRGBBulk
  participant Controller as PixelControllerVtable
  WriteWS2812->>WriteRGBBulk: encode plain RGB output
  WriteRGBBulk->>LoadBulk: load up to 32 pixels
  LoadBulk->>Controller: load, scale, dither, and advance pixels
  Controller-->>LoadBulk: return processed pixel count and RGB bytes
  LoadBulk-->>WriteRGBBulk: return wire-order RGB bytes
  WriteRGBBulk-->>WriteWS2812: append encoded bytes
Loading

Possibly related issues

  • FastLED/FastLED#5356 — This change reduces RGB encoding work on the channel path, one of the CPU cost phases described in the issue.
  • FastLED/FastLED#5377 — This change adds bulk RGB conversion for the WS2812 path and byte-equivalence tests. It does not cover the issue’s RGBW or other encoder paths.

Possibly related PRs

  • FastLED/FastLED#5389 — It adds the instruction-count result fields that this change reports for channel-path timing.
Merge Risk: Low · up to b588c

Bulk RGB encoding for WS2812 is verified byte-for-byte against the per-pixel path in tests. A latent hang exists if a fixed-capacity output buffer is ever used, and the channel timing benchmark uses a different colour order than the legacy one. Both are small fixes and no current caller is affected, so the merge risk is low.

- [ ] 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

@github-actions github-actions Bot added the sanitized Title, body and conversation screened; review text requires separate screening. label Nov 22, 2026
@fastled-project-sync fastled-project-sync Bot moved this to Triage in FastLED Tracker Nov 22, 2026
@zackees
zackees added this pull request to the merge queue Nov 22, 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 22, 2026
@github-actions

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)

@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 22, 2026
Merged via the queue into master with commit da963db Nov 22, 2026
131 of 133 checks passed
@zackees
zackees deleted the perf/5399-channel-bulk-rgb branch November 22, 2026 06:20
@fastled-project-sync fastled-project-sync Bot moved this from Triage to Done in FastLED Tracker Nov 22, 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 22, 2026
@coderabbitai

coderabbitai Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Action not completed

Pull request is closed.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@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 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

sanitized Title, body and conversation screened; review text requires separate screening.

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

perf(encode): bulk RGB encode for the channel API (FastLED.add) path, plus a channel-API latency measurement (#5375)

1 participant