Skip to content

Commit 4dbc052

Browse files
perf: implement L1 caching in heavyComputation
Adds an L1 cache to `heavyComputation` to bypass Map lookup and mutation overhead for consecutive identical calls, significantly improving throughput for repeated identical tasks. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: shenald-dev <245350826+shenald-dev@users.noreply.github.com>
1 parent 82c2b68 commit 4dbc052

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

.jules/bolt.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,11 @@ Action: Apply `express.json()` strictly as route-specific middleware to the exac
180180

181181
Learning: In Express API gateways, declaring high-frequency, simple endpoints (like `/health`) below global middleware such as `helmet` and `cors` introduces significant and unnecessary CPU parsing overhead for every ping, even if the ping does not require CORS or security headers.
182182
Action: Moved the `/health` endpoint definition above `helmet()` and `cors()` in `src/index.js`, while manually explicitly setting the `Content-Type` header. This drastically reduces CPU overhead and latency for load balancer pings while maintaining correct response headers.
183+
184+
## 2026-04-27 — Optimize Cache Lookups
185+
186+
Learning:
187+
In highly trafficked functions like `heavyComputation` that rely on an LRU map cache, repeated calls with identical parameters incur significant overhead due to Map `delete` and `set` operations used to refresh the LRU order.
188+
189+
Action:
190+
Added an L1 cache using module-scoped variables (`lastIterations` and `lastResult`) to `heavyComputation` in `src/index.js`. This avoids redundant Map lookups and mutations for consecutive identical calls, transforming a hot path from an (1)$ Map operation to a much faster strict equality check.

src/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,17 +154,28 @@ app.use((err, req, res, next) => {
154154
});
155155

156156
const computationCache = new Map();
157+
let lastIterations = null;
158+
let lastResult = null;
157159

158160
/**
159161
* Performs a heavy mathematical computation.
160162
* Optimized with memoization for repeated calls with identical parameters.
161163
*/
162164
function heavyComputation(iterations) {
165+
// L1 Cache: Fast path for repeated consecutive calls
166+
if (iterations === lastIterations) {
167+
return lastResult;
168+
}
169+
163170
const cachedVal = computationCache.get(iterations);
164171
if (cachedVal !== undefined) {
165172
// Refresh LRU by deleting and re-inserting
166173
computationCache.delete(iterations);
167174
computationCache.set(iterations, cachedVal);
175+
176+
// Update L1 cache
177+
lastIterations = iterations;
178+
lastResult = cachedVal;
168179
return cachedVal;
169180
}
170181

@@ -180,6 +191,10 @@ function heavyComputation(iterations) {
180191
}
181192

182193
computationCache.set(iterations, sum);
194+
195+
// Update L1 cache
196+
lastIterations = iterations;
197+
lastResult = sum;
183198
return sum;
184199
}
185200

0 commit comments

Comments
 (0)