Skip to content

perf(analysis): IDW from every point takes the points in blocks, 16 cells at a time - #3009

Merged
Mavrick-F merged 1 commit into
opengeos:mainfrom
Mavrick-F:perf/idw-point-blocks
Nov 26, 2026
Merged

Mavrick-F merged 1 commit into
opengeos:mainfrom
Mavrick-F:perf/idw-point-blocks

Conversation

@Mavrick-F

@Mavrick-F Mavrick-F commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Summary

After #2942, IDW from every point at a power of 2 spends about 93% of the benchmark run in the per-cell pass. Two things held that pass back: it streamed all the points' columns from the shared cache or memory again for every cell, and it kept eight lanes of nearest distances beside its two sums, which spilled to the stack.

  • A run of 16 cells of a row now goes over one block of 512 points (their x, dy² and values fit the nearest cache) before the next block.
  • Each point's dy² is the same along a row, so it is made once per block and run.
  • The nearest distance is no longer kept in the pass. A point within 1e-24 (squared) alone weighs 1e24 or more, so it is only looked for when the sum of the weights reaches that.
  • The inner loop went from 95 instructions with 14 stack references per eight points to 51 with none.
  • Cancel is now looked at between runs of up to 16 cells instead of between cells. At other powers it is still every cell.

Output

The same bit for bit: each cell's sums are added in the same eight lanes in the same order. The nine cases below wrote byte-identical GeoTIFFs with main and this PR (powers 2, 3 and 0.5, lon/lat and projected points, row widths that aren't multiples of 16, and a point on a cell centre).

Timing

Whole rustgis-cli run (load, IDW, write), release builds of main at 6810fba and of this change on it, through the host queue on a shared 4-core, 8-thread box (load average about 7 to 14), runs alternating between the two. Medians; CPU is user plus system. "Benchmark" is the case from #1227: 100,000 points, field value, onto 100 × 100 cells.

Case (power 2 unless said) Runs Wall, main → PR CPU, main → PR
Benchmark 21 0.35 → 0.27 s (1.30×) 1.89 → 1.44 s (1.31×)
Same points, 167 × 167 cells 21 0.80 → 0.62 s (1.29×) 5.13 → 3.85 s (1.33×)
Same points projected (UTM), 1000 m cells 21 0.41 → 0.34 s (1.21×) 1.98 → 1.51 s (1.31×)
20,000 points, 334 × 334 cells 7 0.62 → 0.50 s 4.10 → 3.13 s
1,000 points, 1000 × 1000 cells 7 0.34 → 0.30 s 1.94 → 1.55 s
20 points, 2000 × 2000 cells 7 0.25 → 0.27 s 1.02 → 0.94 s
1,000 points, 500 × 500 cells, power 3 7 1.13 → 1.21 s 7.91 → 8.31 s
1,000 points, 200 × 200 cells, power 0.5 7 0.19 → 0.19 s 1.27 → 1.33 s

Other powers take the same per-cell pass as before and now measure about 5% more CPU. The instruction count is within 1%, and a standalone build of both versions of that code showed no slowdown, so this looks like how the loop landed in this build.

What remains is the pass itself: one f64 division for each point and cell, two at a time in the baseline x86-64 build.

Memory

Each thread keeps 4 KB of dy² for the block. The point columns are as before, and peak memory in the runs above moved by 1.5 MB or less.

Surfaces

The change is in rustgis_analysis::rasterops::idw, so it reaches the IDW Interpolation tool everywhere: desktop, browser, command line, Python, and the Assistant and MCP through gp.run. Nothing saved changes, and there are no new commands or parameters. The browser runs the same blocked pass on one thread. #2927 changes a line of the nearest-few path next to one edited here; whichever lands second has a one-line conflict.

Testing

  • idw_at_power_two_is_the_general_paths_surface now uses 1,101 points, so the surface is checked across more than two blocks with points past the last whole set of lanes. idw_on_several_threads_is_idw_on_one and the underflow tests cover thread counts and the fallback path.
  • cargo fmt --all --check, and the full gate on the fork's CI on this commit.
  • The timings and output comparison above.

UI evidence

No visible UI change.

Related issue

Part of #1227.

Summary by CodeRabbit

  • Performance
    • Improved inverse distance weighting for large point sets, particularly with power 2.
  • Bug Fixes
    • Added fallback handling for extreme distance calculations and points very close to output cells.
  • Tests
    • Expanded coverage for larger point sets, including cases that span multiple processing blocks.

…ells at a time

Each cell's pass streamed every point's columns from the shared cache or memory, and held eight lanes of nearest distances beside its two sums, which spilled to the stack. At a power of 2, a run of 16 cells of a row now goes over one block of 512 points before the next, with each point's dy² made once per block and run. The nearest distance is only looked for when the sum of the weights says a point could be within 1e-24. The sums are added in the same order as before.

Part of opengeos#1227.
@coderabbitai

coderabbitai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The all-points power-2 IDW path now processes output cells in runs of up to 16 and points in blocks of 512. It reuses squared north–south distances and falls back to nearest-point weighting when sums are unsafe or a point is within the exact-distance threshold.

Changes: Blocked power-2 IDW

Layer / File(s) Summary
Block-based IDW evaluation
crates/analysis/src/rasterops.rs
The evaluator processes up to 16 cells at a time and points in blocks of 512. It reuses squared north–south distances and accumulates weighted sums in fixed-order lanes. It uses nearest-point weighting for unsafe sums and exact-distance cases.
Raster processing and validation
crates/analysis/src/rasterops.rs
The all-points power-2 path processes cells in runs with cancellation checks. Other powers and the nearest-few path remain per-cell. The test now uses 1,100 points to cover multiple blocks and a partial lane set.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Refactor

Possibly related issues

  • opengeos/RustGIS#2918 — This change speeds up the brute-force per-cell scan described in the issue, but does not add a spatial index or non-finite coordinate filtering.
  • opengeos/RustGIS#1227 — This change speeds up the IDW per-cell pass described in the issue, but does not change the MAX_IDW_WORK refusal policy.

Possibly related PRs

  • opengeos/RustGIS#2942 — This PR restructures the power-2 IDW path introduced there into cell runs and point blocks.
  • opengeos/RustGIS#1520 — This PR continues the squared-distance weighting and point-hit threshold used by that power-2 implementation.
  • opengeos/RustGIS#1311 — This PR changes the cancellation-check cadence for power-2 processing introduced there.

Suggested reviewers: pick2510, giswqs

Merge Risk: Minimal · up to 1f5ff

This speeds up the power-2 IDW interpolation by processing cells and points in blocks, without changing saved output, parameters or the interface. No concrete defect was found, and the author reports identical output in comparisons and a test covering multiple point blocks. It is low risk to merge, and a revert would be straightforward.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: the power-2 all-points IDW pass now walks points in blocks and processes 16 cells at a time. It uses the perf() type and the analysis scope, and it matches the raw sum…
Description check Passed The description follows the repository template. It has Summary, Testing, UI evidence, and Related issue sections. Summary explains the motivation and the change. Testing lists the unit tests, the CI …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: the power-2 all-points IDW pass now walks points in blocks and processes 16 cells at a time. It uses the perf() type and the analysis scope, and it matches the raw summary and objectives. The title is 83 characters, which is slightly long, but it is still clear.

Full details: Description check

Explanation

The description follows the repository template. It has Summary, Testing, UI evidence, and Related issue sections. Summary explains the motivation and the change. Testing lists the unit tests, the CI gate, and the timing and output comparisons. UI evidence states 'No visible UI change.' as the template allows. Related issue reads 'Part of #1227.' The description is thorough and mostly complete.

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.

Share

A rabbit hops past sixteen squares,
Then checks each block with careful care.
The sums run low, the sums run high,
Nearest-point weights step in nearby.
One thousand points now test the trail.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: e74a9443-827b-4e9a-85b6-9288d0d775c3
Commits

Reviewing files that changed from the base of the PR and between 336a178 and 1f5ff00.

Files selected for processing: 1

  • crates/analysis/src/rasterops.rs

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 0 remain after this review.

@Mavrick-F

Copy link
Copy Markdown
Member Author

@coderabbitai full review

@coderabbitai

coderabbitai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor
Action performed

Full review finished.

/// [`Self::sums`] with w = 1/d² for each of up to [`IDW_CELLS`] cells at `y`, one at each of
/// `xs`, bit for bit. The points go a block at a time through all the cells, each point's dy²
/// (the same along the row) made once into `dy2`.
fn run_sums(&self, xs: &[f64], y: f64, kx: f64, dy2: &mut [f64; IDW_BLOCK]) -> [(f64, f64); IDW_CELLS] {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR's central claim is that run_sums gives the same bits as sums for every cell, but no test pins that. idw_on_several_threads_is_idw_on_one only compares thread counts, and idw_at_power_two_is_the_general_paths_surface compares against a reference with a tolerance. A later change to the block or lane order could break the bit-for-bit promise without any test failing.

A small test next to this function would close the gap. For a set of points that spans more than two blocks and ends in a partial lane set (the 1,101-point fixture is already right), with a row of 16 cells plus a partial run, assert that run_sums(xs, y, kx, &mut dy2) matches sums(c, kx, |d2| 1.0 / d2) per cell with to_bits() equality on both the numerator and denominator.

This is a test for code that already works, so it is a quality point. The PR's own timing and output comparisons are not in the repository.

Category: quality

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving this for the release quality pass: it's an extra test for code that works, and the bit-for-bit output was checked on nine cases for this PR.

@github-actions

Copy link
Copy Markdown
Contributor

Code review

I found no bugs, crash paths, or security issues in this change. I checked the arithmetic and the edge cases closely, so I'm fairly confident in the "no bugs" result. The main open item is a missing test for the bit-for-bit claim.

I left nothing to re-raise: there are no prior review threads on this PR, so no earlier concerns were skipped.

Bugs

  • None found. Checked with high confidence:
    • Lane mapping across blocks: block sizes are multiples of 8, so each point keeps its lane, and the last partial block adds its remainder in the same order as sums.
    • dy_rest slicing: dy2.get(x.len() * 8..) can reach past the block's remainder, but zip truncates it to x_rest.len(), so only the written entries are used.
    • stands shortcut: den < 1/IDW_EXACT_D2 implies no point lies within IDW_EXACT_D2. The distance and dy² arithmetic is operand-for-operand the same as d2(), so the shortcut matches the old path.
    • Pace accounting: work per run is points × cells, which sums to the same total as the old per-cell count.
    • Panics: get/unwrap_or are used, and the const assert is compile-time only.

Security

  • None.

Performance

  • Low-severity note, not posted inline: the PR reports about 5% more CPU for powers other than 2 (for example, power 3: 7.91 → 8.31 s). The author attributes this to code layout, and it is disclosed. Confidence that it is a real regression: moderate.

Quality

  • Test gap (posted inline on run_sums, confidence high): the "bit for bit" claim is only checked outside the repository. Existing tests compare thread counts or use a tolerance. A bitwise test against sums(c, kx, |d2| 1.0 / d2) would protect the claim.

AGENTS.md

  • None. Checked: no unwrap/expect/panic/unsafe; memory adds only 4 KB of per-thread scratch and no second copy of the layer; threads go through for_each_row; the PR states its memory and surface reach; no new command, so the cancellation lists and command-surface.txt are unaffected; the idw_interpolation wrapper in tools.rs is unchanged; there is no wasm-specific branch in the diff. Line 897 is 139 columns, within max_width = 140.

Change size

Compared: the whole PR against main (merge base 6810fba to PR head 1f5ff00).

Measure Value
Files changed 1 (added 0, modified 1, removed 0, renamed 0)
Size before 178.5 KB
Size after 182.2 KB
Total size change +3.7 KB
Path Before After Change
crates/analysis/src/rasterops.rs 178.5 KB 182.2 KB +3.7 KB

@Mavrick-F
Mavrick-F added this pull request to the merge queue Nov 26, 2026
@github-actions

Copy link
Copy Markdown
Contributor

📖 Docs preview

Item Value
Preview https://preview.rustgis.app/pr-3009/
Web app https://preview.rustgis.app/pr-3009/app/
Commit 1f5ff00

GitHub Pages takes a minute or two to publish a new commit. Newer commits from the fork aren't published until a maintainer adds the preview label (once per commit).

Merged via the queue into opengeos:main with commit 085ccc0 Nov 26, 2026
28 checks passed
@Mavrick-F
Mavrick-F deleted the perf/idw-point-blocks branch November 26, 2026 06:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant