perf: optimize lru-cache.ts#7455
Conversation
# LRU Cache Optimization Results
## BEFORE
State: test/benchmark support changes applied, implementation stashed to baseline.
### Tests
- `CI=1 NX_DAEMON=false pnpm nx run @tanstack/router-core:test:unit --outputStyle=stream --skipRemoteCache -- tests/lru.test.ts`: failed, 1 failed / 13 total.
- Failure: `updates existing entries without evicting when full`, expected `cache.get('a')` to be `1`, received `undefined`.
- `CI=1 NX_DAEMON=false pnpm nx run @tanstack/router-core:test:unit --outputStyle=stream --skipRemoteCache`: failed, 1 failed / 1104 total.
- `CI=1 NX_DAEMON=false pnpm nx run @tanstack/router-core:test:types --outputStyle=stream --skipRemoteCache`: passed.
- `CI=1 NX_DAEMON=false pnpm nx run @tanstack/router-core:test:eslint --outputStyle=stream --skipRemoteCache`: passed with 20 existing warnings.
### Performance
Command: `CI=1 NX_DAEMON=false pnpm nx run @tanstack/router-core:test:perf --outputStyle=stream --skipRemoteCache -- tests/lru-cache.bench.ts`
| Case | hz | mean | rme | samples |
| --- | ---: | ---: | ---: | ---: |
| newest hit | 18,518.89 | 0.0540 | +/-0.58% | 9,260 |
| rotating hit | 13,741.92 | 0.0728 | +/-3.08% | 6,871 |
| update newest while full | 17,309.32 | 0.0578 | +/-0.67% | 8,655 |
| update oldest while full | 17,448.76 | 0.0573 | +/-1.65% | 8,725 |
| update rotating entries while full | 9,113.98 | 0.1097 | +/-0.50% | 4,557 |
| miss-heavy get | 78,647.37 | 0.0127 | +/-0.73% | 39,324 |
| insert churn | 14,705.09 | 0.0680 | +/-3.13% | 7,353 |
| mixed workload | 41,227.14 | 0.0243 | +/-0.25% | 20,614 |
### Bundle Size
Command: `CI=1 NX_DAEMON=false pnpm nx run @benchmarks/bundle-size:build --outputStyle=stream --skipRemoteCache --skipNxCache -- --scenario react-router.minimal >/tmp/lru-before-targeted-bundle.log 2>&1 && pnpm benchmark:bundle-size:query --id react-router.minimal`
- `react-router.minimal gzip=89421 initial=89281 raw=280786 brotli=77765`
## AFTER
State: test/benchmark support changes plus optimized LRU implementation applied.
### Tests
- `CI=1 NX_DAEMON=false pnpm nx run @tanstack/router-core:test:unit --outputStyle=stream --skipRemoteCache -- tests/lru.test.ts`: passed, 13 passed / 13 total.
- `CI=1 NX_DAEMON=false pnpm nx run @tanstack/router-core:test:unit --outputStyle=stream --skipRemoteCache`: passed, 1101 passed / 3 expected fail / 1104 total.
- `CI=1 NX_DAEMON=false pnpm nx run @tanstack/router-core:test:types --outputStyle=stream --skipRemoteCache`: passed.
- `CI=1 NX_DAEMON=false pnpm nx run @tanstack/router-core:test:eslint --outputStyle=stream --skipRemoteCache`: passed with 20 existing warnings.
### Performance
Command: `CI=1 NX_DAEMON=false pnpm nx run @tanstack/router-core:test:perf --outputStyle=stream --skipRemoteCache -- tests/lru-cache.bench.ts`
| Case | hz | mean | rme | samples |
| --- | ---: | ---: | ---: | ---: |
| newest hit | 19,796.85 | 0.0505 | +/-0.43% | 9,899 |
| rotating hit | 14,971.94 | 0.0668 | +/-0.44% | 7,486 |
| update newest while full | 19,253.35 | 0.0519 | +/-1.03% | 9,627 |
| update oldest while full | 18,284.35 | 0.0547 | +/-3.18% | 9,143 |
| update rotating entries while full | 14,969.14 | 0.0668 | +/-0.92% | 7,485 |
| miss-heavy get | 78,455.30 | 0.0127 | +/-0.26% | 39,228 |
| insert churn | 14,950.34 | 0.0669 | +/-4.60% | 7,476 |
| mixed workload | 38,459.24 | 0.0260 | +/-2.41% | 19,230 |
### Performance Comparison
| Case | BEFORE hz | AFTER hz | hz delta | BEFORE mean | AFTER mean | mean delta |
| --- | ---: | ---: | ---: | ---: | ---: | ---: |
| newest hit | 18,518.89 | 19,796.85 | +6.90% | 0.0540 | 0.0505 | -6.48% |
| rotating hit | 13,741.92 | 14,971.94 | +8.95% | 0.0728 | 0.0668 | -8.24% |
| update newest while full | 17,309.32 | 19,253.35 | +11.23% | 0.0578 | 0.0519 | -10.21% |
| update oldest while full | 17,448.76 | 18,284.35 | +4.79% | 0.0573 | 0.0547 | -4.54% |
| update rotating entries while full | 9,113.98 | 14,969.14 | +64.24% | 0.1097 | 0.0668 | -39.11% |
| miss-heavy get | 78,647.37 | 78,455.30 | -0.24% | 0.0127 | 0.0127 | 0.00% |
| insert churn | 14,705.09 | 14,950.34 | +1.67% | 0.0680 | 0.0669 | -1.62% |
| mixed workload | 41,227.14 | 38,459.24 | -6.71% | 0.0243 | 0.0260 | +7.00% |
Notes: `update rotating entries while full` improves materially because the old implementation evicted before checking existing entries. `mixed workload` regressed in this run and has moderate noise (`AFTER rme +/-2.41%`), so treat as directional.
### Bundle Size
Command: `CI=1 NX_DAEMON=false pnpm nx run @benchmarks/bundle-size:build --outputStyle=stream --skipRemoteCache --skipNxCache -- --scenario react-router.minimal >/tmp/lru-after-targeted-bundle.log 2>&1 && pnpm benchmark:bundle-size:query --id react-router.minimal && pnpm benchmark:bundle-size:diff --baseline /var/folders/6f/2t42ntqs4yv4h6qwzbh5pmcm0000gn/T/opencode/lru-base-current.json --id react-router.minimal`
- `react-router.minimal gzip=89407 initial=89268 raw=280685 brotli=77653`
- Diff vs BEFORE: `gzip -14`, `initial -13`, `raw -101`, `brotli -112`.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthroughThe PR refactors the LRU cache implementation to decouple update semantics from eviction: updating existing keys now calls ChangesLRU Cache Refactoring
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
|
View your CI Pipeline Execution ↗ for commit 1cd3483
☁️ Nx Cloud last updated this comment at |
🚀 Changeset Version Preview1 package(s) bumped directly, 8 bumped as dependents. 🟩 Patch bumps
|
Bundle Size Benchmarks
Current gzip tracks all emitted client JS chunks. Initial gzip tracks only the entry/import graph. Trend sparkline is historical current gzip ending with this PR measurement; lower is better. |
Merging this PR will not alter performance
Comparing Footnotes
|
LRU Cache Optimization Results
BEFORE
State: test/benchmark support changes applied, implementation stashed to baseline.
Tests
CI=1 NX_DAEMON=false pnpm nx run @tanstack/router-core:test:unit --outputStyle=stream --skipRemoteCache -- tests/lru.test.ts: failed, 1 failed / 13 total.updates existing entries without evicting when full, expectedcache.get('a')to be1, receivedundefined.CI=1 NX_DAEMON=false pnpm nx run @tanstack/router-core:test:unit --outputStyle=stream --skipRemoteCache: failed, 1 failed / 1104 total.CI=1 NX_DAEMON=false pnpm nx run @tanstack/router-core:test:types --outputStyle=stream --skipRemoteCache: passed.CI=1 NX_DAEMON=false pnpm nx run @tanstack/router-core:test:eslint --outputStyle=stream --skipRemoteCache: passed with 20 existing warnings.Performance
Command:
CI=1 NX_DAEMON=false pnpm nx run @tanstack/router-core:test:perf --outputStyle=stream --skipRemoteCache -- tests/lru-cache.bench.tsBundle Size
Command:
CI=1 NX_DAEMON=false pnpm nx run @benchmarks/bundle-size:build --outputStyle=stream --skipRemoteCache --skipNxCache -- --scenario react-router.minimal >/tmp/lru-before-targeted-bundle.log 2>&1 && pnpm benchmark:bundle-size:query --id react-router.minimalreact-router.minimal gzip=89421 initial=89281 raw=280786 brotli=77765AFTER
State: test/benchmark support changes plus optimized LRU implementation applied.
Tests
CI=1 NX_DAEMON=false pnpm nx run @tanstack/router-core:test:unit --outputStyle=stream --skipRemoteCache -- tests/lru.test.ts: passed, 13 passed / 13 total.CI=1 NX_DAEMON=false pnpm nx run @tanstack/router-core:test:unit --outputStyle=stream --skipRemoteCache: passed, 1101 passed / 3 expected fail / 1104 total.CI=1 NX_DAEMON=false pnpm nx run @tanstack/router-core:test:types --outputStyle=stream --skipRemoteCache: passed.CI=1 NX_DAEMON=false pnpm nx run @tanstack/router-core:test:eslint --outputStyle=stream --skipRemoteCache: passed with 20 existing warnings.Performance
Command:
CI=1 NX_DAEMON=false pnpm nx run @tanstack/router-core:test:perf --outputStyle=stream --skipRemoteCache -- tests/lru-cache.bench.tsPerformance Comparison
| Case | BEFORE hz | AFTER hz | hz delta | BEFORE mean | AFTER mean | mean delta | | --- | ---: | ---: | ---: | ---: | ---: | ---: |
| newest hit | 18,518.89 | 19,796.85 | +6.90% | 0.0540 | 0.0505 | -6.48% | | rotating hit | 13,741.92 | 14,971.94 | +8.95% | 0.0728 | 0.0668 | -8.24% | | update newest while full | 17,309.32 | 19,253.35 | +11.23% | 0.0578 | 0.0519 | -10.21% | | update oldest while full | 17,448.76 | 18,284.35 | +4.79% | 0.0573 | 0.0547 | -4.54% | | update rotating entries while full | 9,113.98 | 14,969.14 | +64.24% | 0.1097 | 0.0668 | -39.11% | | miss-heavy get | 78,647.37 | 78,455.30 | -0.24% | 0.0127 | 0.0127 | 0.00% | | insert churn | 14,705.09 | 14,950.34 | +1.67% | 0.0680 | 0.0669 | -1.62% | | mixed workload | 41,227.14 | 38,459.24 | -6.71% | 0.0243 | 0.0260 | +7.00% |
Notes:
update rotating entries while fullimproves materially because the old implementation evicted before checking existing entries.mixed workloadregressed in this run and has moderate noise (AFTER rme +/-2.41%), so treat as directional.Bundle Size
Command:
CI=1 NX_DAEMON=false pnpm nx run @benchmarks/bundle-size:build --outputStyle=stream --skipRemoteCache --skipNxCache -- --scenario react-router.minimal >/tmp/lru-after-targeted-bundle.log 2>&1 && pnpm benchmark:bundle-size:query --id react-router.minimal && pnpm benchmark:bundle-size:diff --baseline /var/folders/6f/2t42ntqs4yv4h6qwzbh5pmcm0000gn/T/opencode/lru-base-current.json --id react-router.minimalreact-router.minimal gzip=89407 initial=89268 raw=280685 brotli=77653gzip -14,initial -13,raw -101,brotli -112.Summary by CodeRabbit
Refactor
Tests