Repository navigation
perf(analysis): reuse terrain raster constants - #1122
Conversation
|
No actionable comments were generated in the recent review.
|
| Layer / File(s) | Summary |
|---|---|
Gradient inputs and terrain callers crates/analysis/src/rasterops.rs |
gradient now accepts cell dimensions as an argument. slope, aspect, and hillshade pass dimensions from Raster::cell_size_m(). Hillshade caches the zenith cosine and sine. A regression test checks slope and aspect arrays within 1e-4 and exact hillshade output for a 4×3 raster with no-data. |
Priority:
Estimated code review effort: 2 (Simple) | ~10 minutes
Change: Refactor
Possibly related issues
- Issue 1111 describes slow hillshade related to repeated terrain constants and trigonometric calculations. This change caches hillshade zenith values, while the issue summary notes that other work remains separate.
Suggested reviewers: giswqs
Merge Risk: 
Minimal · up to 3e71e
-
No actionable merge-blocking issue is established for this terrain optimization. Merge after normal checks pass.
Pre-merge checks: 
5
-
Full details: Docstring Coverage
-
Explanation
Docstring coverage is 80.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 5 functions across 1 files.
-
Finishing Touches
-
🧪 Generate unit tests (beta)
-
- Create a new PR
-
- 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.
A rabbit checks the slopes in rows,
Where no-data rests, the raster shows.
The aspect turns, the hillshades gleam,
Cell sizes guide each gradient stream.
Then carrots crown the tested scene.
Comment @coderabbitai help to get the list of available commands.
| let expected_aspect = | ||
| [285.89474, 305.4469, 311.66492, 288.18884, 358.39075, NODATA, 160.65001, 180.0, 7.9960175, 97.375114, 163.86998, 172.00398]; | ||
| for (actual, expected) in | ||
| [(slope(&r, 0, 1.5).unwrap_or_default(), expected_slope), (aspect(&r, 0).unwrap_or_default(), expected_aspect)] |
There was a problem hiding this comment.
Nit: unwrap_or_default() turns an Err from slope/aspect into an empty Vec. The test still fails, but with a length mismatch (0 vs 12) instead of the error message. The rest of this test module (ramp()) reports errors with unwrap_or_else(|e| panic!("{e}")), so matching that keeps failures readable. The same applies to the hillshade(...) call at line 1085.
| [(slope(&r, 0, 1.5).unwrap_or_default(), expected_slope), (aspect(&r, 0).unwrap_or_default(), expected_aspect)] | |
| [(slope(&r, 0, 1.5).unwrap_or_else(|e| panic!("{e}")), expected_slope), (aspect(&r, 0).unwrap_or_else(|e| panic!("{e}")), expected_aspect)] |
Code reviewBugs: None found. Hoisting Security: None. No new input handling, parsing, or I/O. Performance: The change is sound. It removes a CRS check, and for geographic rasters a Quality:
AGENTS.md: Compliant. The new Change sizeCompared: the whole PR against main (merge base 13aee13 to PR head 3e71e7d).
|
📖 Docs preview
GitHub Pages takes a minute or two to publish a new commit. |
Summary
Slope, aspect and hillshade recomputed the raster's cell size for every pixel, including a CRS lookup and, for geographic data, a cosine. Compute it once per operation and reuse it for Horn's gradient. Hillshade also computes its fixed lighting-angle sine and cosine once. The formulas, no-data handling and Float32 output stay unchanged.
This is a small part of #1111's terrain work. The larger row-pass optimization and Byte output remain for separate work. The axis-orientation correction in #1085 is independent; the two changes merge without conflicts.
On a 2,500 × 2,500 synthetic geographic raster in the optimized test profile, one local run per version measured slope 1.76 s → 1.56 s, aspect 1.58 s → 1.39 s, and hillshade 2.42 s → 2.19 s. These are kernel timings, not full application timings. Checksums of every output pixel matched before and after.
Testing
uvx pre-commit run --all-filescargo xtask ci(format, workspace Clippy, tests and layers)cargo build --locked --target wasm32-unknown-unknown -p rustgis-web(with clang)cargo test -p rustgis-analysis --lib terrain_Related issue
Part of #1111.
Summary by CodeRabbit