Skip to content
Merged
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
4 changes: 4 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Nearby issues across the ±180° meridian are dropped even though they are within the radius, so this action is not safe as written. The bounding-box implementation should normalize longitudes or split a dateline-crossing box before applying the pre-filter.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At .jules/bolt.md, line 27:

<comment>Nearby issues across the ±180° meridian are dropped even though they are within the radius, so this action is not safe as written. The bounding-box implementation should normalize longitudes or split a dateline-crossing box before applying the pre-filter.</comment>

<file context>
@@ -21,3 +21,7 @@
+
+## 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.
</file context>

14 changes: 14 additions & 0 deletions backend/spatial_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,24 @@ 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
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

Comment thread
coderabbitai[bot] marked this conversation as resolved.
distance = haversine_distance(
target_lat, target_lon,
issue.latitude, issue.longitude
Expand Down
Loading