From 7cd7d522842042557bd971f4c10a418729350068 Mon Sep 17 00:00:00 2001 From: mavrick Date: Tue, 24 Nov 2026 14:42:51 -0600 Subject: [PATCH 1/7] perf(analysis): Clip keeps a polygon inside a mask as it is and skips the overlay for masks it is clear of Clip overlaid every polygon feature with its whole mask, and a feature meeting several masks got a fresh union of all of them. Most features are wholly inside one mask or outside the others, clear of every mask edge, so the overlay found nothing to cut. A polygon feature whose padded box meets no edge of a mask (found through a grid of the mask's edges, made when first needed) is wholly inside or wholly outside it, as any one of its points is. Inside one mask it is kept as it is, wound as the overlay winds a polygon; the masks it is outside take nothing from it; only the masks whose edges its box meets are unioned and overlaid. Rings with no area, non-finite coordinates and a point on an edge go to the overlay. --- crates/analysis/src/tools.rs | 226 +++++++++++++++++++++++++++++++++-- 1 file changed, 219 insertions(+), 7 deletions(-) diff --git a/crates/analysis/src/tools.rs b/crates/analysis/src/tools.rs index a96e46581f..58a247abf0 100644 --- a/crates/analysis/src/tools.rs +++ b/crates/analysis/src/tools.rs @@ -1875,10 +1875,94 @@ fn clip_part(g: &Geometry, mask: &MultiPolygon) -> Option<(Geometry, bool)> } } +/// Where a polygon feature lies against one clip mask, found without an overlay. +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +enum Place { + /// All of it is inside the mask, clear of its edges: Clip keeps it as it is. + Inside, + /// None of it is inside the mask, and it is clear of its edges: nothing is clipped from it. + Outside, + /// An edge of the mask is within reach of its box (or it is degenerate, or has a non-finite + /// coordinate): only the overlay can say what is left. + Edge, +} + +/// A clip mask's edges, on a grid, so that the edges near a feature are found without going +/// through all of them. Holds one box per edge (32 bytes, twice the edge's own coordinates) and +/// the grid's references: about three times the mask's coordinates. +struct MaskEdges { + boxes: Vec, + grid: ExtentIndex, + /// The mask has a coordinate that isn't finite, which no grid cell holds reliably: every + /// feature is then [`Place::Edge`]. + unsure: bool, +} + +impl MaskEdges { + /// The edges of every ring of `mask`. + fn new(mask: &MultiPolygon) -> Self { + let rings = mask.0.iter().flat_map(|p| std::iter::once(p.exterior()).chain(p.interiors().iter())); + let unsure = mask.coords_iter().any(|c| !c.x.is_finite() || !c.y.is_finite()); + let boxes: Vec = rings + .flat_map(|r| r.0.windows(2).map(|w| w.iter().fold(Extent::EMPTY, |e, c| e.union(&Extent::new(c.x, c.y, c.x, c.y))))) + .collect(); + let grid = ExtentIndex::new(boxes.iter().copied()); + MaskEdges { boxes, grid, unsure } + } + + /// Whether any edge's box meets `ext`: exactly, so an edge within `ext`'s padding counts. + fn near(&self, ext: &Extent) -> bool { + self.unsure || self.grid.any_candidate(ext, |i| self.boxes.get(i).is_none_or(|b| b.intersects(ext))) + } +} + +/// Whether every ring of `whole` has an area to keep as it is: four or more points enclosing more +/// than a billionth of `ext`'s. A collapsed or hair-thin ring is the overlay's to drop. +fn rings_have_area(whole: &MultiPolygon, ext: &Extent) -> bool { + let least = ext.width() * ext.height() * 1e-9; + whole.0.iter().flat_map(|p| std::iter::once(p.exterior()).chain(p.interiors().iter())).all(|r| { + // From the ring's first point, so that large coordinates don't swamp a small ring's area. + let o = r.0.first().copied().unwrap_or_default(); + let twice: f64 = + r.0.windows(2) + .map(|w| w.first().zip(w.get(1)).map_or(0.0, |(a, b)| (a.x - o.x) * (b.y - o.y) - (b.x - o.x) * (a.y - o.y))) + .sum(); + r.0.len() >= 4 && twice.abs() * 0.5 > least + }) +} + +/// Where `whole` (a feature's polygons, in the run's plane, dissolved) lies against `mask`, whose +/// edges are `edges`; `ext` is the feature's extent, padded as [`clip_on`] pads it for the +/// overlay's grid. With no mask edge in `ext`, the feature's outline can't meet the mask's or +/// hold a piece of it, so it is wholly in or wholly out, as any one of its points is. The +/// answer is exact, never a float comparison against an edge the feature might poke out of; +/// anything unsure (a collapsed ring, a coordinate that isn't finite, a point on an edge) is +/// [`Place::Edge`], for the overlay to decide. +fn place_against(whole: &MultiPolygon, ext: &Extent, mask: &MultiPolygon, edges: &MaskEdges) -> Place { + use geo::algorithm::coordinate_position::{CoordPos, CoordinatePosition}; + if ext.is_empty() || !whole.coords_iter().all(|c| c.x.is_finite() && c.y.is_finite()) || !rings_have_area(whole, ext) { + return Place::Edge; + } + if edges.near(ext) { + return Place::Edge; + } + let Some(c) = whole.coords_iter().next() else { return Place::Edge }; + match mask.coordinate_position(&c) { + CoordPos::Inside => Place::Inside, + CoordPos::Outside => Place::Outside, + CoordPos::OnBoundary => Place::Edge, + } +} + /// Clip each input feature to the union of only the selected clip polygons whose projected /// extents meet it. Union candidates together so overlapping masks never duplicate output. +/// A polygon feature with no mask edge in its box is not sent to the overlay: one inside a mask +/// is kept as it is (wound as the overlay winds a polygon, its vertices not snapped to the +/// overlay's grid), and the masks it is outside take nothing from it, so only those whose edges +/// may cross it are unioned and overlaid (see [`place_against`]). /// Cancel is looked at, and progress counted, mask by mask and feature by feature; one mask's -/// union, and one feature's overlay, can't be stopped once started (#1562). Masks with no polygon, +/// union, one mask's edge index (made when a polygon feature first needs it), and one feature's +/// overlay, can't be stopped once started (#1562). Masks with no polygon, /// or whose union has no area, are left out and counted (a mask selected twice is used once); /// with none left, it's an error. fn clip(inp: &dyn Inputs, p: &Json) -> Result { @@ -2050,7 +2134,9 @@ impl MaskCache { /// largest size of the masks one feature meets, together (found for each batch first, standing /// for their union); as many threads run as fit, at that much work each, in half the scratch /// budget, and their recent unions of several masks share `cache_bytes` (by default the other -/// half of [`crate::parallel::scratch_budget`]). +/// half of [`crate::parallel::scratch_budget`]). The masks' edge indexes ([`MaskEdges`]) are +/// shared by the threads and kept for the run: about three times the coordinates of the masks +/// the polygon features meet, which the run holds already. fn clip_on(inp: &dyn Inputs, p: &Json, threads: Option, cache_bytes: Option) -> Result { let ds = input(inp, p, "input")?; let clip_ds = input(inp, p, "clip")?; @@ -2124,8 +2210,10 @@ fn clip_on(inp: &dyn Inputs, p: &Json, threads: Option, cache_bytes: Opti if matches!(g, Geometry::Point(_) | Geometry::MultiPoint(_)) { at_poles.add_to(poles(&g.extent()), &mut near); } - near + (near, ext) }; + // Each mask's edges, made when a polygon feature first needs them (on any thread). + let edges: Vec> = masks.iter().map(|_| std::sync::OnceLock::new()).collect(); // The union of the masks a feature meets, when they are several, can be far larger than any // one mask (at most all of them). Its size is taken as theirs together, which it doesn't // exceed by much (an overlay adds a vertex where edges cross, and takes away the overlaps). @@ -2150,7 +2238,7 @@ fn clip_on(inp: &dyn Inputs, p: &Json, threads: Option, cache_bytes: Opti batch, most_threads, || (), - |_, _, &r| Ok::<_, String>(ds.features.get(r).and_then(|f| f.geometry.as_ref()).map_or(0, |g| union_bytes(&near_of(g)))), + |_, _, &r| Ok::<_, String>(ds.features.get(r).and_then(|f| f.geometry.as_ref()).map_or(0, |g| union_bytes(&near_of(g).0))), )?; // The batch's threads, by its largest union too. let largest_union = unions.into_iter().max().unwrap_or(0); @@ -2165,18 +2253,42 @@ fn clip_on(inp: &dyn Inputs, p: &Json, threads: Option, cache_bytes: Opti || MaskCache::new(cache_cap), |cache, _, &r| { let Some(g) = ds.features.get(r).and_then(|f| f.geometry.as_ref()) else { return Ok::<_, String>(None) }; - let near = near_of(g); + let (near, ext) = near_of(g); if near.is_empty() { // A big union is kept only while the features next to each other meet it. cache.big = None; return Ok(None); } let shape = dissolved(g); - Ok(if let [i] = near.as_slice() { + // A polygon feature wholly inside one mask is kept as it is, and the masks it is + // wholly outside take nothing from it: the overlay (and the union of the masks + // it meets) is only for those whose edges it may cross. + let mut crossing = std::borrow::Cow::Borrowed(near.as_slice()); + if let Some(whole) = to_multipolygon(&shape) { + let mut edge = Vec::with_capacity(near.len()); + for &i in &near { + let Some((mask, _)) = masks.get(i) else { continue }; + let mask_edges = edges.get(i).map(|e| e.get_or_init(|| MaskEdges::new(mask))); + match mask_edges.map_or(Place::Edge, |e| place_against(&whole, &ext, mask, e)) { + Place::Inside => { + cache.big = None; + return Ok(from_multipolygon(&whole.orient(Direction::Default)).map(|g| (g, false))); + } + Place::Outside => {} + Place::Edge => edge.push(i), + } + } + if edge.is_empty() { + cache.big = None; + return Ok(None); + } + crossing = std::borrow::Cow::Owned(edge); + } + Ok(if let [i] = crossing.as_ref() { cache.big = None; masks.get(*i).and_then(|(m, _)| clip_part(&shape, m)) } else { - clip_part(&shape, &cache.union_of(&near, &masks)) + clip_part(&shape, &cache.union_of(&crossing, &masks)) }) }, )?; @@ -13407,6 +13519,106 @@ mod tests { } } + /// Polygon features inside, outside and across the masks' edges, and over several masks: the + /// ones clear of every edge are kept as they are or dropped without an overlay, and the rest + /// clip as the whole mask would (their pieces in the same order, with their own attributes). + #[test] + fn clip_sends_only_features_near_a_mask_edge_to_the_overlay() { + use geo::Area; + let mut masks = Dataset::new("masks", GeometryType::Polygon, vec![]); + // Two squares that overlap, and a square with a hole. + masks.push(Some(sq(0.0, 0.0, 10.0)), vec![]); + masks.push(Some(sq(5.0, 5.0, 10.0)), vec![]); + let mut holed = sq(30.0, 0.0, 10.0); + if let (Geometry::Polygon(p), Geometry::Polygon(h)) = (&mut holed, sq(33.0, 3.0, 4.0)) + && let Some(ring) = h.into_iter().flatten().next() + { + p[0].push(ring.into_iter().rev().collect()); + } + masks.push(Some(holed), vec![]); + let clockwise = Geometry::Polygon(vec![vec![vec![ + Coord::new(1.0, 1.0), + Coord::new(1.0, 2.0), + Coord::new(2.0, 2.0), + Coord::new(2.0, 1.0), + Coord::new(1.0, 1.0), + ]]]); + // (the feature, the area of what is kept) + let cases = [ + ("inside one mask", sq(2.0, 2.0, 1.0), 1.0), + ("inside both masks", sq(6.0, 6.0, 1.0), 1.0), + ("inside one mask, across the other's edges", sq(9.5, 9.5, 1.0), 1.0), + ("across one mask's edge", sq(-0.5, 2.0, 1.0), 0.5), + ("across both masks' edges", sq(9.5, 4.5, 1.0), 0.75), + ("in the hole", sq(34.0, 4.0, 1.0), 0.0), + ("far from every mask", sq(20.0, 0.0, 1.0), 0.0), + ("inside, wound the other way", clockwise.clone(), 1.0), + ]; + let mut features = Dataset::new("features", GeometryType::Polygon, vec![Field::new("row", FieldType::Integer)]); + for (i, (_, g, _)) in cases.iter().enumerate() { + features.push(Some(g.clone()), vec![Value::Int(i as i64)]); + } + let m = Map(HashMap::from([("masks".into(), masks), ("features".into(), features)])); + let out = layer(crate::run("clip", &m, &serde_json::json!({"input":"features", "clip":"masks"})).unwrap()); + let kept: Vec = out.features.iter().map(|f| f.attrs[0].as_f64().unwrap() as i64).collect(); + let want: Vec = cases.iter().enumerate().filter(|(_, (_, _, a))| *a > 0.0).map(|(i, _)| i as i64).collect(); + assert_eq!(kept, want); + for f in &out.features { + let (name, g, area) = &cases[f.attrs[0].as_f64().unwrap() as usize]; + let got = to_multipolygon(f.geometry.as_ref().unwrap()).unwrap().unsigned_area(); + let plane = to_multipolygon(g).unwrap().unsigned_area(); + // Mercator stretches the areas the same way for the piece and for the whole. + assert!((got / plane - area).abs() < 0.02 * area.max(1.0), "{name}: {got} of {plane}"); + } + // A feature clear of the edges is kept as it is, to the projection's rounding (its + // vertices in the same order, wound as the overlay winds a polygon). + let by_row = |r: i64| out.features.iter().find(|f| f.attrs[0].as_f64().unwrap() as i64 == r).and_then(|f| f.geometry.clone()); + let same = |a: Option, b: Geometry| match (a, b) { + (Some(Geometry::Polygon(a)), Geometry::Polygon(b)) => { + let (a, b): (Vec<_>, Vec<_>) = (a.into_iter().flatten().flatten().collect(), b.into_iter().flatten().flatten().collect()); + a.len() == b.len() && a.iter().zip(&b).all(|(a, b)| (a.x - b.x).abs() < 1e-9 && (a.y - b.y).abs() < 1e-9) + } + _ => false, + }; + assert!(same(by_row(0), sq(2.0, 2.0, 1.0))); + assert!(same(by_row(1), sq(6.0, 6.0, 1.0))); + assert!(same(by_row(2), sq(9.5, 9.5, 1.0))); + let Some(Geometry::Polygon(wound)) = by_row(7) else { panic!("not a polygon") }; + let ccw = to_multipolygon(&Geometry::Polygon(wound)).unwrap(); + assert!(ccw.0[0].exterior().0.windows(2).map(|w| w[0].x * w[1].y - w[1].x * w[0].y).sum::() > 0.0); + } + + /// A feature is `Inside` or `Outside` a mask only when no edge of it is within reach of the + /// feature's padded box; any edge there, a ring with no area, or a coordinate that isn't + /// finite, leaves it to the overlay. + #[test] + fn a_feature_is_placed_without_an_overlay_only_clear_of_the_mask_edges() { + let mask = union_all(to_multipolygon(&sq(0.0, 0.0, 1.0)).unwrap().0.iter()); + let edges = MaskEdges::new(&mask); + let place = |g: &Geometry| { + let whole = to_multipolygon(g).unwrap(); + let ext = projected_extent(g).padded(0.0, 100.0); + place_against(&whole, &ext, &mask, &edges) + }; + assert_eq!(place(&sq(0.4, 0.4, 0.1)), Place::Inside); + assert_eq!(place(&sq(3.0, 3.0, 0.1)), Place::Outside); + // Across an edge, on it, within the padding (100 m) of it, and around the whole mask. + assert_eq!(place(&sq(0.9, 0.4, 0.2)), Place::Edge); + assert_eq!(place(&sq(0.9, 0.4, 0.1)), Place::Edge); + assert_eq!(place(&sq(0.4, 0.4, 0.6 - 0.0005)), Place::Edge); + assert_eq!(place(&sq(-1.0, -1.0, 3.0)), Place::Edge); + // No area to keep, or no place to look at. + let flat = + Geometry::Polygon(vec![vec![vec![Coord::new(0.4, 0.4), Coord::new(0.5, 0.4), Coord::new(0.6, 0.4), Coord::new(0.4, 0.4)]]]); + assert_eq!(place(&flat), Place::Edge); + let mut bad = sq(0.4, 0.4, 0.1); + if let Geometry::Polygon(p) = &mut bad { + p[0][0][2] = Coord::new(f64::NAN, 0.5); + } + let ext = Extent::new(0.4, 0.4, 0.5, 0.5); + assert_eq!(place_against(&to_multipolygon(&bad).unwrap(), &ext, &mask, &edges), Place::Edge); + } + #[test] fn indexed_clip_handles_repeated_and_changing_neighbourhoods() { let mut masks = Dataset::new("masks", GeometryType::Polygon, vec![]); From 15b362213cfd52d6271c1984ad799eb9fbf3879c Mon Sep 17 00:00:00 2001 From: mavrick Date: Tue, 24 Nov 2026 15:28:45 -0600 Subject: [PATCH 2/7] perf(analysis): Clip's mask edge index holds grid references only The index kept a box for every mask edge, a second and larger copy of the mask's geometry. It now holds the grid's references and a table of the mask's rings, and reads an edge's ends from the mask. A mask missing from the list goes to the overlay instead of being skipped. --- crates/analysis/src/tools.rs | 122 ++++++++++++++++++++++++++++------- 1 file changed, 99 insertions(+), 23 deletions(-) diff --git a/crates/analysis/src/tools.rs b/crates/analysis/src/tools.rs index 58a247abf0..3df214de41 100644 --- a/crates/analysis/src/tools.rs +++ b/crates/analysis/src/tools.rs @@ -1888,10 +1888,14 @@ enum Place { } /// A clip mask's edges, on a grid, so that the edges near a feature are found without going -/// through all of them. Holds one box per edge (32 bytes, twice the edge's own coordinates) and -/// the grid's references: about three times the mask's coordinates. +/// through all of them. It holds the grid's references (one for each cell an edge's box meets) +/// and a table of the mask's rings, not a second copy of its coordinates: an edge's ends are read +/// from the mask it was made for, which is passed to [`MaskEdges::near`]. Making it holds each +/// edge's box until the grid is built. struct MaskEdges { - boxes: Vec, + /// Each ring's first edge's number (edges are numbered through the rings in order), its + /// polygon, and which of the polygon's rings it is: 0 for the outside, then the holes. + rings: Vec<(usize, usize, usize)>, grid: ExtentIndex, /// The mask has a coordinate that isn't finite, which no grid cell holds reliably: every /// feature is then [`Place::Edge`]. @@ -1899,20 +1903,48 @@ struct MaskEdges { } impl MaskEdges { + /// The rings of every polygon of `mask`: the outside, then the holes. + fn rings_of(mask: &MultiPolygon) -> impl Iterator)> { + mask.0.iter().enumerate().flat_map(|(p, poly)| { + std::iter::once(poly.exterior()).chain(poly.interiors().iter()).enumerate().map(move |(r, ring)| (p, r, ring)) + }) + } + /// The edges of every ring of `mask`. fn new(mask: &MultiPolygon) -> Self { - let rings = mask.0.iter().flat_map(|p| std::iter::once(p.exterior()).chain(p.interiors().iter())); let unsure = mask.coords_iter().any(|c| !c.x.is_finite() || !c.y.is_finite()); - let boxes: Vec = rings - .flat_map(|r| r.0.windows(2).map(|w| w.iter().fold(Extent::EMPTY, |e, c| e.union(&Extent::new(c.x, c.y, c.x, c.y))))) + let mut first = 0; + let rings: Vec<(usize, usize, usize)> = Self::rings_of(mask) + .map(|(p, r, ring)| { + let start = first; + first += ring.0.len().saturating_sub(1); + (start, p, r) + }) .collect(); - let grid = ExtentIndex::new(boxes.iter().copied()); - MaskEdges { boxes, grid, unsure } + let boxes = Self::rings_of(mask).flat_map(|(_, _, ring)| { + ring.0.windows(2).map(|w| w.iter().fold(Extent::EMPTY, |e, c| e.union(&Extent::new(c.x, c.y, c.x, c.y)))) + }); + MaskEdges { rings, grid: ExtentIndex::new(boxes), unsure } } - /// Whether any edge's box meets `ext`: exactly, so an edge within `ext`'s padding counts. - fn near(&self, ext: &Extent) -> bool { - self.unsure || self.grid.any_candidate(ext, |i| self.boxes.get(i).is_none_or(|b| b.intersects(ext))) + /// The ends of edge `i` of `mask`, which this was made from. + fn edge(&self, mask: &MultiPolygon, i: usize) -> Option<[geo::Coord; 2]> { + let (start, p, r) = *self.rings.get(self.rings.partition_point(|ring| ring.0 <= i).checked_sub(1)?)?; + let poly = mask.0.get(p)?; + let ring = if r == 0 { poly.exterior() } else { poly.interiors().get(r - 1)? }; + let j = i - start; + Some([*ring.0.get(j)?, *ring.0.get(j + 1)?]) + } + + /// Whether any edge of `mask`, which this was made from, has a box that meets `ext`: exactly, + /// so an edge within `ext`'s padding counts. An edge it can't find counts too. + fn near(&self, mask: &MultiPolygon, ext: &Extent) -> bool { + self.unsure + || self.grid.any_candidate(ext, |i| { + self.edge(mask, i).is_none_or(|[a, b]| { + a.x.min(b.x) <= ext.max_x && a.x.max(b.x) >= ext.min_x && a.y.min(b.y) <= ext.max_y && a.y.max(b.y) >= ext.min_y + }) + }) } } @@ -1943,7 +1975,7 @@ fn place_against(whole: &MultiPolygon, ext: &Extent, mask: &MultiPolygon, ext: &Extent, mask: &MultiPolygon Result { clip_on(inp, p, None, None) } @@ -2135,8 +2167,8 @@ impl MaskCache { /// for their union); as many threads run as fit, at that much work each, in half the scratch /// budget, and their recent unions of several masks share `cache_bytes` (by default the other /// half of [`crate::parallel::scratch_budget`]). The masks' edge indexes ([`MaskEdges`]) are -/// shared by the threads and kept for the run: about three times the coordinates of the masks -/// the polygon features meet, which the run holds already. +/// shared by the threads and kept for the run: only grid references to the masks' edges, which +/// the run holds already. fn clip_on(inp: &dyn Inputs, p: &Json, threads: Option, cache_bytes: Option) -> Result { let ds = input(inp, p, "input")?; let clip_ds = input(inp, p, "clip")?; @@ -2263,13 +2295,15 @@ fn clip_on(inp: &dyn Inputs, p: &Json, threads: Option, cache_bytes: Opti // A polygon feature wholly inside one mask is kept as it is, and the masks it is // wholly outside take nothing from it: the overlay (and the union of the masks // it meets) is only for those whose edges it may cross. - let mut crossing = std::borrow::Cow::Borrowed(near.as_slice()); + let mut crossing = Cow::Borrowed(near.as_slice()); if let Some(whole) = to_multipolygon(&shape) { let mut edge = Vec::with_capacity(near.len()); for &i in &near { - let Some((mask, _)) = masks.get(i) else { continue }; - let mask_edges = edges.get(i).map(|e| e.get_or_init(|| MaskEdges::new(mask))); - match mask_edges.map_or(Place::Edge, |e| place_against(&whole, &ext, mask, e)) { + // A mask that isn't there (not so, as `near` is of the masks) is left to the overlay. + let place = masks.get(i).zip(edges.get(i)).map_or(Place::Edge, |((mask, _), cell)| { + place_against(&whole, &ext, mask, cell.get_or_init(|| MaskEdges::new(mask))) + }); + match place { Place::Inside => { cache.big = None; return Ok(from_multipolygon(&whole.orient(Direction::Default)).map(|g| (g, false))); @@ -2282,7 +2316,7 @@ fn clip_on(inp: &dyn Inputs, p: &Json, threads: Option, cache_bytes: Opti cache.big = None; return Ok(None); } - crossing = std::borrow::Cow::Owned(edge); + crossing = Cow::Owned(edge); } Ok(if let [i] = crossing.as_ref() { cache.big = None; @@ -13523,7 +13557,7 @@ mod tests { /// ones clear of every edge are kept as they are or dropped without an overlay, and the rest /// clip as the whole mask would (their pieces in the same order, with their own attributes). #[test] - fn clip_sends_only_features_near_a_mask_edge_to_the_overlay() { + fn clip_keeps_polygon_features_clear_of_mask_edges_as_the_overlay_would() { use geo::Area; let mut masks = Dataset::new("masks", GeometryType::Polygon, vec![]); // Two squares that overlap, and a square with a hole. @@ -13619,6 +13653,48 @@ mod tests { assert_eq!(place_against(&to_multipolygon(&bad).unwrap(), &ext, &mask, &edges), Place::Edge); } + /// The edges found near a box are the ones a scan of every edge's box finds, for a mask of + /// several polygons with holes, enough edges to be on a grid, and boxes everywhere around them. + #[test] + fn mask_edges_find_what_a_scan_of_the_edges_finds() { + let ring = |cx: f64, cy: f64, r: f64, n: usize| { + geo::LineString::from( + (0..=n) + .map(|i| { + let a = std::f64::consts::TAU * (i % n) as f64 / n as f64; + (cx + r * a.cos(), cy + r * a.sin()) + }) + .collect::>(), + ) + }; + let mask = MultiPolygon::new(vec![ + geo::Polygon::new(ring(0.0, 0.0, 10.0, 40), vec![ring(0.0, 0.0, 4.0, 20), ring(6.0, 0.0, 1.0, 8)]), + geo::Polygon::new(ring(30.0, 5.0, 3.0, 12), vec![]), + ]); + let edges = MaskEdges::new(&mask); + let boxes: Vec = MaskEdges::rings_of(&mask) + .flat_map(|(_, _, r)| { + r.0.windows(2).map(|w| Extent::new(w[0].x.min(w[1].x), w[0].y.min(w[1].y), w[0].x.max(w[1].x), w[0].y.max(w[1].y))) + }) + .collect(); + assert!(edges.grid.cell_size().is_some()); + let mut seed = 7_u32; + let mut next = move || { + seed = seed.wrapping_mul(1_664_525).wrapping_add(1_013_904_223); + f64::from(seed >> 8) / f64::from(1_u32 << 24) + }; + let mut found = [0, 0]; + for _ in 0..4000 { + let (x, y, w, h) = (next() * 50.0 - 12.0, next() * 30.0 - 12.0, next() * 2.0, next() * 2.0); + let ext = Extent::new(x, y, x + w, y + h); + let want = boxes.iter().any(|b| b.intersects(&ext)); + assert_eq!(edges.near(&mask, &ext), want, "{ext:?}"); + found[usize::from(want)] += 1; + } + // Both answers came up. + assert!(found.iter().all(|&n| n > 100), "{found:?}"); + } + #[test] fn indexed_clip_handles_repeated_and_changing_neighbourhoods() { let mut masks = Dataset::new("masks", GeometryType::Polygon, vec![]); From 68f36b7d6d49b85f34200395ad76c0b00de27791 Mon Sep 17 00:00:00 2001 From: mavrick Date: Tue, 24 Nov 2026 15:37:39 -0600 Subject: [PATCH 3/7] fix(analysis): Clip sends a feature that isn't one valid shape to the overlay A ring that crosses itself inside a mask was kept as it was, where the overlay fills each of its lobes. A polygon feature is now validated once, before its masks are looked at: only a valid one with an area and finite coordinates is kept whole or dropped without the overlay. --- crates/analysis/src/tools.rs | 79 ++++++++++++++++++++++++------------ 1 file changed, 52 insertions(+), 27 deletions(-) diff --git a/crates/analysis/src/tools.rs b/crates/analysis/src/tools.rs index 3df214de41..44778f0455 100644 --- a/crates/analysis/src/tools.rs +++ b/crates/analysis/src/tools.rs @@ -1882,8 +1882,8 @@ enum Place { Inside, /// None of it is inside the mask, and it is clear of its edges: nothing is clipped from it. Outside, - /// An edge of the mask is within reach of its box (or it is degenerate, or has a non-finite - /// coordinate): only the overlay can say what is left. + /// An edge of the mask is within reach of its box, or a point of it is on an edge (or the mask + /// has a non-finite coordinate): only the overlay can say what is left. Edge, } @@ -1963,18 +1963,24 @@ fn rings_have_area(whole: &MultiPolygon, ext: &Extent) -> bool { }) } -/// Where `whole` (a feature's polygons, in the run's plane, dissolved) lies against `mask`, whose -/// edges are `edges`; `ext` is the feature's extent, padded as [`clip_on`] pads it for the -/// overlay's grid. With no mask edge in `ext`, the feature's outline can't meet the mask's or -/// hold a piece of it, so it is wholly in or wholly out, as any one of its points is. The -/// answer is exact, never a float comparison against an edge the feature might poke out of; -/// anything unsure (a collapsed ring, a coordinate that isn't finite, a point on an edge) is +/// Whether `whole` (a feature's polygons, in the run's plane, dissolved) can be kept as it is, or +/// dropped, without the overlay's say: its box `ext` isn't empty, its coordinates are finite, each +/// ring has an area, and it is valid (no ring crosses itself or another, a hole is inside its +/// polygon), so the overlay would only have rounded and rewound what is already one clean shape. +/// Made once for a feature, whatever number of masks it meets. +fn fit_to_place(whole: &MultiPolygon, ext: &Extent) -> bool { + use geo::Validation; + !ext.is_empty() && whole.coords_iter().all(|c| c.x.is_finite() && c.y.is_finite()) && rings_have_area(whole, ext) && whole.is_valid() +} + +/// Where `whole`, a feature [`fit_to_place`] accepts, lies against `mask`, whose edges are +/// `edges`; `ext` is the feature's extent, padded as [`clip_on`] pads it for the overlay's grid. +/// With no mask edge in `ext`, the feature's outline can't meet the mask's or hold a piece of it, +/// so it is wholly in or wholly out, as any one of its points is. The answer is exact, never a +/// float comparison against an edge the feature might poke out of; a point on an edge is /// [`Place::Edge`], for the overlay to decide. fn place_against(whole: &MultiPolygon, ext: &Extent, mask: &MultiPolygon, edges: &MaskEdges) -> Place { use geo::algorithm::coordinate_position::{CoordPos, CoordinatePosition}; - if ext.is_empty() || !whole.coords_iter().all(|c| c.x.is_finite() && c.y.is_finite()) || !rings_have_area(whole, ext) { - return Place::Edge; - } if edges.near(mask, ext) { return Place::Edge; } @@ -1988,7 +1994,7 @@ fn place_against(whole: &MultiPolygon, ext: &Extent, mask: &MultiPolygon, cache_bytes: Opti // wholly outside take nothing from it: the overlay (and the union of the masks // it meets) is only for those whose edges it may cross. let mut crossing = Cow::Borrowed(near.as_slice()); - if let Some(whole) = to_multipolygon(&shape) { + if let Some(whole) = to_multipolygon(&shape).filter(|whole| fit_to_place(whole, &ext)) { let mut edge = Vec::with_capacity(near.len()); for &i in &near { // A mask that isn't there (not so, as `near` is of the masks) is left to the overlay. @@ -13577,6 +13583,13 @@ mod tests { Coord::new(2.0, 1.0), Coord::new(1.0, 1.0), ]]]); + let bowtie = Geometry::Polygon(vec![vec![vec![ + Coord::new(1.0, 1.0), + Coord::new(4.0, 4.0), + Coord::new(1.0, 4.0), + Coord::new(3.0, 1.0), + Coord::new(1.0, 1.0), + ]]]); // (the feature, the area of what is kept) let cases = [ ("inside one mask", sq(2.0, 2.0, 1.0), 1.0), @@ -13592,12 +13605,16 @@ mod tests { for (i, (_, g, _)) in cases.iter().enumerate() { features.push(Some(g.clone()), vec![Value::Int(i as i64)]); } + // Inside one mask, but a ring that crosses itself: the overlay fills its two lobes. + let crossing_row = cases.len() as i64; + features.push(Some(bowtie.clone()), vec![Value::Int(crossing_row)]); let m = Map(HashMap::from([("masks".into(), masks), ("features".into(), features)])); let out = layer(crate::run("clip", &m, &serde_json::json!({"input":"features", "clip":"masks"})).unwrap()); let kept: Vec = out.features.iter().map(|f| f.attrs[0].as_f64().unwrap() as i64).collect(); - let want: Vec = cases.iter().enumerate().filter(|(_, (_, _, a))| *a > 0.0).map(|(i, _)| i as i64).collect(); + let want: Vec = + cases.iter().enumerate().filter(|(_, (_, _, a))| *a > 0.0).map(|(i, _)| i as i64).chain([crossing_row]).collect(); assert_eq!(kept, want); - for f in &out.features { + for f in out.features.iter().filter(|f| f.attrs[0].as_f64().unwrap() as i64 != crossing_row) { let (name, g, area) = &cases[f.attrs[0].as_f64().unwrap() as usize]; let got = to_multipolygon(f.geometry.as_ref().unwrap()).unwrap().unsigned_area(); let plane = to_multipolygon(g).unwrap().unsigned_area(); @@ -13617,14 +13634,19 @@ mod tests { assert!(same(by_row(0), sq(2.0, 2.0, 1.0))); assert!(same(by_row(1), sq(6.0, 6.0, 1.0))); assert!(same(by_row(2), sq(9.5, 9.5, 1.0))); + // The crossing ring is what the overlay makes of it: the two lobes, 3.9 square degrees. + let lobes = by_row(crossing_row).and_then(|g| to_multipolygon(&g)).unwrap().unsigned_area(); + let whole = to_multipolygon(&bowtie).unwrap().unsigned_area(); + assert!(lobes > 2.0 * whole, "{lobes} of {whole}"); let Some(Geometry::Polygon(wound)) = by_row(7) else { panic!("not a polygon") }; let ccw = to_multipolygon(&Geometry::Polygon(wound)).unwrap(); assert!(ccw.0[0].exterior().0.windows(2).map(|w| w[0].x * w[1].y - w[1].x * w[0].y).sum::() > 0.0); } /// A feature is `Inside` or `Outside` a mask only when no edge of it is within reach of the - /// feature's padded box; any edge there, a ring with no area, or a coordinate that isn't - /// finite, leaves it to the overlay. + /// feature's padded box; any edge there leaves it to the overlay, and so does a feature that + /// isn't one clean shape (no area, a coordinate that isn't finite, a ring that crosses itself, + /// a hole outside its polygon), whatever the mask. #[test] fn a_feature_is_placed_without_an_overlay_only_clear_of_the_mask_edges() { let mask = union_all(to_multipolygon(&sq(0.0, 0.0, 1.0)).unwrap().0.iter()); @@ -13641,16 +13663,19 @@ mod tests { assert_eq!(place(&sq(0.9, 0.4, 0.1)), Place::Edge); assert_eq!(place(&sq(0.4, 0.4, 0.6 - 0.0005)), Place::Edge); assert_eq!(place(&sq(-1.0, -1.0, 3.0)), Place::Edge); - // No area to keep, or no place to look at. - let flat = - Geometry::Polygon(vec![vec![vec![Coord::new(0.4, 0.4), Coord::new(0.5, 0.4), Coord::new(0.6, 0.4), Coord::new(0.4, 0.4)]]]); - assert_eq!(place(&flat), Place::Edge); - let mut bad = sq(0.4, 0.4, 0.1); - if let Geometry::Polygon(p) = &mut bad { - p[0][0][2] = Coord::new(f64::NAN, 0.5); - } - let ext = Extent::new(0.4, 0.4, 0.5, 0.5); - assert_eq!(place_against(&to_multipolygon(&bad).unwrap(), &ext, &mask, &edges), Place::Edge); + // Nothing to keep as it is: no area, a coordinate that isn't finite, a ring that crosses + // itself, a hole outside its polygon. + let fit = |g: &Geometry| fit_to_place(&to_multipolygon(g).unwrap(), &projected_extent(g).padded(0.0, 100.0)); + assert!(fit(&sq(0.4, 0.4, 0.1))); + let ring = |pts: &[(f64, f64)]| pts.iter().map(|&(x, y)| Coord::new(x, y)).collect::>(); + assert!(!fit(&Geometry::Polygon(vec![vec![ring(&[(0.4, 0.4), (0.5, 0.4), (0.6, 0.4), (0.4, 0.4)])]]))); + assert!(!fit(&Geometry::Polygon(vec![vec![ring(&[(0.4, 0.4), (0.5, f64::NAN), (0.5, 0.5), (0.4, 0.5), (0.4, 0.4)])]]))); + assert!(!fit(&Geometry::Polygon(vec![vec![ring(&[(0.1, 0.1), (0.4, 0.4), (0.1, 0.4), (0.3, 0.1), (0.1, 0.1)])]]))); + let hole_out = vec![ + ring(&[(0.1, 0.1), (0.2, 0.1), (0.2, 0.2), (0.1, 0.2), (0.1, 0.1)]), + ring(&[(0.5, 0.5), (0.6, 0.5), (0.6, 0.6), (0.5, 0.6), (0.5, 0.5)]), + ]; + assert!(!fit(&Geometry::Polygon(vec![hole_out]))); } /// The edges found near a box are the ones a scan of every edge's box finds, for a mask of From 73fd5b806676ad4302b7096c33055abc8083d811 Mon Sep 17 00:00:00 2001 From: mavrick Date: Tue, 24 Nov 2026 16:36:40 -0600 Subject: [PATCH 4/7] perf(analysis): Clip checks a feature is valid only when it would keep it whole The validity check ran for every polygon feature, with geo's unbounded one, before any mask was looked at, and it doesn't see a polygon whose hole splits its interior. It now runs once, with the bounded check Check Geometry uses, and only when a mask says the feature is inside it; a feature that isn't valid, or is too complex for the check, goes to the overlay as before. --- crates/analysis/src/tools.rs | 54 +++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/crates/analysis/src/tools.rs b/crates/analysis/src/tools.rs index 44778f0455..e33dc4acbf 100644 --- a/crates/analysis/src/tools.rs +++ b/crates/analysis/src/tools.rs @@ -1963,14 +1963,12 @@ fn rings_have_area(whole: &MultiPolygon, ext: &Extent) -> bool { }) } -/// Whether `whole` (a feature's polygons, in the run's plane, dissolved) can be kept as it is, or -/// dropped, without the overlay's say: its box `ext` isn't empty, its coordinates are finite, each -/// ring has an area, and it is valid (no ring crosses itself or another, a hole is inside its -/// polygon), so the overlay would only have rounded and rewound what is already one clean shape. -/// Made once for a feature, whatever number of masks it meets. +/// Whether `whole` (a feature's polygons, in the run's plane, dissolved) can be placed against the +/// masks without the overlay's say: its box `ext` isn't empty, its coordinates are finite and each +/// ring has an area. Made once for a feature, whatever number of masks it meets. Keeping it +/// whole also needs it to be valid ([`crate::validity`]), which is looked at only then. fn fit_to_place(whole: &MultiPolygon, ext: &Extent) -> bool { - use geo::Validation; - !ext.is_empty() && whole.coords_iter().all(|c| c.x.is_finite() && c.y.is_finite()) && rings_have_area(whole, ext) && whole.is_valid() + !ext.is_empty() && whole.coords_iter().all(|c| c.x.is_finite() && c.y.is_finite()) && rings_have_area(whole, ext) } /// Where `whole`, a feature [`fit_to_place`] accepts, lies against `mask`, whose edges are @@ -1994,7 +1992,7 @@ fn place_against(whole: &MultiPolygon, ext: &Extent, mask: &MultiPolygon, cache_bytes: Opti let mut crossing = Cow::Borrowed(near.as_slice()); if let Some(whole) = to_multipolygon(&shape).filter(|whole| fit_to_place(whole, &ext)) { let mut edge = Vec::with_capacity(near.len()); + let mut valid = None; for &i in &near { // A mask that isn't there (not so, as `near` is of the masks) is left to the overlay. let place = masks.get(i).zip(edges.get(i)).map_or(Place::Edge, |((mask, _), cell)| { place_against(&whole, &ext, mask, cell.get_or_init(|| MaskEdges::new(mask))) }); match place { - Place::Inside => { + // Kept as it is only if it is one valid shape (no ring crossing itself or + // another, a hole inside its polygon, its interior in one piece): the + // overlay would have rewritten anything else. Looked at once, and with a + // bounded amount of work, a feature too complex for it going to the overlay. + Place::Inside + if *valid.get_or_insert_with(|| { + crate::validity::check(Some(&shape), crate::validity::MAX_WORK).problems.is_empty() + }) => + { cache.big = None; return Ok(from_multipolygon(&whole.orient(Direction::Default)).map(|g| (g, false))); } + Place::Inside => edge.push(i), Place::Outside => {} Place::Edge => edge.push(i), } @@ -13608,13 +13616,21 @@ mod tests { // Inside one mask, but a ring that crosses itself: the overlay fills its two lobes. let crossing_row = cases.len() as i64; features.push(Some(bowtie.clone()), vec![Value::Int(crossing_row)]); + // Inside one mask, its hole touching the outline at two points: the interior is in two + // pieces, which the overlay returns as two polygons. + let split_row = crossing_row + 1; + let split = Geometry::Polygon(vec![vec![ + vec![Coord::new(1.0, 1.0), Coord::new(9.0, 1.0), Coord::new(9.0, 9.0), Coord::new(1.0, 9.0), Coord::new(1.0, 1.0)], + vec![Coord::new(1.0, 5.0), Coord::new(5.0, 2.0), Coord::new(9.0, 5.0), Coord::new(5.0, 8.0), Coord::new(1.0, 5.0)], + ]]); + features.push(Some(split), vec![Value::Int(split_row)]); let m = Map(HashMap::from([("masks".into(), masks), ("features".into(), features)])); let out = layer(crate::run("clip", &m, &serde_json::json!({"input":"features", "clip":"masks"})).unwrap()); let kept: Vec = out.features.iter().map(|f| f.attrs[0].as_f64().unwrap() as i64).collect(); let want: Vec = - cases.iter().enumerate().filter(|(_, (_, _, a))| *a > 0.0).map(|(i, _)| i as i64).chain([crossing_row]).collect(); + cases.iter().enumerate().filter(|(_, (_, _, a))| *a > 0.0).map(|(i, _)| i as i64).chain([crossing_row, split_row]).collect(); assert_eq!(kept, want); - for f in out.features.iter().filter(|f| f.attrs[0].as_f64().unwrap() as i64 != crossing_row) { + for f in out.features.iter().filter(|f| f.attrs[0].as_f64().unwrap() as i64 <= 7) { let (name, g, area) = &cases[f.attrs[0].as_f64().unwrap() as usize]; let got = to_multipolygon(f.geometry.as_ref().unwrap()).unwrap().unsigned_area(); let plane = to_multipolygon(g).unwrap().unsigned_area(); @@ -13635,6 +13651,8 @@ mod tests { assert!(same(by_row(1), sq(6.0, 6.0, 1.0))); assert!(same(by_row(2), sq(9.5, 9.5, 1.0))); // The crossing ring is what the overlay makes of it: the two lobes, 3.9 square degrees. + let Some(Geometry::Polygon(pieces)) = by_row(split_row) else { panic!("not a polygon") }; + assert_eq!(pieces.len(), 2); let lobes = by_row(crossing_row).and_then(|g| to_multipolygon(&g)).unwrap().unsigned_area(); let whole = to_multipolygon(&bowtie).unwrap().unsigned_area(); assert!(lobes > 2.0 * whole, "{lobes} of {whole}"); @@ -13644,9 +13662,8 @@ mod tests { } /// A feature is `Inside` or `Outside` a mask only when no edge of it is within reach of the - /// feature's padded box; any edge there leaves it to the overlay, and so does a feature that - /// isn't one clean shape (no area, a coordinate that isn't finite, a ring that crosses itself, - /// a hole outside its polygon), whatever the mask. + /// feature's padded box; any edge there leaves it to the overlay, and so does a feature with + /// no area or a coordinate that isn't finite, whatever the mask. #[test] fn a_feature_is_placed_without_an_overlay_only_clear_of_the_mask_edges() { let mask = union_all(to_multipolygon(&sq(0.0, 0.0, 1.0)).unwrap().0.iter()); @@ -13663,19 +13680,12 @@ mod tests { assert_eq!(place(&sq(0.9, 0.4, 0.1)), Place::Edge); assert_eq!(place(&sq(0.4, 0.4, 0.6 - 0.0005)), Place::Edge); assert_eq!(place(&sq(-1.0, -1.0, 3.0)), Place::Edge); - // Nothing to keep as it is: no area, a coordinate that isn't finite, a ring that crosses - // itself, a hole outside its polygon. + // Nothing to place: no area, or a coordinate that isn't finite. let fit = |g: &Geometry| fit_to_place(&to_multipolygon(g).unwrap(), &projected_extent(g).padded(0.0, 100.0)); assert!(fit(&sq(0.4, 0.4, 0.1))); let ring = |pts: &[(f64, f64)]| pts.iter().map(|&(x, y)| Coord::new(x, y)).collect::>(); assert!(!fit(&Geometry::Polygon(vec![vec![ring(&[(0.4, 0.4), (0.5, 0.4), (0.6, 0.4), (0.4, 0.4)])]]))); assert!(!fit(&Geometry::Polygon(vec![vec![ring(&[(0.4, 0.4), (0.5, f64::NAN), (0.5, 0.5), (0.4, 0.5), (0.4, 0.4)])]]))); - assert!(!fit(&Geometry::Polygon(vec![vec![ring(&[(0.1, 0.1), (0.4, 0.4), (0.1, 0.4), (0.3, 0.1), (0.1, 0.1)])]]))); - let hole_out = vec![ - ring(&[(0.1, 0.1), (0.2, 0.1), (0.2, 0.2), (0.1, 0.2), (0.1, 0.1)]), - ring(&[(0.5, 0.5), (0.6, 0.5), (0.6, 0.6), (0.5, 0.6), (0.5, 0.5)]), - ]; - assert!(!fit(&Geometry::Polygon(vec![hole_out]))); } /// The edges found near a box are the ones a scan of every edge's box finds, for a mask of From a09af578c90bb14576f0ba86a14d90c21d3535fc Mon Sep 17 00:00:00 2001 From: mavrick Date: Tue, 24 Nov 2026 17:21:02 -0600 Subject: [PATCH 5/7] fix(analysis): Clip counts its mask edge indexes in the scratch memory it plans The indexes are shared by the threads and kept for the run, about three times the masks' coordinates, and the planner didn't count them. They now come out of the unions' share of the scratch budget. --- crates/analysis/src/tools.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/crates/analysis/src/tools.rs b/crates/analysis/src/tools.rs index e33dc4acbf..b322c6f459 100644 --- a/crates/analysis/src/tools.rs +++ b/crates/analysis/src/tools.rs @@ -2170,9 +2170,10 @@ impl MaskCache { /// largest size of the masks one feature meets, together (found for each batch first, standing /// for their union); as many threads run as fit, at that much work each, in half the scratch /// budget, and their recent unions of several masks share `cache_bytes` (by default the other -/// half of [`crate::parallel::scratch_budget`]). The masks' edge indexes ([`MaskEdges`]) are -/// shared by the threads and kept for the run: only grid references to the masks' edges, which -/// the run holds already. +/// half of [`crate::parallel::scratch_budget`], less the masks' edge indexes). The masks' edge +/// indexes ([`MaskEdges`]) are shared by the threads and kept for the run: grid references to the +/// masks' edges, about three times the masks' coordinates, made when a polygon feature first needs +/// a mask's (holding each of its edges' boxes meanwhile, twice its coordinates, on one thread). fn clip_on(inp: &dyn Inputs, p: &Json, threads: Option, cache_bytes: Option) -> Result { let ds = input(inp, p, "input")?; let clip_ds = input(inp, p, "clip")?; @@ -2254,6 +2255,10 @@ fn clip_on(inp: &dyn Inputs, p: &Json, threads: Option, cache_bytes: Opti // one mask (at most all of them). Its size is taken as theirs together, which it doesn't // exceed by much (an overlay adds a vertex where edges cross, and takes away the overlaps). let mask_bytes: Vec = masks.iter().map(|(m, _)| kept_union_bytes(&[], m)).collect(); + // The masks' edge indexes ([`MaskEdges`]) hold about three times their masks' coordinates, at + // most (2.3 times on the benchmark's block groups, 3.1 on a smaller set): all of them, for the + // run. + let index_bytes = mask_bytes.iter().fold(0usize, |a, &b| a.saturating_add(b)).saturating_mul(3); let union_bytes = |near: &[usize]| match near { [] | [_] => 0, _ => near.iter().filter_map(|&i| mask_bytes.get(i)).fold(0usize, |a, &b| a.saturating_add(b)), @@ -2282,7 +2287,10 @@ fn clip_on(inp: &dyn Inputs, p: &Json, threads: Option, cache_bytes: Opti // Single candidates reuse their normalized mask. Each thread keeps eight recent multi-mask // unions, so alternating neighbourhoods can reuse work too, the least recently used going // first; the threads' unions together stay within their share of the scratch memory. - let cache_cap = cache_bytes.unwrap_or_else(|| crate::parallel::scratch_budget() / 2) / clip_threads.max(1); + // The masks' edge indexes come out of the unions' share, whether or not they have been + // made yet: they are shared by the threads and kept for the run. + let cache_cap = + cache_bytes.unwrap_or_else(|| (crate::parallel::scratch_budget() / 2).saturating_sub(index_bytes)) / clip_threads.max(1); let clipped = crate::parallel::try_map_on( batch, clip_threads, From 545e467e3558e2c17397eb5711b09809e56cc7ca Mon Sep 17 00:00:00 2001 From: mavrick Date: Tue, 24 Nov 2026 17:50:23 -0600 Subject: [PATCH 6/7] fix(analysis): Clip charges the mask edge indexes at what they hold and bounds the validity check The planner took three times the masks' coordinates for the indexes, which a mask of long diagonal edges exceeds; it now takes what each index made so far reports. The validity check, which holds about 100 bytes per coordinate, runs only for a feature of at most 10,000 coordinates; a larger one goes to the overlay. Two arms of the placement match are one. --- crates/analysis/src/tools.rs | 41 +++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/crates/analysis/src/tools.rs b/crates/analysis/src/tools.rs index b322c6f459..7f71c6faca 100644 --- a/crates/analysis/src/tools.rs +++ b/crates/analysis/src/tools.rs @@ -1887,6 +1887,11 @@ enum Place { Edge, } +/// The most coordinates a polygon feature may have to be kept whole without the overlay: the +/// validity check holds about 100 bytes for each ([`crate::validity`]), so a thread's share is +/// about a megabyte. +const KEEP_WHOLE_COORDS: usize = 10_000; + /// A clip mask's edges, on a grid, so that the edges near a feature are found without going /// through all of them. It holds the grid's references (one for each cell an edge's box meets) /// and a table of the mask's rings, not a second copy of its coordinates: an edge's ends are read @@ -1897,6 +1902,8 @@ struct MaskEdges { /// polygon, and which of the polygon's rings it is: 0 for the outside, then the holes. rings: Vec<(usize, usize, usize)>, grid: ExtentIndex, + /// What it holds, about: the grid's storage and the table of rings. + bytes: usize, /// The mask has a coordinate that isn't finite, which no grid cell holds reliably: every /// feature is then [`Place::Edge`]. unsure: bool, @@ -1924,7 +1931,9 @@ impl MaskEdges { let boxes = Self::rings_of(mask).flat_map(|(_, _, ring)| { ring.0.windows(2).map(|w| w.iter().fold(Extent::EMPTY, |e, c| e.union(&Extent::new(c.x, c.y, c.x, c.y)))) }); - MaskEdges { rings, grid: ExtentIndex::new(boxes), unsure } + let grid = ExtentIndex::new(boxes); + let bytes = grid.storage_bytes().saturating_add(rings.capacity().saturating_mul(std::mem::size_of::<(usize, usize, usize)>())); + MaskEdges { rings, grid, bytes, unsure } } /// The ends of edge `i` of `mask`, which this was made from. @@ -2170,10 +2179,10 @@ impl MaskCache { /// largest size of the masks one feature meets, together (found for each batch first, standing /// for their union); as many threads run as fit, at that much work each, in half the scratch /// budget, and their recent unions of several masks share `cache_bytes` (by default the other -/// half of [`crate::parallel::scratch_budget`], less the masks' edge indexes). The masks' edge -/// indexes ([`MaskEdges`]) are shared by the threads and kept for the run: grid references to the -/// masks' edges, about three times the masks' coordinates, made when a polygon feature first needs -/// a mask's (holding each of its edges' boxes meanwhile, twice its coordinates, on one thread). +/// half of [`crate::parallel::scratch_budget`], less the masks' edge indexes made so far). The masks' +/// edge indexes ([`MaskEdges`]) are shared by the threads and kept for the run: grid references to +/// the masks' edges, made when a polygon feature first needs a mask's (holding each of its edges' +/// boxes meanwhile, twice its coordinates, on one thread). fn clip_on(inp: &dyn Inputs, p: &Json, threads: Option, cache_bytes: Option) -> Result { let ds = input(inp, p, "input")?; let clip_ds = input(inp, p, "clip")?; @@ -2255,10 +2264,6 @@ fn clip_on(inp: &dyn Inputs, p: &Json, threads: Option, cache_bytes: Opti // one mask (at most all of them). Its size is taken as theirs together, which it doesn't // exceed by much (an overlay adds a vertex where edges cross, and takes away the overlaps). let mask_bytes: Vec = masks.iter().map(|(m, _)| kept_union_bytes(&[], m)).collect(); - // The masks' edge indexes ([`MaskEdges`]) hold about three times their masks' coordinates, at - // most (2.3 times on the benchmark's block groups, 3.1 on a smaller set): all of them, for the - // run. - let index_bytes = mask_bytes.iter().fold(0usize, |a, &b| a.saturating_add(b)).saturating_mul(3); let union_bytes = |near: &[usize]| match near { [] | [_] => 0, _ => near.iter().filter_map(|&i| mask_bytes.get(i)).fold(0usize, |a, &b| a.saturating_add(b)), @@ -2287,8 +2292,9 @@ fn clip_on(inp: &dyn Inputs, p: &Json, threads: Option, cache_bytes: Opti // Single candidates reuse their normalized mask. Each thread keeps eight recent multi-mask // unions, so alternating neighbourhoods can reuse work too, the least recently used going // first; the threads' unions together stay within their share of the scratch memory. - // The masks' edge indexes come out of the unions' share, whether or not they have been - // made yet: they are shared by the threads and kept for the run. + // The masks' edge indexes made so far come out of the unions' share, at what each holds + // ([`MaskEdges::bytes`]): they are shared by the threads and kept for the run. + let index_bytes = edges.iter().filter_map(std::sync::OnceLock::get).fold(0usize, |a, e| a.saturating_add(e.bytes)); let cache_cap = cache_bytes.unwrap_or_else(|| (crate::parallel::scratch_budget() / 2).saturating_sub(index_bytes)) / clip_threads.max(1); let clipped = crate::parallel::try_map_on( @@ -2319,19 +2325,20 @@ fn clip_on(inp: &dyn Inputs, p: &Json, threads: Option, cache_bytes: Opti match place { // Kept as it is only if it is one valid shape (no ring crossing itself or // another, a hole inside its polygon, its interior in one piece): the - // overlay would have rewritten anything else. Looked at once, and with a - // bounded amount of work, a feature too complex for it going to the overlay. + // overlay would have rewritten anything else. Looked at once; a feature of + // more than [`KEEP_WHOLE_COORDS`] coordinates, which the check would need + // about 100 bytes each for, goes to the overlay. Place::Inside if *valid.get_or_insert_with(|| { - crate::validity::check(Some(&shape), crate::validity::MAX_WORK).problems.is_empty() + shape.coord_count() <= KEEP_WHOLE_COORDS + && crate::validity::check(Some(&shape), crate::validity::MAX_WORK).problems.is_empty() }) => { cache.big = None; return Ok(from_multipolygon(&whole.orient(Direction::Default)).map(|g| (g, false))); } - Place::Inside => edge.push(i), + Place::Inside | Place::Edge => edge.push(i), Place::Outside => {} - Place::Edge => edge.push(i), } } if edge.is_empty() { @@ -13658,7 +13665,7 @@ mod tests { assert!(same(by_row(0), sq(2.0, 2.0, 1.0))); assert!(same(by_row(1), sq(6.0, 6.0, 1.0))); assert!(same(by_row(2), sq(9.5, 9.5, 1.0))); - // The crossing ring is what the overlay makes of it: the two lobes, 3.9 square degrees. + // The crossing ring is what the overlay makes of it: its two lobes, well over the net area. let Some(Geometry::Polygon(pieces)) = by_row(split_row) else { panic!("not a polygon") }; assert_eq!(pieces.len(), 2); let lobes = by_row(crossing_row).and_then(|g| to_multipolygon(&g)).unwrap().unsigned_area(); From a9029db189005a81412c10ecd11aee851956303e Mon Sep 17 00:00:00 2001 From: mavrick Date: Tue, 24 Nov 2026 18:40:20 -0600 Subject: [PATCH 7/7] perf(analysis): Clip keeps a feature whole only if its rings are valid in the overlay's plane Review: a ring simple in longitude/latitude can cross itself once projected, so the check now looks at the projected rings, which the overlay would have rewritten. --- crates/analysis/src/tools.rs | 44 ++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/crates/analysis/src/tools.rs b/crates/analysis/src/tools.rs index 7f71c6faca..75eb9c8336 100644 --- a/crates/analysis/src/tools.rs +++ b/crates/analysis/src/tools.rs @@ -1857,6 +1857,13 @@ fn part(g: &Geometry, mask: &MultiPolygon, inside: bool) -> Option) -> Geometry { + let ring = |r: &geo::LineString| r.0.iter().map(|c| Coord::new(c.x, c.y)).collect(); + Geometry::Polygon(mp.0.iter().map(|p| std::iter::once(p.exterior()).chain(p.interiors()).map(ring).collect()).collect()) +} + /// Clip's part and whether its total polygon area is less than one millionth of the input's: /// a possible boundary sliver, kept unchanged. Measure both in the overlay plane while they /// are already projected, once per feature, without projecting the result back again to measure @@ -2324,14 +2331,16 @@ fn clip_on(inp: &dyn Inputs, p: &Json, threads: Option, cache_bytes: Opti }); match place { // Kept as it is only if it is one valid shape (no ring crossing itself or - // another, a hole inside its polygon, its interior in one piece): the - // overlay would have rewritten anything else. Looked at once; a feature of - // more than [`KEEP_WHOLE_COORDS`] coordinates, which the check would need - // about 100 bytes each for, goes to the overlay. + // another, a hole inside its polygon, its interior in one piece) in the + // run's plane, where the overlay works: it would have rewritten anything + // else, and a ring simple in longitude/latitude can cross itself once + // projected. Looked at once; a feature of more than [`KEEP_WHOLE_COORDS`] + // coordinates, which the check would need about 100 bytes each for, goes + // to the overlay. Place::Inside if *valid.get_or_insert_with(|| { shape.coord_count() <= KEEP_WHOLE_COORDS - && crate::validity::check(Some(&shape), crate::validity::MAX_WORK).problems.is_empty() + && crate::validity::check(Some(&in_plane(&whole)), crate::validity::MAX_WORK).problems.is_empty() }) => { cache.big = None; @@ -13676,6 +13685,31 @@ mod tests { assert!(ccw.0[0].exterior().0.windows(2).map(|w| w[0].x * w[1].y - w[1].x * w[0].y).sum::() > 0.0); } + /// A ring that doesn't cross itself in longitude/latitude but does in the plane the overlay + /// works in (Mercator stretches the north more) isn't kept as it is inside a mask: it goes + /// to the overlay, as it did before features clear of the edges were kept whole. + #[test] + fn clip_overlays_a_ring_that_crosses_itself_only_once_projected() { + let ring = Geometry::Polygon(vec![vec![vec![ + Coord::new(0.0, 0.0), + Coord::new(10.0, 80.0), + Coord::new(0.0, 80.0), + Coord::new(9.0, 73.0), + Coord::new(0.0, 0.0), + ]]]); + assert!(crate::validity::check(Some(&ring), crate::validity::MAX_WORK).problems.is_empty()); + let mut masks = Dataset::new("masks", GeometryType::Polygon, vec![]); + masks.push(Some(sq(-20.0, -20.0, 104.0)), vec![]); + let mut features = Dataset::new("features", GeometryType::Polygon, vec![]); + features.push(Some(ring.clone()), vec![]); + let m = Map(HashMap::from([("masks".into(), masks), ("features".into(), features)])); + let out = layer(crate::run("clip", &m, &serde_json::json!({"input":"features", "clip":"masks"})).unwrap()); + let Some(Some(Geometry::Polygon(got))) = out.features.first().map(|f| f.geometry.clone()) else { panic!("no polygon") }; + let Geometry::Polygon(was) = ring else { panic!("not a polygon") }; + // The overlay splits it where it crosses: more than the one ring of five vertices. + assert_ne!(got.iter().flatten().map(Vec::len).collect::>(), was.iter().flatten().map(Vec::len).collect::>()); + } + /// A feature is `Inside` or `Outside` a mask only when no edge of it is within reach of the /// feature's padded box; any edge there leaves it to the overlay, and so does a feature with /// no area or a coordinate that isn't finite, whatever the mask.