perf(http): resolve ctx.ip on first read instead of on every request #176

Merged
chrnx merged 1 commits from perf/lazy-ctx-ip into develop 2026-10-28 19:34:50 +00:00
Owner

ctx.ip is now resolved the first time something reads it (a step, a handler, or a plugin such as rate-limit), and kept for the rest of the request. A route that never reads it pays nothing.

Why

Since #117 every Bun request called server.requestIP(), whether anything read the address or not. That native call is 7–11% of a small request. Node and Deno get the address almost for free, so the change costs them nothing and only Bun gains.

What changed

  • src/http/peer.ts, src/bun.ts. Bun records its server instead of the address. peerOf calls requestIP() only when asked.
  • src/http/core.ts. Adapters pass a resolveIp function instead of an ip string. The handler receives a RouteRequest whose ip getter resolves once.
  • src/app/index.ts, src/pipeline.ts. The pipeline context is an HttpSeed whose ip reads and writes through to the request. The seed is no longer spread, because a spread would read the getter.
  • Resolution rules are unchanged. Under trustProxy: cf-connecting-ip on edge, then the first x-forwarded-for hop. Otherwise the socket peer.

The getters sit on class prototypes on purpose. A first version put get ip() / set ip() in the object literals, and V8 and JSC treat such literals as slow objects. Every read of every key paid for it: Node −14% to −18%, Deno −18% to −24%.

Behaviour

  • Streams. Inside a stream body ctx.ip is '' unless a step or the handler read it before returning the stream. The body runs after the response has started, when the socket may be gone. Rate-limit already reads it first.
  • Steps may still assign ctx.ip.
  • Behind a proxy, trustProxy: true reads the address from the header on every runtime and never asks the socket.

Measured

develop → this branch on a Ryzen 3700X, docker node:22, 4 pinned cores. 15 cases (including gzip, brotli and identity compression), 4 rounds, A/A noise band ±4%. The run was made before #175 merged, so it isolates this change.

Runtime Result
Node flat: −3.4% to +1.7%, every case overlapping
Deno flat: −2.5% to +4.4%. br shows −7.2% with overlapping ranges, and was −0.4% in the previous run.
Bun +3% to +11%, non-overlapping on 10 of 15 cases (hello +8.7%, security +9.4%, preflight +10.6%, 50 routes +11.2%, compression +7%)

validate (POST) on Bun gains only +1.1%, because the body read dominates it; that part is #175's.

Not covered: WebSocket upgrades. app.upgrade resolves the address as before, once per connection.

Tests

Five new tests in test/fetch.test.ts ("ctx.ip is resolved only when read") use a fake peer source that counts requestIP calls:

  • not asked when unread;
  • asked once when read twice;
  • '' and not asked inside a stream;
  • kept when a step read it first;
  • a step may assign it.

npm test 1210 passed, npm audit clean.

`ctx.ip` is now resolved the first time something reads it (a step, a handler, or a plugin such as rate-limit), and kept for the rest of the request. A route that never reads it pays nothing. ## Why Since #117 every Bun request called `server.requestIP()`, whether anything read the address or not. That native call is 7–11% of a small request. Node and Deno get the address almost for free, so the change costs them nothing and only Bun gains. ## What changed - **`src/http/peer.ts`, `src/bun.ts`.** Bun records its server instead of the address. `peerOf` calls `requestIP()` only when asked. - **`src/http/core.ts`.** Adapters pass a `resolveIp` function instead of an `ip` string. The handler receives a `RouteRequest` whose `ip` getter resolves once. - **`src/app/index.ts`, `src/pipeline.ts`.** The pipeline context is an `HttpSeed` whose `ip` reads and writes through to the request. The seed is no longer spread, because a spread would read the getter. - **Resolution rules are unchanged.** Under `trustProxy`: `cf-connecting-ip` on edge, then the first `x-forwarded-for` hop. Otherwise the socket peer. The getters sit on class prototypes on purpose. A first version put `get ip()` / `set ip()` in the object literals, and V8 and JSC treat such literals as slow objects. Every read of every key paid for it: Node −14% to −18%, Deno −18% to −24%. ## Behaviour - **Streams.** Inside a stream body `ctx.ip` is `''` unless a step or the handler read it before returning the stream. The body runs after the response has started, when the socket may be gone. Rate-limit already reads it first. - **Steps** may still assign `ctx.ip`. - **Behind a proxy**, `trustProxy: true` reads the address from the header on every runtime and never asks the socket. ## Measured `develop → this branch` on a Ryzen 3700X, docker `node:22`, 4 pinned cores. 15 cases (including gzip, brotli and identity compression), 4 rounds, A/A noise band ±4%. The run was made before #175 merged, so it isolates this change. | Runtime | Result | | --- | --- | | Node | flat: −3.4% to +1.7%, every case overlapping | | Deno | flat: −2.5% to +4.4%. br shows −7.2% with overlapping ranges, and was −0.4% in the previous run. | | Bun | +3% to +11%, non-overlapping on 10 of 15 cases (hello +8.7%, security +9.4%, preflight +10.6%, 50 routes +11.2%, compression +7%) | `validate (POST)` on Bun gains only +1.1%, because the body read dominates it; that part is #175's. Not covered: WebSocket upgrades. `app.upgrade` resolves the address as before, once per connection. ## Tests Five new tests in `test/fetch.test.ts` ("ctx.ip is resolved only when read") use a fake peer source that counts `requestIP` calls: - not asked when unread; - asked once when read twice; - `''` and not asked inside a stream; - kept when a step read it first; - a step may assign it. `npm test` 1210 passed, `npm audit` clean.
chrnx added 1 commit 2026-10-28 19:31:54 +00:00
perf(http): resolve ctx.ip on first read instead of on every request
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 23s
CI / runtimes (deno) (pull_request) Successful in 34s
b86cd605c4
Every Bun request called server.requestIP() for ctx.ip (#116), a native
call that builds an object, whether anything read the address or not. On
Mac that measured 7-11% of a small request; recording the server in the
peer map instead costs nothing.

The neutral request now carries a resolver rather than an address. The
handler request and the pipeline seed expose ctx.ip as an accessor that
resolves once on first read, and a step may still assign it. runPipeline
uses the seed it is given instead of spreading it, which would have
evaluated the accessor; both callers build the seed fresh per request.

Inside a stream body ctx.ip is '' unless a step or the handler read it
before the stream was returned: the body runs after the response has
started, when the socket may be gone.

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