From 2dcc7988d189dfdb3f5193b9925355322b3c61e8 Mon Sep 17 00:00:00 2001 From: RohanExploit <178623867+RohanExploit@users.noreply.github.com> Date: Mon, 13 Jul 2026 14:22:55 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Optimize=20spatial=20dedupl?= =?UTF-8?q?ication=20with=20bounding=20box=20pre-filter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .jules/bolt.md | 7 +++++++ backend/spatial_utils.py | 12 ++++++++++++ 2 files changed, 19 insertions(+) diff --git a/.jules/bolt.md b/.jules/bolt.md index b0fee03f..fca0bc8f 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -13,3 +13,10 @@ ## 2025-02-27 - UploadFile Validation Blocking **Learning:** `UploadFile` validation using `python-magic` and file seeking is synchronous and CPU/IO bound. In FastAPI async endpoints, this blocks the event loop. **Action:** Wrap file validation logic in `run_in_threadpool` and await it. +## 2024-07-13 - Scratchpad Files and Test Runners +**Learning:** Temporary scratchpad files (like `test_spatial.py` or `test_perf.py`) created in the repository root will be automatically discovered by test runners like `pytest` because they start with `test_`. If these files contain top-level execution code (like creating arrays of 100k items), they will block test suites and pollute standard output. +**Action:** Always clean up temporary files immediately after use (e.g. `rm test_perf.py`) before running project test suites or requesting code review. + +## 2024-07-13 - Haversine Distance Optimization +**Learning:** Calculating `haversine_distance` for every point in a large dataset is O(N) and extremely slow due to repeated trigonometric functions (`math.sin`, `math.cos`, etc.). +**Action:** For spatial radius queries, always apply a cheap bounding box pre-filter first (e.g., `get_bounding_box` with a 5% epsilon for safety). This reduces the complexity to O(N) fast arithmetic checks + O(K) slow trigonometric functions, where K << N. diff --git a/backend/spatial_utils.py b/backend/spatial_utils.py index 8af329a3..4270c228 100644 --- a/backend/spatial_utils.py +++ b/backend/spatial_utils.py @@ -76,10 +76,22 @@ def find_nearby_issues( """ nearby_issues = [] + # ⚡ Bolt Optimization: Use bounding box pre-filter with 5% epsilon to + # skip expensive haversine distance calculations for issues far away. + # This reduces time complexity from O(N) expensive trig functions to + # O(N) simple bounds checks + O(K) trig functions (where K << N). + min_lat, max_lat, min_lon, max_lon = get_bounding_box( + target_lat, target_lon, radius_meters * 1.05 + ) + for issue in issues: if issue.latitude is None or issue.longitude is None: continue + # Fast pre-filter check + 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