From d51ff3bee41f9e28207a718bd424e30de94ad52b Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Wed, 28 Oct 2026 20:31:18 -0500 Subject: [PATCH] perf(app): skip awaiting the boot in app.fetch once it has finished trackHealth wraps the memoized boot in an async function, so every app.fetch request paid a new promise and microtask hops awaiting a boot that had already settled. A built route table means the boot succeeded, so the fetch handler now goes straight to it; only requests before the table exists await the boot. A failed boot still rejects every request, because the table is never built. Measured in-process on Deno (app.fetch in a loop, param route): about +3-6%. Signed-off-by: Diego Resendez --- CHANGELOG.md | 5 +++++ src/app/index.ts | 17 ++++++++++------- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 13fc49d..7ee823b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -317,6 +317,11 @@ npm treats versions as semver and semver forbids leading zeros. read, added for the incremental `413`, had cut a Bun JSON `POST` by more than half. Behaviour is unchanged: the same `413`s, and `set-cookie` arrays still reach the client one line per cookie. +- **`app.fetch` stops awaiting the boot once it has finished.** Since the health probes, every + request went through an `async` wrapper around the memoized boot, which cost a promise and + microtask hops even after the app was up. The first request still waits for the boot, and a boot + that failed still fails every request. + ### Deprecated - **The `Headers` type** (D19). Import `RequestHeaders` instead: same type, a name that does not diff --git a/src/app/index.ts b/src/app/index.ts index 2955018..92ef455 100644 --- a/src/app/index.ts +++ b/src/app/index.ts @@ -1810,6 +1810,15 @@ function buildAppFetch( ): (request: Request) => Promise { let handler: ((request: Request) => Promise) | undefined; + // Awaits the boot, then builds the table. Only until the first request has done so: a built table + // means the boot succeeded, and awaiting a settled boot again costs every request a promise and + // microtask hops (about 6% of a small request on Deno). + const firstRequest = async (request: Request): Promise => { + await ensureBooted(); // idempotent: no-op once any of fetch/upgrade/listen has booted + handler ??= buildFetch(getRoutes, fetchOpts); + return handler(request); + }; + return async (request: Request): Promise => { // Before the boot, or `/__ready__` could never say `booting`: it would wait for the boot to end. // The boot is still started, not awaited: on the edge, or under a bare `Deno.serve(app.fetch)`, @@ -1822,13 +1831,7 @@ function buildAppFetch( return probe; } - await ensureBooted(); // idempotent: no-op once any of fetch/upgrade/listen has booted - - if (!handler) { - handler = buildFetch(getRoutes, fetchOpts); - } - - return handler(request); + return handler ? handler(request) : firstRequest(request); }; } -- 2.54.1