⚡ Bolt: Memoize auth checks in Map API (#236)

Introduced a request-scoped `Map` (`authCache`) in `app/api/map/route.ts` to memoize `isAuthenticated` calls.
The `getMapData` loop iterates over all albums across all locations. Albums within the same subpage slug trigger repeated `isAuthenticated` checks, which execute expensive HMAC cryptographic operations, causing performance degradation for large datasets.
Eliminates redundant cryptographic operations during map data fetching, reducing CPU utilization and response times for the Map API.

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
This commit is contained in:
Ralksta
2026-05-31 10:03:03 +02:00
committed by GitHub
parent c2fa8e7596
commit 746e9ae4c9
2 changed files with 8 additions and 1 deletions

View File

@@ -18,3 +18,6 @@
## 2024-05-30 - [O(1) Memory Bucketing for Coordinate Aggregation] ## 2024-05-30 - [O(1) Memory Bucketing for Coordinate Aggregation]
**Learning:** When bucketing or grouping large datasets (e.g., thousands of map coordinates into geographical clusters), array allocations (`[].push()`) followed by `reduce` operations create an O(N) memory overhead per bucket and expensive post-processing loops. **Learning:** When bucketing or grouping large datasets (e.g., thousands of map coordinates into geographical clusters), array allocations (`[].push()`) followed by `reduce` operations create an O(N) memory overhead per bucket and expensive post-processing loops.
**Action:** Replace memory-heavy array collections with running statistical accumulators (e.g., `latSum`, `lngSum`, `count`) directly on the bucket. This reduces bucket memory from O(N) to O(1). **Action:** Replace memory-heavy array collections with running statistical accumulators (e.g., `latSum`, `lngSum`, `count`) directly on the bucket. This reduces bucket memory from O(N) to O(1).
## 2024-05-29 - Memoize repeated auth checks in Map API
**Learning:** When filtering map locations, the API performs redundant `isAuthenticated` checks for albums within the same subpage slug. Since each check involves cryptographic operations (HMAC calculations), doing this repeatedly in a nested filter loop severely impacts performance for galleries with many geotagged photos.
**Action:** Use a request-scoped `Map` to memoize the results of `isAuthenticated` calls within the route handler, preventing redundant cryptographic operations for the same subpage context.

View File

@@ -44,12 +44,16 @@ export async function GET(request: NextRequest) {
const locations = await getMapData(); const locations = await getMapData();
// Filter locations and albums based on auth // Filter locations and albums based on auth
const authCache = new Map<string, boolean>();
const publicLocations = locations const publicLocations = locations
.map((loc) => { .map((loc) => {
// Only keep albums the user is allowed to see // Only keep albums the user is allowed to see
const allowedAlbums = loc.albums.filter((a) => { const allowedAlbums = loc.albums.filter((a) => {
if (!a.subpageSlug) return true; // Standalone albums are public if (!a.subpageSlug) return true; // Standalone albums are public
return isAuthenticated(a.subpageSlug, getCookie); if (authCache.has(a.subpageSlug)) return authCache.get(a.subpageSlug)!;
const result = isAuthenticated(a.subpageSlug, getCookie);
authCache.set(a.subpageSlug, result);
return result;
}); });
if (allowedAlbums.length === 0) return null; if (allowedAlbums.length === 0) return null;