From 26904e37d8b0dc082cf247722552db8d4e1127f1 Mon Sep 17 00:00:00 2001 From: Omar Sobh Date: Tue, 15 Sep 2026 23:35:40 -0500 Subject: [PATCH] PERF-2 P1.b: the polyline index prunes from the start with an upper bound from each chunk's first vertex (never the answer; the strict-minimum scan rule is unchanged), so a query near the polygon's far end no longer scans every chunk; pin unchanged, 0 mismatches --- crates/specialized/rtx-cfd/src/mesh/patch_gen.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/crates/specialized/rtx-cfd/src/mesh/patch_gen.rs b/crates/specialized/rtx-cfd/src/mesh/patch_gen.rs index 162a18f..f040817 100644 --- a/crates/specialized/rtx-cfd/src/mesh/patch_gen.rs +++ b/crates/specialized/rtx-cfd/src/mesh/patch_gen.rs @@ -330,10 +330,21 @@ impl<'a> PolylineIndex<'a> { let pts = self.pts; let n = pts.len(); let (mut best, mut best_d) = (pts[0], f64::INFINITY); + // An UPPER bound on the minimum from each chunk's first vertex (a + // point of the polyline, so no segment can beat it by more than the + // rounding margin): chunks farther than it are pruned from the + // start, and the bound never becomes the answer — the scan below + // keeps the brute-force rule (first segment at the strict minimum). + let mut prune_d = f64::INFINITY; + for c in 0..self.boxes.len() { + let a = pts[c * self.chunk]; + let d = (a[0] - q[0]).powi(2) + (a[1] - q[1]).powi(2); + prune_d = prune_d.min(d); + } for (c, b) in self.boxes.iter().enumerate() { let dx = (b[0] - q[0]).max(0.0).max(q[0] - b[1]); let dy = (b[2] - q[1]).max(0.0).max(q[1] - b[3]); - if dx * dx + dy * dy > best_d * (1.0 + 1e-12) { + if dx * dx + dy * dy > best_d.min(prune_d) * (1.0 + 1e-12) { continue; } let start = c * self.chunk;