Repository navigation
perf(wave): hoist row arithmetic + FL_RESTRICT_PARAM in 2D update inner loop (closes #3094) - #3103
Conversation
…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
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughIn ChangesWave inner loop row pointer refactor
Estimated code review effort🎯 2 (Simple) | ⏱️ ~5 minutes Possibly related issues
Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
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::clampreplaced the branchy if/else). This PR is the (b) row-indexing portion only.Change
In
WaveSimulation2D_Real::update():row = j * strideout of the inner loop.row_curr,row_above,row_below,row_next) qualified withFL_RESTRICT_PARAM(codebase's portable restrict macro fromfl/stl/compiler_control.h—__restrict__on gcc/clang,__restricton MSVC,restricton C99).ionly.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 * strideto hoist.Why
fl::sizeissize_tand the optimizer can't always prove the curr/next pointers don't alias. WithFL_RESTRICT_PARAMand 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