perf(http): recover the fetch adapter's per-request cost on Deno and Bun #175

Merged
chrnx merged 2 commits from perf/web-headers-fast-path into develop 2026-10-28 19:31:15 +00:00
Owner

Two fixes to the fetch adapter (Deno, Bun, workerd). Both recover cost that earlier merges in this release added to every request. Node is not touched.

What changed

  1. Response headers (src/http/headers.ts). toWebHeaders copied every response's headers into a Headers object one by one, so that set-cookie arrays survive. A record that holds only strings, which is nearly every response, now goes to new Response as it is. Arrays and undefined values still go through Headers.
  2. Request body (src/http/web.ts). A body with a well-formed Content-Length inside the limit is read with arrayBuffer(), not chunk by chunk.
    • Why this is safe: the runtime's HTTP parser frames the body by that header, so the read cannot overrun it. byteLength is still checked, for a Request built in user code whose header and body disagree.
    • What keeps the bounded read: chunked uploads and malformed lengths.
    • Introduced by: 4d1639f (#117, FPRD-001a, incremental 413).

Behaviour is unchanged: the same 413s, and set-cookie arrays still reach the client one line per cookie.

New tests:

  • a plain-string record is returned untouched;
  • undefined header values are dropped;
  • a declared content-length under the limit with a longer body still answers 413.

How the causes were found

  • Deno. A bisect of beta.2 against each develop merge, run on jarvis, put the step at 50010da. Variants of 50010da, run on Mac:
    • with the header fast path: back to noise;
    • with the resolveOne change reverted: no effect.
  • Bun. The bisect put the POST drop at 4d1639f. On Mac, a variant that reads with arrayBuffer() took validate from 52k to 111k req/s; beta.2 does 121k.

Measurements

Setup:

  • bench/compare.ts, on jarvis (Ryzen 7 3700X), pinned to --cpuset-cpus 6,7,14,15.
  • 4 rounds, 10 s per round, order alternated each round.
  • All 15 cases: GET, POST, CORS, preflight, routing at 50 and 200 routes, 8 KB uncompressed, and the compression cases (identity, gzip, br).
  • An A/A run on this box reads the second side 0.6–3.8% low, so a delta inside ±4% is noise.

develop (9648a9d) → this branch:

Runtime Result
Node every case within ±2%; the two 8 KB cases at −5%, with overlapping ranges. Node does not use the changed code.
Deno +5% to +12% on param, pipeline, steps5, validate, security, CORS, preflight and routing. hello and the 8 KB cases are noise.
Bun validate +178% (11,732 → 32,662). Everything else is noise, except hello + security −3.5% and hello + compression −2.6%, which have non-overlapping ranges and are small.

beta.2 → this branch. A gap remains, and this PR does not close it:

Case Deno Bun
hello −3.9% −12.8%
param −8.3% −15.4%
validate (POST) −8.8% −16.0%
hello + security −8.0% −12.1%
  • Bun. Most of the remaining gap is setPeer + server.requestIP(). These run on every request, even when nothing reads ctx.ip (#116). On Mac, removing them is worth +7% to +11%. A follow-up will resolve ctx.ip lazily.
  • Deno. Part of the remaining gap is a −3% to −6% drift across the first merges after beta.2, which the bisect also showed.
  • Not regressions. gzip, br and hello + compression are not comparable against beta.2, because beta.2 has no compression and sends those responses uncompressed.

Not measured

  • workerd throughput. The edge suite passes, but no benchmark ran on workerd.
  • Chunked uploads. That read path is unchanged, so it was not benchmarked.

Checks

npm run typecheck, lint (0 errors), format:check, test (1205 passed), complexity:check, test:deno, test:bun and test:edge all pass. npm audit --audit-level=moderate reports 0 vulnerabilities.

Two fixes to the fetch adapter (Deno, Bun, workerd). Both recover cost that earlier merges in this release added to every request. Node is not touched. ## What changed 1. **Response headers** (`src/http/headers.ts`). `toWebHeaders` copied every response's headers into a `Headers` object one by one, so that `set-cookie` arrays survive. A record that holds only strings, which is nearly every response, now goes to `new Response` as it is. Arrays and `undefined` values still go through `Headers`. - Introduced by: 50010da (#140, cookies). 2. **Request body** (`src/http/web.ts`). A body with a well-formed `Content-Length` inside the limit is read with `arrayBuffer()`, not chunk by chunk. - Why this is safe: the runtime's HTTP parser frames the body by that header, so the read cannot overrun it. `byteLength` is still checked, for a `Request` built in user code whose header and body disagree. - What keeps the bounded read: chunked uploads and malformed lengths. - Introduced by: 4d1639f (#117, FPRD-001a, incremental 413). Behaviour is unchanged: the same `413`s, and `set-cookie` arrays still reach the client one line per cookie. New tests: - a plain-string record is returned untouched; - `undefined` header values are dropped; - a declared `content-length` under the limit with a longer body still answers 413. ## How the causes were found - **Deno.** A bisect of beta.2 against each develop merge, run on jarvis, put the step at 50010da. Variants of 50010da, run on Mac: - with the header fast path: back to noise; - with the `resolveOne` change reverted: no effect. - **Bun.** The bisect put the POST drop at 4d1639f. On Mac, a variant that reads with `arrayBuffer()` took validate from 52k to 111k req/s; beta.2 does 121k. ## Measurements Setup: - `bench/compare.ts`, on jarvis (Ryzen 7 3700X), pinned to `--cpuset-cpus 6,7,14,15`. - 4 rounds, 10 s per round, order alternated each round. - All 15 cases: GET, POST, CORS, preflight, routing at 50 and 200 routes, 8 KB uncompressed, and the compression cases (identity, gzip, br). - An A/A run on this box reads the second side 0.6–3.8% low, so a delta inside ±4% is noise. **develop (9648a9d) → this branch:** | Runtime | Result | |---|---| | Node | every case within ±2%; the two 8 KB cases at −5%, with overlapping ranges. Node does not use the changed code. | | Deno | +5% to +12% on param, pipeline, steps5, validate, security, CORS, preflight and routing. hello and the 8 KB cases are noise. | | Bun | **validate +178%** (11,732 → 32,662). Everything else is noise, except hello + security −3.5% and hello + compression −2.6%, which have non-overlapping ranges and are small. | **beta.2 → this branch.** A gap remains, and this PR does not close it: | Case | Deno | Bun | |---|---|---| | hello | −3.9% | −12.8% | | param | −8.3% | −15.4% | | validate (POST) | −8.8% | −16.0% | | hello + security | −8.0% | −12.1% | - **Bun.** Most of the remaining gap is `setPeer` + `server.requestIP()`. These run on every request, even when nothing reads `ctx.ip` (#116). On Mac, removing them is worth +7% to +11%. A follow-up will resolve `ctx.ip` lazily. - **Deno.** Part of the remaining gap is a −3% to −6% drift across the first merges after beta.2, which the bisect also showed. - **Not regressions.** gzip, br and hello + compression are not comparable against beta.2, because beta.2 has no compression and sends those responses uncompressed. ## Not measured - workerd throughput. The edge suite passes, but no benchmark ran on workerd. - Chunked uploads. That read path is unchanged, so it was not benchmarked. ## Checks `npm run typecheck`, `lint` (0 errors), `format:check`, `test` (1205 passed), `complexity:check`, `test:deno`, `test:bun` and `test:edge` all pass. `npm audit --audit-level=moderate` reports 0 vulnerabilities.
chrnx added 2 commits 2026-10-28 12:48:07 +00:00
toWebHeaders copied every response's headers into a Headers object one by
one, so set-cookie arrays survive. Deno and Bun build native headers from a
record directly, and the JS copy cost 5-8% on every Deno request (bisected to
50010da). A record holding only strings is now returned as it is; anything
else still goes through Headers.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
perf(http): read a declared-length fetch body with arrayBuffer()
CI / dco (pull_request) Successful in 5s
CI / runtimes (bun) (pull_request) Successful in 20s
CI / build (pull_request) Successful in 29s
CI / runtimes (edge) (pull_request) Successful in 23s
CI / runtimes (deno) (pull_request) Successful in 32s
133af8dfe5
The chunked reader added for the incremental 413 (4d1639f) cut a Bun JSON
POST by more than half: 37.6k -> 11.7k req/s on jarvis. A well-formed
Content-Length inside the limit already bounds the body, because the
runtime frames it by that header, so arrayBuffer() is safe there and native
in Bun, Deno and workerd. byteLength is still checked for a Request built in
user code whose header and body disagree. Chunked uploads keep the bounded
read.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
chrnx merged commit 109297ca4c into develop 2026-10-28 19:31:15 +00:00
Sign in to join this conversation.