perf(http): trim per-request work on the Node path #174

Merged
chrnx merged 4 commits from perf/node-request-path into develop 2026-10-27 08:03:12 +00:00
Owner

Recovers the throughput lost between 26.9.0-beta.2 and develop on the Node path, and goes past beta.2 for requests without a body. Behaviour is unchanged.

Changes

  • Requests without a body skip the stream. A request with no Transfer-Encoding and no, or a zero, Content-Length has no body (RFC 9112 §6.3). That covers every GET. readBody now resolves undefined for it without subscribing to the request stream, and Node drains the unread request when the response ends. This is the largest gain.
  • readBody reads a body with data/end/error/close listeners instead of for await. A single chunk is returned without Buffer.concat. The same 413s apply (declared and streamed), and so does the premature-close rejection.
  • writeResult is no longer async. When compression is off, a buffered response is written synchronously; only a stream or a compressed body returns a promise.
  • declaresOverLimit compares the number first, so the digit regex runs only for a body over the limit.
  • The serve().catch(failSafe) guard (#108) is untouched.
  • Bench: bench/compare.ts gains compression cases (an 8 KB JSON response uncompressed, identity-negotiated, gzip and br, plus a small response under the threshold). Until now no A/B case enabled compression.

Tests

  • test/read-body.test.ts: the bodiless fast path (no listener attached), chunk joining, empty body, 413 with destroy, declared-length 413, stream error, premature close.
  • The full suite passes (1202 tests), along with typecheck, lint, format and complexity.

Benchmark

jarvis, node:22 in Docker, 4 pinned cores, 10 rounds, 100 connections, 10 s per round.

Noise. An A/A run (develop against an identical copy) reads the second-measured side 0.6–3.8% low, so a delta inside ±4% is not a result. Every develop comparison below was also run in the reverse order, and the two orders agree.

Scenario develop → branch beta.2 → branch
hello +15.6% * +14.0% *
param +15.1% * +2.7%
param, 50 routes +9.6% * +11.8% *
param, 200 routes +15.5% * +23.0% *
pipeline (3 steps) +16.4% * +11.8% *
steps5 +15.2% * +11.2% *
hello + security +16.0% * +15.0% *
hello + cors predicate +15.0% * +12.9% *
preflight + cors predicate not re-run -1.9% (noise)
validate (POST) +6.0% * -1.8% (noise)
json 8KB uncompressed +3.8% +0.6%
hello + compression (under threshold) +16.0% * +5.8%
json 8KB + compression, identity +8.4% * n/a
json 8KB + gzip +2.2% n/a
json 8KB + br +5.0% n/a

* = the round ranges do not overlap. n/a = beta.2 has no compression.

Not measured

  • Streamed responses (SSE, pipeStream), with or without compression.
  • Request bodies other than a small JSON POST: large, chunked, multipart.
  • Deno, Bun and edge. This change is confined to the Node adapter (src/http/request.ts, src/http/server.ts). The edge and fetch path (src/http/web.ts) only calls declaresOverLimit, whose result is unchanged.
Recovers the throughput lost between 26.9.0-beta.2 and develop on the Node path, and goes past beta.2 for requests without a body. Behaviour is unchanged. ## Changes - **Requests without a body skip the stream.** A request with no `Transfer-Encoding` and no, or a zero, `Content-Length` has no body (RFC 9112 §6.3). That covers every GET. `readBody` now resolves `undefined` for it without subscribing to the request stream, and Node drains the unread request when the response ends. This is the largest gain. - `readBody` reads a body with `data`/`end`/`error`/`close` listeners instead of `for await`. A single chunk is returned without `Buffer.concat`. The same 413s apply (declared and streamed), and so does the premature-close rejection. - `writeResult` is no longer `async`. When compression is off, a buffered response is written synchronously; only a stream or a compressed body returns a promise. - `declaresOverLimit` compares the number first, so the digit regex runs only for a body over the limit. - The `serve().catch(failSafe)` guard (#108) is untouched. - Bench: `bench/compare.ts` gains compression cases (an 8 KB JSON response uncompressed, identity-negotiated, gzip and br, plus a small response under the threshold). Until now no A/B case enabled compression. ## Tests - `test/read-body.test.ts`: the bodiless fast path (no listener attached), chunk joining, empty body, 413 with destroy, declared-length 413, stream error, premature close. - The full suite passes (1202 tests), along with typecheck, lint, format and complexity. ## Benchmark jarvis, `node:22` in Docker, 4 pinned cores, 10 rounds, 100 connections, 10 s per round. **Noise.** An A/A run (develop against an identical copy) reads the second-measured side 0.6–3.8% low, so a delta inside ±4% is not a result. Every develop comparison below was also run in the reverse order, and the two orders agree. | Scenario | develop → branch | beta.2 → branch | |---|---|---| | hello | +15.6% * | +14.0% * | | param | +15.1% * | +2.7% | | param, 50 routes | +9.6% * | +11.8% * | | param, 200 routes | +15.5% * | +23.0% * | | pipeline (3 steps) | +16.4% * | +11.8% * | | steps5 | +15.2% * | +11.2% * | | hello + security | +16.0% * | +15.0% * | | hello + cors predicate | +15.0% * | +12.9% * | | preflight + cors predicate | not re-run | -1.9% (noise) | | validate (POST) | +6.0% * | -1.8% (noise) | | json 8KB uncompressed | +3.8% | +0.6% | | hello + compression (under threshold) | +16.0% * | +5.8% | | json 8KB + compression, identity | +8.4% * | n/a | | json 8KB + gzip | +2.2% | n/a | | json 8KB + br | +5.0% | n/a | `*` = the round ranges do not overlap. `n/a` = beta.2 has no compression. ## Not measured - Streamed responses (SSE, `pipeStream`), with or without compression. - Request bodies other than a small JSON POST: large, chunked, multipart. - Deno, Bun and edge. This change is confined to the Node adapter (`src/http/request.ts`, `src/http/server.ts`). The edge and fetch path (`src/http/web.ts`) only calls `declaresOverLimit`, whose result is unchanged.
chrnx added 1 commit 2026-10-26 18:21:05 +00:00
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
8b67b1a46c
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>
chrnx added 1 commit 2026-10-26 18:24:17 +00:00
bench(compare): add compression cases to the A/B harness
CI / dco (pull_request) Successful in 4s
CI / runtimes (bun) (pull_request) Successful in 19s
CI / build (pull_request) Successful in 30s
CI / runtimes (edge) (pull_request) Successful in 23s
CI / runtimes (deno) (pull_request) Successful in 34s
704622cf65
The A/B harness had no case with compression on, so a change to the
response write path was measured only on its uncompressed branch. Add an
8 KB JSON response uncompressed, identity-negotiated, gzip and br, plus a
small response under the threshold with compression enabled.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
chrnx force-pushed perf/node-request-path from ad2b33584b to 704622cf65 2026-10-26 18:24:17 +00:00 Compare
chrnx added 1 commit 2026-10-27 03:48:22 +00:00
perf(http): skip the body stream for a request without a body
CI / dco (pull_request) Successful in 5s
CI / runtimes (bun) (pull_request) Successful in 21s
CI / build (pull_request) Successful in 30s
CI / runtimes (edge) (pull_request) Successful in 24s
CI / runtimes (deno) (pull_request) Successful in 32s
88be9b1cb2
On Node every request goes through readBody, GET included. Subscribing
listeners to an empty request stream measured ~4% slower than the old
async iterator on an 8 KB GET response, in both orders on jarvis.

A request with no Transfer-Encoding and no or a zero Content-Length has
no body (RFC 9112 6.3), so resolve undefined without touching the
stream. Node drains the unread request when the response ends.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
chrnx added 1 commit 2026-10-27 05:31:57 +00:00
docs(changelog): note the bodiless-request fast path
CI / dco (pull_request) Successful in 5s
CI / runtimes (bun) (pull_request) Successful in 20s
CI / build (pull_request) Successful in 30s
CI / runtimes (edge) (pull_request) Successful in 22s
CI / runtimes (deno) (pull_request) Successful in 34s
d626fadbfd
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
chrnx merged commit 9648a9d1ed into develop 2026-10-27 08:03:12 +00:00
Sign in to join this conversation.