perf(http): trim per-request work on the Node path
CI / dco (pull_request) Successful in 6s
CI / runtimes (bun) (pull_request) Successful in 19s
CI / build (pull_request) Successful in 28s
CI / runtimes (edge) (pull_request) Successful in 23s
CI / runtimes (deno) (pull_request) Successful in 32s

Profiling the validate (POST) bench route against 4d1639f showed the
loss since then spread over several small costs on the Node request
path rather than one regression. This removes the ones that do not
change behaviour:

- readBody reads with data/end/error/close listeners instead of
  `for await`, whose async iterator costs a promise and a microtask per
  chunk. It keeps the iterator's semantics: the running total enforces
  the limit, an over-limit body destroys the request as leaving the loop
  did, an error or a close before `end` rejects (the latter with
  ERR_STREAM_PREMATURE_CLOSE), and the declared-length 413 is still a
  rejection rather than a synchronous throw. A single chunk is returned
  without Buffer.concat.
- writeResult is no longer async. A buffered response without
  compression is written synchronously and returns nothing to await;
  only a stream or a compressed body returns a promise, which serve
  awaits, so a failure still reaches failSafe. The compression request
  object is not built when compression is off.
- declaresOverLimit compares the number first and runs its /^\d+$/
  shape test only when the length is over the limit.

The `serve().catch(failSafe)` wrapper stays: it is the #108 guarantee,
and handle() is asynchronous, so there is no synchronous path to skip.

Local A/B against 74faa55 (5 s x 4 rounds, macOS, indicative only):
validate (POST) +4.6%. test/read-body.test.ts pins readBody's
semantics: joining, empty body, 413 with destroy, declared-length 413,
stream error and premature close.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-10-26 08:02:03 -05:00
1 parent 74faa55f41
commit 8b67b1a46c
5 files changed
+133 -29

No files matched your search

+26 -18
View File
@@ -15,6 +15,7 @@ import {
watchingRequests,
type HandleResult,
type Preflight,
type NeutralOutcome,
} from './core';
import type { BodyFailure, BodyReader } from './body';
import type { RouteTable, WsRouteDef, MeshControl, HttpOptions } from './types';
@@ -184,44 +185,51 @@ function createRequestHandler(cfg: HandlerConfig) {
}
}
await writeResult(req, res, result, { opts, bus, correlation });
const written = writeResult(req, res, result, { opts, bus, correlation });
if (written) await written;
}
}
/**
* Writes what `handle()` produced: a preflight, or an outcome after `compressOutcome` (CARD-16).
* Module scope rather than inside `serve`, whose closure would charge every branch here again.
*
* Deliberately not `async`, like `handle()`: a buffered response without compression is written
* synchronously and returns nothing to await, so the common request allocates no promise here.
* Only a stream, or a compressed body, returns one.
*/
async function writeResult(
function writeResult(
req: http.IncomingMessage,
res: http.ServerResponse,
result: HandleResult | Preflight,
ctx: { opts: HttpOptions | undefined; bus: Bus | undefined; correlation: Correlation },
): Promise<void> {
const { opts, bus, correlation } = ctx;
): Promise<void> | undefined {
if ('preflight' in result) {
res.writeHead(204, result.preflight);
res.end();
return;
return undefined;
}
// Not awaited unless compression actually returned a promise: an app without it keeps the
// synchronous write it had.
const decided = compressOutcome(
opts?.compression,
{ method: req.method ?? 'GET', headers: req.headers },
result.outcome,
);
const outcome = decided instanceof Promise ? await decided : decided;
const compression = ctx.opts?.compression;
if (!compression) return writeOutcome(res, result.outcome, ctx);
if (outcome.kind === 'stream') {
await pipeStream(res, outcome, bus, opts?.streams, correlation);
return;
}
const decided = compressOutcome(compression, { method: req.method ?? 'GET', headers: req.headers }, result.outcome);
return decided instanceof Promise
? decided.then((outcome) => writeOutcome(res, outcome, ctx))
: writeOutcome(res, decided, ctx);
}
/** Pipes a stream (returning its promise) or writes a buffered outcome in one go (returning nothing). */
function writeOutcome(
res: http.ServerResponse,
outcome: NeutralOutcome,
ctx: { opts: HttpOptions | undefined; bus: Bus | undefined; correlation: Correlation },
): Promise<void> | undefined {
if (outcome.kind === 'stream') return pipeStream(res, outcome, ctx.bus, ctx.opts?.streams, ctx.correlation);
res.writeHead(outcome.status, outcome.headers);
res.end(outcome.body);
return undefined;
}
/**