Skip to content

perf(core): cut per-request allocations on the fetch hot path - #75

Merged
LeandrodaSilva merged 1 commit into
mainfrom
perf/hot-path
Jun 7, 2026
Merged

LeandrodaSilva merged 1 commit into
mainfrom
perf/hot-path

Conversation

@LeandrodaSilva

Copy link
Copy Markdown
Owner

Summary

Benchmark-driven zero-allocation work on the TenCore.fetch hot path. Profiled the in-process request path with a new micro-benchmark, identified the three biggest per-request allocations, and eliminated them — no public API or behavior change.

Changes

  • Memoize _availableLocales — was rebuilding a Set + Object.values() + sorted array on every request. Now cached in _availableLocalesCache, invalidated wherever _i18n is reassigned (setter, updateManifest, embedded init).
  • Skip middleware bind+closure in _handleRequest when no middleware is registered (the common case).
  • Skip the response-hook pass when no response hooks are registered (avoids an async hop).

Benchmarks (Deno 2.8.2, in-process fetch)

benchmark baseline optimized Δ (p75)
core_fetch_api ~13.3 µs ~8–10 µs 13.2 µs → ~8 µs
core_fetch_i18n ~15.4 µs ~11–13 µs 15.6 µs → ~12 µs

Roughly 25–37% faster on the plain API path and 15–30% on the i18n path (p75 is the steadier signal under run-to-run noise).

Verification

  • New test in tenCore.pipeline.test.ts proves the locale cache invalidates when i18n is reassigned (404 before config → 200 with Content-Language after).
  • Added bench thresholds for core_fetch_api / core_fetch_i18n.
  • deno fmt / deno lint / deno check clean; full suite green; coverage 90.6% (above the 90% floor).

https://claude.ai/code/session_015AsggMbqEhejVYtbq1LubN


Generated by Claude Code

Memoize the available-locale list (was rebuilding a Set + sorted array
on every request) and invalidate it whenever the i18n map is reassigned.
Skip the middleware bind+closure allocation when no middleware is
registered, and skip the response-hook pass when none are registered.

Benchmark-driven (src/bench/core.bench.ts), Deno 2.8.2, in-process fetch:
- core_fetch_api:  ~13.3 µs -> ~8-10 µs  (p75 13.2 µs -> ~8 µs)
- core_fetch_i18n: ~15.4 µs -> ~11-13 µs (p75 15.6 µs -> ~12 µs)

https://claude.ai/code/session_015AsggMbqEhejVYtbq1LubN
@LeandrodaSilva
LeandrodaSilva merged commit 261bf23 into main Jun 7, 2026
6 checks passed
@LeandrodaSilva LeandrodaSilva mentioned this pull request Jun 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants