diff --git a/.jules/bolt.md b/.jules/bolt.md index 2ac6922..c476208 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-03 - Avoid libm::sqrt in Hot Spatial Scans +**Learning:** Using `sqrt` inside high-frequency O(N) spatial scans (like finding neighbors) introduces significant computational overhead. Also, calling `.distance()` evaluates all dimensions even if the early dimensions exceed the threshold. +**Action:** Use squared distance comparisons (`d^2 < r^2`) with early exits inside the coordinate loop. Ensure negative or `NaN` thresholds are explicitly handled (`!(epsilon > 0.0)`), and use `!(sum < eps_sq)` for the early exit to safely handle `NaN` values without altering behavioral logic. diff --git a/crates/aether-core/src/manifold.rs b/crates/aether-core/src/manifold.rs index 1bcc201..36d0bb7 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,21 @@ 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; + // Early exit if the squared distance already exceeds or equals eps_sq. + // Also catches NaNs effectively by negating the valid < case. + if !(sum < eps_sq) { + return false; + } + } + true } }