Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@
## 2026-06-20 - Fast Spatial Neighborhood Checks
**Learning:** In hot paths doing spatial scanning (like SparseAttentionGraph), calling `libm::sqrt` for distance thresholds is a massive bottleneck. Moreover, using a squared distance optimization `d^2 < r^2` must be robust to NaN values.
**Action:** Always optimize spatial neighborhood checks using squared distances (`eps_sq`) inside an inline loop. Explicitly check for invalid thresholds before the loop (e.g. `!(epsilon > 0.0)`), and use the `!(sum < eps_sq)` pattern for early exits to handle NaNs safely while matching standard Euclidean semantics without the `sqrt` overhead.
## 2026-06-21 - Optimization in DBSCAN and Auto-K Selection
**Learning:** In hot loops such as `auto_k_selection` and `DBSCAN::region_query`, avoiding `libm::sqrt` by using squared distances (`d^2 < r^2`) significantly reduces mathematical overhead and speeds up the algorithms. Also, explicitly rejecting negative or NaN thresholds before the loop (e.g., `!(epsilon > 0.0)`) and using early exits within the loop formatted as `!(sum < eps_sq)` safely handles `NaN` values without altering behavioral logic.
**Action:** Whenever iterating over distance computations that compare against a threshold, prefer comparing the sum of squared differences directly against the squared threshold instead of taking the square root. Handle edge cases like invalid distances properly.
34 changes: 31 additions & 3 deletions crates/aether-core/src/ml/clustering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,11 @@ impl<const D: usize> KMeans<D> {

/// Automatically determine optimal K using topological analysis
pub fn auto_k_selection<const D: usize>(data: &[[f64; D]], n: usize, epsilon: f64) -> usize {
if !(epsilon > 0.0) {
return n.clamp(1, MAX_POINTS);
}
let eps_sq = epsilon * epsilon;

// Build epsilon-neighborhood graph and count connected components (Ξ²β‚€)
let mut components = 0;
let mut visited = [false; MAX_POINTS];
Expand Down Expand Up @@ -300,8 +305,17 @@ pub fn auto_k_selection<const D: usize>(data: &[[f64; D]], n: usize, epsilon: f6
// Add neighbors
for i in 0..n {
if !visited[i] && i != current {
let dist = distance(&data[current], &data[i]);
if dist < epsilon && top < 64 {
let mut sum = 0.0;
let mut is_neighbor = true;
for d in 0..D {
let diff = data[current][d] - data[i][d];
sum += diff * diff;
if !(sum < eps_sq) {
is_neighbor = false;
break;
}
}
if is_neighbor && top < 64 {
stack[top] = i;
top += 1;
}
Expand Down Expand Up @@ -447,9 +461,23 @@ impl<const D: usize> DBSCAN<D> {
i: usize,
) -> heapless::Vec<usize, MAX_POINTS> {
let mut neighbors = heapless::Vec::new();
if !(self.epsilon >= 0.0) {
return neighbors;
}
let eps_sq = self.epsilon * self.epsilon;

for j in 0..n {
if distance(&data[i], &data[j]) <= self.epsilon {
let mut sum = 0.0;
let mut is_neighbor = true;
for d in 0..D {
let diff = data[i][d] - data[j][d];
sum += diff * diff;
if !(sum <= eps_sq) {
is_neighbor = false;
break;
}
}
if is_neighbor {
let _ = neighbors.push(j);
}
}
Expand Down