From 746e9ae4c9277cae161d90ea447198c79fad03e0 Mon Sep 17 00:00:00 2001 From: Ralksta <16941050+ralksta@users.noreply.github.com> Date: Sun, 31 May 2026 10:03:03 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Memoize=20auth=20checks=20i?= =?UTF-8?q?n=20Map=20API=20(#236)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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> --- .jules/bolt.md | 3 +++ app/api/map/route.ts | 6 +++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/.jules/bolt.md b/.jules/bolt.md index edc00d2..9be86bc 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -18,3 +18,6 @@ ## 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. **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. diff --git a/app/api/map/route.ts b/app/api/map/route.ts index 20572e1..6ba75ba 100644 --- a/app/api/map/route.ts +++ b/app/api/map/route.ts @@ -44,12 +44,16 @@ export async function GET(request: NextRequest) { const locations = await getMapData(); // Filter locations and albums based on auth + const authCache = new Map(); const publicLocations = locations .map((loc) => { // Only keep albums the user is allowed to see const allowedAlbums = loc.albums.filter((a) => { 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;