perf(api): replace O(n^2) map viewport clustering with spatial grid #1157
Loading…
Reference in a new issue
No description provided.
Delete branch "perf/map-viewport-api"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary\n- GET /api/map/viewport clustered up to 5000 markers with an O(n²) BFS (~25M distance checks worst case); representativeMarker re-projected members twice.\n- Replaced with a Web-Mercator spatial grid (cell >= 96px, wrapped 3x3 neighborhood, sorted per-expansion candidates) so discovery order — member lists, centroids, representatives, cluster ids, output order — is unchanged. Contract untouched; no migration needed (EXPLAIN showed all join keys already indexed; bbox predicate is post-join COALESCE so no plain index would help).\n\n## Measured (5000 markers, testcontainer postgres)\n| | Before | After |\n|---|---|---|\n| Clustering | 133.1ms | 7.0ms (~19x) |\n| In-memory 5k | 208.4ms | 36.0ms |\n\nClustering now ~9% of a capped request (SQL dominant).\n\n## Testing\n- Equivalence matrix (zooms 0-22, fixtures 0-750, antimeridian wrap, hidden-members, representative ties) vs frozen legacy oracle\n-
rtk ./gradlew :apps:api:testfully green (incl. MapQueryServiceTest 9/9)4b44733cd7ff414ce9be