Skip to content

perf(wave): fold half-duplex zero-clamp into main update loop (refs #3073 §3) #3093

Closed

Description

@zackees

Scope from #3073 §3.

Current code

src/fl/math/wave/wave_simulation_real.cpp.hpp runs the half-duplex zero-clamp as a separate second full pass over the grid:

  • 1D: lines ~151–158
  • 2D: lines ~296–306
if (mHalfDuplex) {
    for (fl::size j = 1; j <= height; ++j) {
        for (fl::size i = 1; i <= width; ++i) {
            int index = j * stride + i;
            if (next[index] < 0) next[index] = 0;
        }
    }
}

This doubles memory traffic on the grid — especially expensive in 2D when vector_psram<i16> puts the grid in ESP32 PSRAM (slow RAM, no cache prefetch help).

Change

Fold into the existing update loop right before the store:

if (mHalfDuplex && f < 0) f = 0;
next[index] = (i16)f;

Better — combine the half-duplex clamp with the existing Q15 saturation clamp using fl::clamp (which most compilers lower branchlessly). Hoist lo out of the inner loop:

const i32 lo = mHalfDuplex ? 0 : -32768;
// ... inner loop:
next[index] = static_cast<i16>(fl::clamp(f, lo, 32767));

Acceptance

  • bash test fl_math_wave_wave_simulation_real --cpp passes.
  • Half-duplex test case (the one verifying the kernel still runs at saturated CFL input) continues to show non-negative output post-update.
  • 2D grid touched exactly once per update, not twice.

Relation

Should be bundled with §4 (#TBD — branchless clamp + hoisted indexing) since both touch the same inner loop body.

Refs: #3073 §3.

Activity

  1. zackees commented on Jun 15, 2026

    @zackees
    MemberAuthor

    Parent meta: #3098. Bundle with #3094 (§4 branchless clamp + hoisted indexing) — same inner loop body.

  2. added a commit that references this issue on Jun 15, 2026
    9d5442e
  3. moved this from Triage to Done in FastLED Trackeron Jun 15, 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

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions