Skip to content

perf(encode): resolve pixel types once per strip and convert in bulk instead of per-pixel PixelIterator calls (#5375) #5377

Closed

Description

@zackees

Sub-issue of #5375 (meta), part of #5356.

Problem

Pixel conversion still runs one pixel at a time through the type-erased fl::PixelIterator (src/fl/chipsets/encoders/pixel_iterator.h). It is a hand-built vtable: every pixel goes through function pointers for the load, scale, dither step, advance and has(n) calls. The compiler can't inline across those calls, so each pixel pays several indirect calls plus the loss of the loop optimisations that inlining would allow.

#5358 removed this from one case: legacy single-strip RGB WS2812 now calls detail::encode_ws2812_rgb_direct with the real PixelController<> type (src/fl/channels/slim_bridge_controller.h). That case dropped from 357 to ~219 µs.

Every other path still goes through the per-pixel iterator:

  • legacy RGBW: iterator.writeWS2812WhiteChannel(&encoded) in the same function;
  • the channel API: Channel::encode* uses ReorderingPixelIteratorAny for every encoder (WS2812, TM1812 RGBWW, TM1908, UCS7604, APA102, SK9822, ...) in src/fl/channels/channel.cpp.hpp;
  • platform drivers that call pixels.as_iterator().

Fix (keeps the layering)

Resolve the types once per strip, then apply the conversion in bulk:

  1. At the point where the concrete PixelController<ORDER, ...> type is still known, run one templated bulk pass. It loads, scales, dithers and colour-corrects the whole strip into a contiguous wire-ready pixel array, with no type-erased call per pixel.
  2. Encoders then run a functor over that pixel array (span in, bytes out). The functor is chosen once per strip, so the per-pixel inner loop is a plain, inlinable loop over bytes.
  3. Keep PixelIterator as the convenience API for drivers that use it. The hot channel and legacy paths stop calling it per pixel. Generalise what perf(channels): encode plain-RGB WS2812 frames straight from PixelController (#5357) #5358 did for RGB instead of adding one more special case.

Constraints

  • Small-memory tiers must not grow: this is why PixelIterator exists (see the comment in its constructor). Either the bulk kernels are instantiated only where they're already needed, or they're compiled out under FL_PLATFORM_HAS_TINY_MEMORY.
  • Output must be byte-for-byte identical to the current per-pixel encoder for every encoder, colour order, RGBW mode and dither mode. Add host tests that compare the two for each combination.
  • Watch the extra RAM for a whole-strip intermediate buffer on large strips; encode into the existing output buffer in chunks if it matters.

Rules for this sub-issue (from the meta issue)

  • Keep the layered architecture. Fix this inside its layer. Don't bypass ChannelManager or the channel engine.
  • Accept on instructions, not wall time. Measure instructions per show() on the ESP32-C6 before and after. Use bash autoresearch esp32c6 --legacy --rmt with the counter from the measurement sub-issue. Put both numbers in the PR. Code layout alone moves wall time by 10–20 µs between builds, so a wall-time number on its own can't show a 5–10% cut.
  • Usual gates:
    • bash test --cpp;
    • bash lint;
    • a passing AutoResearch run with the show() latency gate on;
    • bash bloat esp32s3 --build, with no growth past the ratchet.
  • Keep it small. Don't grow the binary on small-memory tiers (AVR, ATtiny). If a new bulk kernel doesn't pay for itself on FL_PLATFORM_HAS_TINY_MEMORY builds, compile it out of them.

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