Repository navigation
fix(analysis): Kriging merges coincident points in linear work and looks at Cancel as it prepares - #2500
Conversation
…oks at Cancel as it prepares Points sharing an x made each new point scan every earlier one; they are now found through cells around each point, with the same result. Huge coordinates each get a cell of their own. Reading and merging look at Cancel every 4,096 points.
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
| let cell = coincident_cell; | ||
| points.sort_by(|a, b| a.0.x.total_cmp(&b.0.x).then_with(|| a.0.y.total_cmp(&b.0.y))); | ||
| let mut kept: Vec<(Coord, f64, usize)> = Vec::with_capacity(points.len()); | ||
| let mut cells: HashMap<(i64, i64), Vec<usize>> = HashMap::new(); |
There was a problem hiding this comment.
Size for real data / performance, confidence ~70% on the magnitude. The cell index allocates a Vec<usize> per occupied cell and a hash entry for it. Distinct points each get their own cell, so the merge's working set moves from about 56 B per point (raw 24 + accum 32) to roughly 170 B or more per point, around 3× the old peak. Per point the merge also does nine HashMap::get calls with SipHash and one heap allocation. Without shared x values the old sorted scan did about one comparison per point, so the common case may slow down. The PR body gives no multiple of the layer and no timing for that case, and AGENTS.md asks for both when a change stores layer data.
Suggestions: measure with cargo xtask memory and state the multiple in the PR. Consider a flat index, such as a HashMap<(i64, i64), u32> head plus a Vec<u32> of next links, which avoids one allocation per cell. Time the typical case as well as the worst case.
There was a problem hiding this comment.
Fixed in 8c5e1ac, and measured.
- The common case: each point first scans back up to 8 kept points, as the old merge did, which settles nearly every point. A typical layer never files a point in the cells.
- Cells: only a longer run within 1e-9 to the left (a shared x) is filed, lazily, from the first kept point inside that window. A cell holds a head index, and each point a link to the one before it, with no
Vecper cell. The lookup reads 6 cells, not 9. - Timings (release): random points with no shared x take 7.6 ms per 100,000 against 6.1–6.4 ms before, and 85 ms per million against 66–79 ms. 40,000 points on one x take 5.3 ms against about 450–510 ms.
- Memory: the merge holds 48 bytes per point besides the points read, against 32 before, plus about 40 per point only for points in such runs. That's about 0.7× of a point layer's rows, against 0.55× (up to about 1.1× when every point is in a run), freed once the grid is made.
The PR description now gives the timings and the multiple.
| accum.push((c, v, 1)); | ||
| } | ||
| let pts: Vec<(Coord, f64)> = accum.into_iter().map(|(c, sum, count)| (c, sum / count as f64)).collect(); | ||
| let (raw_pts, beyond_note) = points_in(crs, raw_pts)?; |
There was a problem hiding this comment.
Bug (Cancel gap), confidence ~85%. points_in (unchanged, line 4966) reprojects every point in one loop with no control::check(). It runs between the reading loop and merge_coincident, and for any non-LonLat CRS it is an unchecked pass over all points. That is the stretch this PR says it makes responsive. The new test kriging_preparation_looks_at_cancel_as_it_goes sets no CRS, so it most likely takes points_in's LonLat early return and never reaches that loop.
Suggested fix: in points_in, count the points with enumerate() and call crate::control::check()? when i % LOOK_EVERY == 0. Then add a projected-CRS variant of the test that counts looks.
There was a problem hiding this comment.
Fixed in 8c5e1ac. points_in now looks at Cancel every 4,096 points while it projects, and it still projects in the points' own storage (a filtered collect through Result), so it holds no second copy. This also covers IDW and the Geostatistical Wizard, which share it. New test kriging_looks_at_cancel_while_it_projects_its_points: 40,000 points in Web Mercator look at Cancel ten times each while reading, projecting and merging, then a few times for the grid. Without the projection's ten, the count is about 20.
Code reviewBugs
Security
Performance
Quality
AGENTS.md
Prior reviews: none on this PR, so no concerns were skipped as already raised. Change sizeCompared with the base branch: the whole PR, merge base 3687eef to head e048128.
|
📖 Docs preview
GitHub Pages takes a minute or two to publish a new commit. |
…ls, and projecting points looks at Cancel Most points settle in a short scan back, as before, so a typical layer never files points in cells; only runs within 1e-9 to the left are filed, as a head per cell and links, with no list per cell. points_in looks at Cancel every 4,096 points, keeping the points' own storage.
| let fi = find_field_case_insensitive(ds, field)?; | ||
| let mut raw_pts: Vec<(Coord, f64)> = Vec::new(); | ||
| for r in rows(inp, p, "input", ds) { | ||
| for (n, r) in rows(inp, p, "input", ds).into_iter().enumerate() { |
There was a problem hiding this comment.
AGENTS.md (progress not counted), confidence ~65%. The new Cancel checks here, in merge_coincident's loop, and in points_in look at Cancel but count no progress. AGENTS.md's "Never hold the window" asks long loops to count their progress; a busy indicator without a count is allowed only where progress can't be measured. Here it can: rows(...) gives the total and the merge gets points.len().
Suggested shape: before this loop, let input_rows = rows(inp, p, "input", ds); crate::control::begin(input_rows.len());, then crate::control::reached(n); next to each crate::control::check()?; (reached takes the index at the top of each loop, as in control.rs). Do the same around the merge loop with begin(points.len()). Mind that points_in runs between these two loops, so either begin again there or count only the reading and merge phases.
Code reviewSummary. The merge is sound. I traced the cell lookup against the old scan, and the tests check it bit for bit against the old code. I found no correctness, crash, or security bugs. One repository rule is not met, and there are a few minor nits. Bugs: None found. I checked the window and filing logic, the Security: None. Performance: Nothing blocking. The Quality (grouped nits, for the release quality pass):
AGENTS.md:
Already raised, left alone: 2 earlier threads. The Change sizeCompared with the base branch: the whole PR against main (merge base 3687eef to PR head 8c5e1ac).
|
|
@coderabbitai review |
Action not completed
|
Summary
Ordinary Kriging merges sample points at the same place (within 1e-9 in x and y) before it works out its grid. It sorted the points by x and then y. Each new point then scanned back through the points kept until it found one more than 1e-9 to its left.
The problem: points sharing an x made every new point rescan all earlier points with that x. That is O(n²) on one line, and O(n·√n) on a square grid's columns. Survey lines and gridded samples are exactly that shape. Nothing before the grid looked at Cancel (not reading the rows, not the merge), so Cancel waited for all of it.
The fix (
crates/analysis/src/tools.rs):merge_coincidentfirst scans back up to 8 kept points, as the merge always did. That settles nearly every point (a near point found, or one more than 1e-9 to the left), so a typical layer costs what it did. Only a point in a longer run within 1e-9 to its left (a shared x) looks in cells of 4e-9 (four times the tolerance): its own cell and 5 neighbours (a kept point is never to its right).coincident_cell). Huge coordinates don't all share the last cell, which would make the merge quadratic again.points_in, which IDW and the Geostatistical Wizard share) and merging look at Cancel every 4,096 points.points_instill projects in the points' own storage. The sort is one short step without a look, and the comment says so.Measured in release, merging only (median of a few runs):
Memory (Size for real data): what the merge holds besides the points read (24 bytes each), per point:
Reach every surface
Testing
cargo fmt,clippy -D warningsonrustgis-analysis, therustgis-analysistests, the engine's cancellation tests,cargo xtask test-hygiene, andRUSTFLAGS='-D warnings' cargo build --locked --target wasm32-unknown-unknown -p rustgis-web.kriging_merges_coincident_points_as_before: the old scan is kept in the test as the reference. The new merge must give the same points and values, bit for bit, on:kriging_files_huge_coordinates_in_cells_of_their_own: 10,000 different coordinates past 1e11 get 10,000 cells, apart from every cell near 2^24 and zero, and 100,000 of them merge.kriging_looks_at_cancel_while_it_projects_its_points: the same in Web Mercator. Ten looks each reading, projecting and merging, and it stops at its 15th look.kriging_preparation_looks_at_cancel_as_it_goes: 40,000 points on one x.UI evidence
No change to the interface.
Related issue
None; found in a review of long-running work and Cancel.
Summary by CodeRabbit