From 3506415af7844603ae77340c1f28dda83c25fe3b Mon Sep 17 00:00:00 2001 From: mavrick Date: Wed, 25 Nov 2026 09:59:34 -0600 Subject: [PATCH 1/3] perf(analysis): IDW from every point weighs each cell in one pass, with no buffer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When every point is weighed (0 neighbours, or as many as there are points), each cell no longer fills a vector with every point's distance and value and makes a second pass over it. The points are kept as separate x, y and value columns, and one pass adds up w·v and w with w = 1/d² at a power of 2, in eight fixed lanes so the loop goes in vector instructions and the sums are added in the same order on any thread. At another power, or should 1/d² overflow or vanish, a first pass finds the nearest point and a second weighs each by (dₙ²/d²)^(p/2), the ratios relative to the nearest point that keep a high power from overflowing. A cell on a point still takes the first such point's value. The nearest-few path is unchanged. --- crates/analysis/src/rasterops.rs | 122 ++++++++++++++++++++++++++++++- 1 file changed, 118 insertions(+), 4 deletions(-) diff --git a/crates/analysis/src/rasterops.rs b/crates/analysis/src/rasterops.rs index f6a5d91b67..a28606a4a4 100644 --- a/crates/analysis/src/rasterops.rs +++ b/crates/analysis/src/rasterops.rs @@ -821,6 +821,113 @@ pub fn idw( idw_on(None, points, extent, cell, power, neighbours, name, crs) } +/// How near a cell must be to a point, as a squared distance, to take its value outright. +const IDW_EXACT_D2: f64 = 1e-24; + +/// The separate sums a cell's pass over every point keeps: a fixed number, so that a cell's sums +/// are added in the same order on any thread, and enough for the compiler to add them in vectors. +const IDW_LANES: usize = 8; + +/// The points of an [`idw`] from every point as separate x, y and value columns, which a cell's +/// pass reads in step, and so in vectors. +struct IdwColumns { + x: Vec, + y: Vec, + v: Vec, +} + +impl IdwColumns { + fn new(points: &[(Coord, f64)]) -> Self { + Self { + x: points.iter().map(|(p, _)| p.x).collect(), + y: points.iter().map(|(p, _)| p.y).collect(), + v: points.iter().map(|(_, v)| *v).collect(), + } + } + + /// The value at `c`, east-west distances times `kx`: that of the first point within + /// [`IDW_EXACT_D2`] of it, else the mean of every point's value weighted by 1/dᵖ. + /// + /// At a power of 2 that is one pass, with w = 1/d². At any other power, or should 1/d² + /// overflow or vanish (at distances far from any map's), a first pass finds the nearest point + /// and a second weighs each by (dₙ²/d²)^(p/2): the same ratios, which can't overflow for a + /// high power. + fn at(&self, c: Coord, kx: f64, power: f64) -> f64 { + let nearest = if power == 2.0 { + let (num, den, nearest) = self.sums(c, kx, |d2| 1.0 / d2); + if nearest >= IDW_EXACT_D2 && num.is_finite() && den.is_finite() && den > 0.0 { + return num / den; + } + nearest + } else { + self.nearest(c, kx) + }; + if nearest < IDW_EXACT_D2 { + let on = self.x.iter().zip(&self.y).zip(&self.v).find(|((x, y), _)| d2(c, kx, **x, **y) < IDW_EXACT_D2); + return on.map_or(f64::NAN, |(_, v)| *v); + } + let half = power / 2.0; + let (num, den, _) = self.sums(c, kx, |d2| (nearest / d2).powf(half)); + num / den + } + + /// The smallest squared distance from `c` to a point. + fn nearest(&self, c: Coord, kx: f64) -> f64 { + let mut near = [f64::INFINITY; IDW_LANES]; + let (xs, x_rest) = self.x.as_chunks::(); + let (ys, y_rest) = self.y.as_chunks::(); + for (x, y) in xs.iter().zip(ys) { + for ((m, x), y) in near.iter_mut().zip(x).zip(y) { + *m = smaller(*m, d2(c, kx, *x, *y)); + } + } + for ((m, x), y) in near.iter_mut().zip(x_rest).zip(y_rest) { + *m = smaller(*m, d2(c, kx, *x, *y)); + } + near.into_iter().fold(f64::INFINITY, smaller) + } + + /// Over every point, the sums of w·v and of w, w the `weight` of its squared distance from + /// `c`, and the smallest squared distance: in [`IDW_LANES`] lanes, each sum in an array of its + /// own so they go in vectors, added up in a fixed order. + #[inline(always)] + fn sums(&self, c: Coord, kx: f64, weight: impl Fn(f64) -> f64) -> (f64, f64, f64) { + let (mut num, mut den, mut near) = ([0.0; IDW_LANES], [0.0; IDW_LANES], [f64::INFINITY; IDW_LANES]); + let mut add = |x: &[f64], y: &[f64], v: &[f64]| { + for (((((n, dn), m), x), y), v) in num.iter_mut().zip(den.iter_mut()).zip(near.iter_mut()).zip(x).zip(y).zip(v) { + let d = d2(c, kx, *x, *y); + let w = weight(d); + *n += w * v; + *dn += w; + *m = smaller(*m, d); + } + }; + let (xs, x_rest) = self.x.as_chunks::(); + let (ys, y_rest) = self.y.as_chunks::(); + let (vs, v_rest) = self.v.as_chunks::(); + for ((x, y), v) in xs.iter().zip(ys).zip(vs) { + add(x, y, v); + } + add(x_rest, y_rest, v_rest); + (num.into_iter().sum(), den.into_iter().sum(), near.into_iter().fold(f64::INFINITY, smaller)) + } +} + +/// The smaller of `a` and `b`, or `a` when `b` is not a number: as `f64::min` for the distances +/// here (`a` is never NaN), in a form that goes in vector instructions. +#[inline(always)] +fn smaller(a: f64, b: f64) -> f64 { + if b < a { b } else { a } +} + +/// The squared distance from `c` to (x, y), east-west distances times `kx`. +#[inline(always)] +fn d2(c: Coord, kx: f64, x: f64, y: f64) -> f64 { + let dx = (x - c.x) * kx; + let dy = y - c.y; + dx * dx + dy * dy +} + /// [`idw`] on `threads` threads, or as many as its work calls for. fn idw_on( threads: Option, @@ -854,10 +961,12 @@ fn idw_on( // the nearest points are the same. let squared = power == 2.0; let geographic = crs.is_geographic(); - let exact = if squared { 1e-24 } else { 1e-12 }; + let exact = if squared { IDW_EXACT_D2 } else { 1e-12 }; + // With every point weighed, each cell takes one pass over them as columns, with no buffer. + let every = (k == points.len()).then(|| IdwColumns::new(points)); // Each row on its own, on several threads when the work is large (#1252); a thread's - // distances go into its own buffer. - let scratch_bytes = points.len().saturating_mul(std::mem::size_of::<(f64, f64)>()); + // distances for the nearest few go into its own buffer. + let scratch_bytes = if every.is_some() { 0 } else { points.len().saturating_mul(std::mem::size_of::<(f64, f64)>()) }; // Every cell weighs every point: as many times the work as there are threads (#1291). let work = w.saturating_mul(h).saturating_mul(points.len()); let threads = threads.unwrap_or_else(|| crate::parallel::threads_for_rows(h, work, scratch_bytes)); @@ -874,7 +983,7 @@ fn idw_on( crate::parallel::for_each_row( data.chunks_mut(w), threads, - || (Vec::with_capacity(points.len()), crate::control::Pace::default()), + || (Vec::with_capacity(if every.is_some() { 0 } else { points.len() }), crate::control::Pace::default()), |(near, pace): &mut (Vec<(f64, f64)>, crate::control::Pace), row, out: &mut [f32]| { // East-west degrees shrink with latitude, which only changes by row; a projected // system's units don't. @@ -886,6 +995,11 @@ fn idw_on( return; } let c = Coord::new(extent.min_x + (col as f64 + 0.5) * cell, extent.max_y - (row as f64 + 0.5) * cell); + if let Some(every) = &every { + let v = every.at(c, kx, power); + *o = if v.is_finite() { v as f32 } else { NODATA }; + continue; + } near.clear(); let d2 = |p: &Coord| { let dx = (p.x - c.x) * kx; From c7e30a9bb3f4a90bf85455c8599672a2cd830027 Mon Sep 17 00:00:00 2001 From: mavrick Date: Wed, 25 Nov 2026 10:34:51 -0600 Subject: [PATCH 2/3] fix(analysis): IDW keeps a far point's weight at a small power, and makes its columns only for a run it takes At a power other than 2, a point whose squared distance is past f64's range of the nearest's lost its weight: the squared ratio vanished where the ratio of distances, raised to a small power, still weighs something. Such a point is now weighed from the distances, as before the one-pass change. The points' columns are made after the work cap is checked, so a run refused for too much work no longer copies them first. The every-point pass's doc says what makes it fall back (sums that overflow, weights that vanish), and the nearest-few path measures with the same squared-distance helper instead of a closure of its own. --- crates/analysis/src/rasterops.rs | 44 ++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/crates/analysis/src/rasterops.rs b/crates/analysis/src/rasterops.rs index a28606a4a4..874f735ecd 100644 --- a/crates/analysis/src/rasterops.rs +++ b/crates/analysis/src/rasterops.rs @@ -848,10 +848,10 @@ impl IdwColumns { /// The value at `c`, east-west distances times `kx`: that of the first point within /// [`IDW_EXACT_D2`] of it, else the mean of every point's value weighted by 1/dᵖ. /// - /// At a power of 2 that is one pass, with w = 1/d². At any other power, or should 1/d² - /// overflow or vanish (at distances far from any map's), a first pass finds the nearest point - /// and a second weighs each by (dₙ²/d²)^(p/2): the same ratios, which can't overflow for a - /// high power. + /// At a power of 2 that is one pass, with w = 1/d², at most 1e24 once no point is within + /// [`IDW_EXACT_D2`]. At any other power, or should the sums overflow (from huge values) or the + /// weights vanish (at distances near f64's limit), a first pass finds the nearest point and a + /// second weighs each by (dₙ²/d²)^(p/2): the same ratios, which can't overflow for a high power. fn at(&self, c: Coord, kx: f64, power: f64) -> f64 { let nearest = if power == 2.0 { let (num, den, nearest) = self.sums(c, kx, |d2| 1.0 / d2); @@ -867,7 +867,12 @@ impl IdwColumns { return on.map_or(f64::NAN, |(_, v)| *v); } let half = power / 2.0; - let (num, den, _) = self.sums(c, kx, |d2| (nearest / d2).powf(half)); + let (num, den, _) = self.sums(c, kx, |d2| { + // Past f64's range the squared ratio would vanish where the ratio of distances, raised + // to a small power, still weighs something: then the distances are taken. + let ratio = nearest / d2; + if ratio >= f64::MIN_POSITIVE { ratio.powf(half) } else { (nearest.sqrt() / d2.sqrt()).powf(power) } + }); num / den } @@ -963,10 +968,10 @@ fn idw_on( let geographic = crs.is_geographic(); let exact = if squared { IDW_EXACT_D2 } else { 1e-12 }; // With every point weighed, each cell takes one pass over them as columns, with no buffer. - let every = (k == points.len()).then(|| IdwColumns::new(points)); + let all = k == points.len(); // Each row on its own, on several threads when the work is large (#1252); a thread's // distances for the nearest few go into its own buffer. - let scratch_bytes = if every.is_some() { 0 } else { points.len().saturating_mul(std::mem::size_of::<(f64, f64)>()) }; + let scratch_bytes = if all { 0 } else { points.len().saturating_mul(std::mem::size_of::<(f64, f64)>()) }; // Every cell weighs every point: as many times the work as there are threads (#1291). let work = w.saturating_mul(h).saturating_mul(points.len()); let threads = threads.unwrap_or_else(|| crate::parallel::threads_for_rows(h, work, scratch_bytes)); @@ -978,12 +983,13 @@ fn idw_on( crate::parallel::UNCAPPED_HINT )); } + let every = all.then(|| IdwColumns::new(points)); let mut data = new_band(w, h, NODATA)?; crate::control::begin(h); crate::parallel::for_each_row( data.chunks_mut(w), threads, - || (Vec::with_capacity(if every.is_some() { 0 } else { points.len() }), crate::control::Pace::default()), + || (Vec::with_capacity(if all { 0 } else { points.len() }), crate::control::Pace::default()), |(near, pace): &mut (Vec<(f64, f64)>, crate::control::Pace), row, out: &mut [f32]| { // East-west degrees shrink with latitude, which only changes by row; a projected // system's units don't. @@ -1001,15 +1007,11 @@ fn idw_on( continue; } near.clear(); - let d2 = |p: &Coord| { - let dx = (p.x - c.x) * kx; - dx * dx + (p.y - c.y).powi(2) - }; // One loop or the other, each kept tight. if squared { - near.extend(points.iter().map(|(p, v)| (d2(p), *v))); + near.extend(points.iter().map(|(p, v)| (d2(c, kx, p.x, p.y), *v))); } else { - near.extend(points.iter().map(|(p, v)| (d2(p).sqrt(), *v))); + near.extend(points.iter().map(|(p, v)| (d2(c, kx, p.x, p.y).sqrt(), *v))); } if k < near.len() { near.select_nth_unstable_by(k - 1, |a, b| a.0.partial_cmp(&b.0).unwrap_or(std::cmp::Ordering::Equal)); @@ -2655,6 +2657,20 @@ mod tests { assert_eq!(idw_on(Some(1), &pts, &extent, 0.01, 2.0, 0, "idw", Crs::LonLat).unwrap_err(), crate::control::CANCELLED); } + /// From every point at a small power, a point so far that its squared distance's ratio to the + /// nearest's is past f64's range still pulls the cell: its weight, (dₙ/d)ᵖ, isn't lost to the + /// squares. + #[test] + fn idw_from_every_point_keeps_a_far_points_weight_at_a_small_power() { + let pts = [(Coord::new(1e-10, 0.0), 0.0), (Coord::new(1e154, 0.0), 100.0)]; + let g = idw(&pts, &Extent::new(-0.5, -0.5, 0.5, 0.5), 1.0, 0.01, 0, "idw", Crs::LonLat).unwrap_or_else(|e| panic!("{e}")); + // The cell at (0, 0): weights 1 and (1e-10 / 1e154)^0.01 = 10^-1.64. + let far = 10f64.powf(-1.64); + let want = (100.0 * far / (1.0 + far)) as f32; + let got = g.bands[0].data[0]; + assert!((got - want).abs() <= want * 1e-5, "{got} vs {want}"); + } + /// Rows made on several threads give the same surface, bit for bit, as one thread (#1252), /// from every point and from the nearest few. #[test] From fba6b1498b0061b0ad3d5b484360c72976ec3d0e Mon Sep 17 00:00:00 2001 From: mavrick Date: Wed, 25 Nov 2026 12:53:21 -0600 Subject: [PATCH 3/3] fix(analysis): IDW from every point keeps values whose weighted terms underflow MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit At a power of 2, the one pass weighs each point by 1/d². Where every 1/d² · v underflowed (points very far off, or tiny values), the sums stayed finite and the cell took 0. The pass's result now stands only when the sum of the weights, and that sum times the largest |value|, are at least 1e-200, so that what underflowed moves it by less than 10⁻⁹⁹ of the largest value; otherwise the cell is weighed relative to its nearest point. A field of zeros keeps the one pass. The bound is taken once a run, so it costs the pass nothing. The flag for weighing every point is named for that. --- crates/analysis/src/rasterops.rs | 44 ++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/crates/analysis/src/rasterops.rs b/crates/analysis/src/rasterops.rs index 874f735ecd..edc093df80 100644 --- a/crates/analysis/src/rasterops.rs +++ b/crates/analysis/src/rasterops.rs @@ -824,6 +824,12 @@ pub fn idw( /// How near a cell must be to a point, as a squared distance, to take its value outright. const IDW_EXACT_D2: f64 = 1e-24; +/// The least that the sum of the weights of a pass at a power of 2, and that sum times the largest +/// |value|, may be for its result to stand: the terms that underflowed, each below +/// `f64::MIN_POSITIVE`, then move it by less than 10⁻⁹⁹ of the largest value, even from as many +/// points as a run takes. +const IDW_SAFE_SCALE: f64 = 1e-200; + /// The separate sums a cell's pass over every point keeps: a fixed number, so that a cell's sums /// are added in the same order on any thread, and enough for the compiler to add them in vectors. const IDW_LANES: usize = 8; @@ -834,6 +840,8 @@ struct IdwColumns { x: Vec, y: Vec, v: Vec, + /// The largest |value|. + largest: f64, } impl IdwColumns { @@ -842,6 +850,7 @@ impl IdwColumns { x: points.iter().map(|(p, _)| p.x).collect(), y: points.iter().map(|(p, _)| p.y).collect(), v: points.iter().map(|(_, v)| *v).collect(), + largest: points.iter().fold(0.0, |m, (_, v)| larger(m, v.abs())), } } @@ -849,13 +858,17 @@ impl IdwColumns { /// [`IDW_EXACT_D2`] of it, else the mean of every point's value weighted by 1/dᵖ. /// /// At a power of 2 that is one pass, with w = 1/d², at most 1e24 once no point is within - /// [`IDW_EXACT_D2`]. At any other power, or should the sums overflow (from huge values) or the - /// weights vanish (at distances near f64's limit), a first pass finds the nearest point and a - /// second weighs each by (dₙ²/d²)^(p/2): the same ratios, which can't overflow for a high power. + /// [`IDW_EXACT_D2`]. At any other power, or should the sums overflow (from huge values) or + /// their terms underflow (from tiny values or distances near f64's limit, below + /// [`IDW_SAFE_SCALE`]), a first pass finds the nearest point and a second weighs each by + /// (dₙ²/d²)^(p/2): the same ratios, which keep the nearest point's weight at 1 and can't + /// overflow for a high power. fn at(&self, c: Coord, kx: f64, power: f64) -> f64 { let nearest = if power == 2.0 { let (num, den, nearest) = self.sums(c, kx, |d2| 1.0 / d2); - if nearest >= IDW_EXACT_D2 && num.is_finite() && den.is_finite() && den > 0.0 { + // With every value 0 the sum is exactly 0, whatever underflowed. + let scaled = den >= IDW_SAFE_SCALE && (self.largest == 0.0 || den * self.largest >= IDW_SAFE_SCALE); + if nearest >= IDW_EXACT_D2 && num.is_finite() && den.is_finite() && scaled { return num / den; } nearest @@ -918,6 +931,12 @@ impl IdwColumns { } } +/// The larger of `a` and `b`, or `a` when `b` is not a number. +#[inline(always)] +fn larger(a: f64, b: f64) -> f64 { + if b > a { b } else { a } +} + /// The smaller of `a` and `b`, or `a` when `b` is not a number: as `f64::min` for the distances /// here (`a` is never NaN), in a form that goes in vector instructions. #[inline(always)] @@ -968,10 +987,10 @@ fn idw_on( let geographic = crs.is_geographic(); let exact = if squared { IDW_EXACT_D2 } else { 1e-12 }; // With every point weighed, each cell takes one pass over them as columns, with no buffer. - let all = k == points.len(); + let every_point = k == points.len(); // Each row on its own, on several threads when the work is large (#1252); a thread's // distances for the nearest few go into its own buffer. - let scratch_bytes = if all { 0 } else { points.len().saturating_mul(std::mem::size_of::<(f64, f64)>()) }; + let scratch_bytes = if every_point { 0 } else { points.len().saturating_mul(std::mem::size_of::<(f64, f64)>()) }; // Every cell weighs every point: as many times the work as there are threads (#1291). let work = w.saturating_mul(h).saturating_mul(points.len()); let threads = threads.unwrap_or_else(|| crate::parallel::threads_for_rows(h, work, scratch_bytes)); @@ -983,13 +1002,13 @@ fn idw_on( crate::parallel::UNCAPPED_HINT )); } - let every = all.then(|| IdwColumns::new(points)); + let every = every_point.then(|| IdwColumns::new(points)); let mut data = new_band(w, h, NODATA)?; crate::control::begin(h); crate::parallel::for_each_row( data.chunks_mut(w), threads, - || (Vec::with_capacity(if all { 0 } else { points.len() }), crate::control::Pace::default()), + || (Vec::with_capacity(if every_point { 0 } else { points.len() }), crate::control::Pace::default()), |(near, pace): &mut (Vec<(f64, f64)>, crate::control::Pace), row, out: &mut [f32]| { // East-west degrees shrink with latitude, which only changes by row; a projected // system's units don't. @@ -2671,6 +2690,15 @@ mod tests { assert!((got - want).abs() <= want * 1e-5, "{got} vs {want}"); } + /// From every point at a power of 2, points so far that each 1/d² · v underflows to nothing + /// still give their surface: a constant one stays that constant. + #[test] + fn idw_from_every_point_keeps_values_whose_weighted_terms_underflow() { + let pts = [(Coord::new(-1e150, 0.0), 1e-30), (Coord::new(1e150, 0.0), 1e-30)]; + let g = idw(&pts, &Extent::new(-0.5, -0.5, 0.5, 0.5), 1.0, 2.0, 0, "idw", Crs::Epsg(3857)).unwrap_or_else(|e| panic!("{e}")); + assert_eq!(g.bands[0].data[0], 1e-30_f32); + } + /// Rows made on several threads give the same surface, bit for bit, as one thread (#1252), /// from every point and from the nearest few. #[test]