From 0221e7eb5fd7c8ff1b87b98ea32fee12e2aa7902 Mon Sep 17 00:00:00 2001 From: RohanExploit <178623867+RohanExploit@users.noreply.github.com> Date: Wed, 15 Jul 2026 13:48:18 +0000 Subject: [PATCH 1/2] perf: optimize nearby issues search with bounding box pre-filter Added a fast bounding box pre-filter with a 5% epsilon to `find_nearby_issues` in `backend/spatial_utils.py` to quickly discard coordinates outside the search radius before running the computationally expensive exact haversine distance calculations. Also appended this learning to `.jules/bolt.md`. --- .jules/bolt.md | 4 ++++ backend/spatial_utils.py | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/.jules/bolt.md b/.jules/bolt.md index ba84fca0..bf784525 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -21,3 +21,7 @@ ## 2025-05-30 - O(1) Blockchain Hash Lookup **Learning:** Calculating a blockchain-style hash chain requires finding the previous record's hash. A naive DB scan is O(log N) with indexes, but high-concurrency follow operations can be optimized. **Action:** Implement an in-memory thread-safe cache for the last hash of each grievance to achieve O(1) lookup during follower creation, falling back to DB on cache miss. + +## 2025-07-15 - Fast Bounding Box Pre-filter +**Learning:** Calculating great circle distance (Haversine) for every issue against a target location is computationally expensive (O(N) with heavy math ops like sin, cos, atan2). In high-traffic aggregations, this can become a bottleneck. +**Action:** Use a fast bounding box pre-filter (`get_bounding_box` with a 5% epsilon) to quickly discard issues that are definitely outside the search radius before running the expensive exact haversine distance calculation. diff --git a/backend/spatial_utils.py b/backend/spatial_utils.py index 8af329a3..281135f6 100644 --- a/backend/spatial_utils.py +++ b/backend/spatial_utils.py @@ -76,10 +76,20 @@ def find_nearby_issues( """ nearby_issues = [] + # Fast bounding box pre-filter with a 5% epsilon to optimize expensive haversine calculations + epsilon_radius = radius_meters * 1.05 + min_lat, max_lat, min_lon, max_lon = get_bounding_box( + target_lat, target_lon, epsilon_radius + ) + for issue in issues: if issue.latitude is None or issue.longitude is None: continue + # Skip issues outside the bounding box + if not (min_lat <= issue.latitude <= max_lat and min_lon <= issue.longitude <= max_lon): + continue + distance = haversine_distance( target_lat, target_lon, issue.latitude, issue.longitude From dbd4616016bb63c981c317f87b3cea96b8141cf4 Mon Sep 17 00:00:00 2001 From: Rohan Gaikwad Date: Wed, 15 Jul 2026 21:47:07 +0530 Subject: [PATCH 2/2] Update backend/spatial_utils.py Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> --- backend/spatial_utils.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/backend/spatial_utils.py b/backend/spatial_utils.py index 281135f6..213bfeaa 100644 --- a/backend/spatial_utils.py +++ b/backend/spatial_utils.py @@ -87,7 +87,11 @@ def find_nearby_issues( continue # Skip issues outside the bounding box - if not (min_lat <= issue.latitude <= max_lat and min_lon <= issue.longitude <= max_lon): + lon_delta = (issue.longitude - target_lon + 180) % 360 - 180 + lon_half_width = max(target_lon - min_lon, max_lon - target_lon) + if not (min_lat <= issue.latitude <= max_lat and ( + abs(target_lat) >= 89.9 or abs(lon_delta) <= lon_half_width + )): continue distance = haversine_distance(