From f03f7c57fc2ebaa20e358560612b910cb6a82782 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Mon, 27 Apr 2026 02:49:30 +0000 Subject: [PATCH] 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: shenald-dev <245350826+shenald-dev@users.noreply.github.com> --- .jules/bolt.md | 8 ++++++++ src/index.js | 15 +++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/.jules/bolt.md b/.jules/bolt.md index a7fe852..4a07c98 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -180,3 +180,11 @@ Action: Apply `express.json()` strictly as route-specific middleware to the exac 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. 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. + +## 2026-04-27 — Optimize Cache Lookups + +Learning: +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. + +Action: +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. diff --git a/src/index.js b/src/index.js index beca4f1..b6bade4 100644 --- a/src/index.js +++ b/src/index.js @@ -154,17 +154,28 @@ app.use((err, req, res, next) => { }); const computationCache = new Map(); +let lastIterations = null; +let lastResult = null; /** * Performs a heavy mathematical computation. * Optimized with memoization for repeated calls with identical parameters. */ function heavyComputation(iterations) { + // L1 Cache: Fast path for repeated consecutive calls + if (iterations === lastIterations) { + return lastResult; + } + const cachedVal = computationCache.get(iterations); if (cachedVal !== undefined) { // Refresh LRU by deleting and re-inserting computationCache.delete(iterations); computationCache.set(iterations, cachedVal); + + // Update L1 cache + lastIterations = iterations; + lastResult = cachedVal; return cachedVal; } @@ -180,6 +191,10 @@ function heavyComputation(iterations) { } computationCache.set(iterations, sum); + + // Update L1 cache + lastIterations = iterations; + lastResult = sum; return sum; }