From 9c71bf17c5e64b2afbc2d5086e3e705e59a97a00 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 4 Jun 2026 09:28:02 +0000 Subject: [PATCH] perf(manifold): replace libm::sqrt with squared distance in is_neighbor Optimizes the `is_neighbor` check used in `SparseAttentionGraph::add_point` by comparing squared distances instead of calling the expensive `libm::sqrt`. Includes early exit capabilities and correctly handles NaN/negative thresholds and coordinates. Co-authored-by: teerthsharma <78080953+teerthsharma@users.noreply.github.com> --- .jules/bolt.md | 4 ++++ crates/aether-core/src/manifold.rs | 15 +++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/.jules/bolt.md b/.jules/bolt.md index 2ac6922..2b056f2 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -5,3 +5,7 @@ ## 2026-05-01 - Avoid High-Level Tensor Ops in Scalar Reductions **Learning:** High-level `Tensor` operations like `sub()` and `mul()` trigger intermediate heap allocations for shape and stride metadata. When computing scalar reductions (like MSE, distances, or loss functions), using these operations introduces severe memory overhead inside hot loops. Attempting to use `.min()` length truncation as a safeguard is an anti-pattern as it masks shape mismatch errors. **Action:** For scalar reductions, assert shape equality (`assert_eq!(a.shape, b.shape)`) and perform a single-pass iteration directly over the underlying borrowed data arrays (`a.data.borrow()`) to eliminate intermediate allocations and safely compute the result. + +## 2026-06-04 - Avoid libm::sqrt in Distance Comparisons +**Learning:** In `aether-core::manifold::SparseAttentionGraph::add_point`, the `is_neighbor` function is a critical hot path used to build the spatial proximity graph in $O(N)$ comparisons for each added point. The previous implementation computed the exact Euclidean distance involving an expensive `libm::sqrt` call. Since we only need to compare against a threshold (`epsilon`), computing the squared distance (`d^2 < r^2`) is sufficient and eliminates the costly square root operation. +**Action:** Always prefer squared distance comparisons over `sqrt` in hot spatial query loops. Use early exits (`!(sum < eps_sq)`) inside the summation loop to further short-circuit the computation, and handle `NaN` cleanly by comparing using `!` logic rather than explicitly matching. diff --git a/crates/aether-core/src/manifold.rs b/crates/aether-core/src/manifold.rs index 1bcc201..17d93bb 100644 --- a/crates/aether-core/src/manifold.rs +++ b/crates/aether-core/src/manifold.rs @@ -24,7 +24,6 @@ // ═══════════════════════════════════════════════════════════════════════════════ // - #![allow(dead_code)] use libm::sqrt; @@ -76,7 +75,19 @@ impl ManifoldPoint { /// Check if within epsilon-neighborhood (sparse attention criterion) pub fn is_neighbor(&self, other: &Self, epsilon: f64) -> bool { - self.distance(other) < epsilon + if !(epsilon > 0.0) { + return false; + } + let eps_sq = epsilon * epsilon; + let mut sum = 0.0; + for i in 0..D { + let d = self.coords[i] - other.coords[i]; + sum += d * d; + if !(sum < eps_sq) { + return false; + } + } + true } }