Skip to content

perf(wave): hoist row arithmetic + FL_RESTRICT_PARAM in 2D update inner loop (closes #3094) - #3103

Merged
zackees merged 1 commit into
masterfrom
fix/3094-wave-row-hoist
Jun 15, 2026
Merged

zackees merged 1 commit into
masterfrom
fix/3094-wave-row-hoist

Conversation

@zackees

@zackees zackees commented Jun 15, 2026 •

Copy link
Copy Markdown
Member

Implements #3094 (§4 from meta tracker #3098).

Scope reduction note

The §4 issue body proposed two changes — (a) branchless Q15 clamp and (b) hoisted row indexing + __restrict__. (a) was already done as a side effect of #3101 (§3 half-duplex fold-in: fl::clamp replaced the branchy if/else). This PR is the (b) row-indexing portion only.

Change

In WaveSimulation2D_Real::update():

  • Lift row = j * stride out of the inner loop.
  • Set up four row pointers (row_curr, row_above, row_below, row_next) qualified with FL_RESTRICT_PARAM (codebase's portable restrict macro from fl/stl/compiler_control.h — __restrict__ on gcc/clang, __restrict on MSVC, restrict on C99).
  • Inner loop now indexes by i only.
  • Small side benefit: c = row_curr[i] is fetched once and reused in both the Laplacian (-(c << 2)) and the new-value computation ((c << 1)).

1D update is unchanged — no j * stride to hoist.

Why

fl::size is size_t and the optimizer can't always prove the curr/next pointers don't alias. With FL_RESTRICT_PARAM and explicit row pointers the dependency picture across iterations is clear, which lets the back-end interleave loads or vectorize.

Validation

  • bash test fl_math_wave_wave_simulation_real --cpp — 5/5 pass.
  • bash lint --cpp — clean.

Closes #3094
Refs #3098 (parent meta), #3099 §2, #3101 §3

Summary by CodeRabbit

  • Refactor
    • Optimized wave simulation engine performance through enhanced memory access patterns in core update calculations. All mathematical precision, damping behavior, and output characteristics remain unchanged, ensuring backward compatibility while delivering improved performance to wave-based effects.

…er loop

The 2D update inner loop previously recomputed `int index = j * stride + i;`
on every iteration and accessed curr[index+1], curr[index-1],
curr[index+stride], curr[index-stride] without an alias guarantee. The
optimizer *should* hoist `j * stride` and prove non-aliasing across the
curr/next pointers, but with `fl::size` (size_t) iteration and
non-restricted pointers it often doesn't.

Spell it out: lift `row = j * stride` out of the inner loop, set up
`row_curr / row_above / row_below / row_next` pointers, and qualify them
with `FL_RESTRICT_PARAM` (the codebase's portable restrict-keyword macro
from fl/stl/compiler_control.h — __restrict__ on gcc/clang, __restrict on
MSVC, plain `restrict` on C99). The inner loop now indexes by `i` only.

Small side benefit: `c = row_curr[i]` is fetched once and reused in both
the Laplacian (`-(c << 2)`) and the new-value computation (`(c << 1)`),
saving a load on platforms without aggressive CSE.

Branchless-clamp portion of the original §4 scope was already done as a
side effect of #3101 (§3 half-duplex fold-in: `fl::clamp` replaced the
branchy if/else). This PR is the row-indexing portion only.

1D update is unchanged — no `j * stride` to hoist.

Validated by tests/fl/math/wave/wave_simulation_real.cpp (5/5 pass).

Closes #3094
Parent meta: #3098
@coderabbitai

coderabbitai Bot commented Jun 15, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 35068eeb-01b6-4862-b5e5-f4f207c1469c

📥 Commits

Reviewing files that changed from the base of the PR and between 9d5442e and 5bd7899.

📒 Files selected for processing (1)
  • src/fl/math/wave/wave_simulation_real.cpp.hpp

📝 Walkthrough

Walkthrough

In WaveSimulation2D_Real::update(), the inner cell update loop is refactored to hoist row base-address arithmetic outside the i loop. Four FL_RESTRICT_PARAM row pointers (row_curr, row_above, row_below, row_next) replace per-cell linear index computation, and the final clamped store writes through row_next[i] instead of next[index].

Changes

Wave inner loop row pointer refactor

Layer / File(s) Summary
Row pointer hoisting and store rewrite
src/fl/math/wave/wave_simulation_real.cpp.hpp
Per-row pointer variables with FL_RESTRICT_PARAM are introduced for row_curr, row_above, row_below, and row_next; the Laplacian reads and the final clamped store now use these pointers instead of a per-cell linear index recomputed each iteration.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~5 minutes

Possibly related issues

Possibly related PRs

  • FastLED/FastLED#3101: Modifies the same WaveSimulation2D_Real::update() inner loop, folding the half-duplex q15_min clamp into fl::clamp in the same code path this PR restructures with row pointers.

Poem

🐇 Hop, hop, no more multiply each step,
Row pointers precomputed, the inner loop swept.
__restrict__ whispered: "these don't alias, friend,"
The optimizer smiled and hoisted to the end.
Fewer index sums, the same dancing wave—
A tidy refactor, from this little knave!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the performance optimization: hoisting row arithmetic and adding FL_RESTRICT_PARAM in the 2D wave update loop, directly matching the main change.
Linked Issues check ✅ Passed The PR implements the second optimization from #3094 as specified: hoisting row arithmetic outside the inner loop and adding restrict qualifiers to four explicit row pointers.
Out of Scope Changes check ✅ Passed All changes are within scope; the refactoring modifies only the WaveSimulation2D_Real::update() inner loop to implement the row-hoisting optimization from #3094.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/3094-wave-row-hoist

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

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

perf(wave): branchless Q15 clamp + hoisted row indexing + __restrict__ (refs #3073 §4)

1 participant