perf(compression): stop charging uncompressed responses for compression being on #179

Merged
chrnx merged 2 commits from perf/compression-skip-buffer into develop 2026-10-29 14:47:38 +00:00
Owner

With compression on, a response that was never compressed still cost 17% on Deno and 7% on Bun (Mac, small hello). Two things caused it:

  1. The eligibility check (e8842e6). It read four headers through findHeader, each one an Object.keys plus a toLowerCase per key, and then ran two regexes on every response.
    • It now walks the keys once and keeps the first value of each name, as findHeader did.
    • The content-type decision is memoized for the last type seen.
    • no-transform is tested only when cache-control is set.
    • No behaviour change.
  2. Vary: Accept-Encoding on a body under the threshold (fe4827c).
    • Why it can go. Such a body is sent identity whatever the client accepts. Accept-Encoding played no part, so RFC 9110 §12.5.5 asks for no Vary. A cache that stores the response for everyone is right to, since every client accepts identity.
    • Why it mattered. Deno pays about 10% of a small request for the header, Bun about 3%. The cost is inside Deno: our objects and body type are unchanged, and a bare Deno.serve shows part of it.
    • Unchanged. These keep Vary:
      • encoded responses;
      • responses that are identity for a request-side reason (identity client, HEAD);
      • every 304;
      • every compressible edge response, where Cloudflare encodes.
    • Byte allocation. The size is now measured with Buffer.byteLength, and the bytes are allocated only when the body is encoded.

Compression is not in 26.9.0-beta.2, so this changes no published behaviour. The CHANGELOG entry for createApp({ compression }) is updated.

Measured

develop (1935e8e) → this branch on a Ryzen 3700X, docker node:22, 4 pinned cores. 15 cases, 4 rounds, A/A noise band ±4%.

Runtime Uncompressed cases (11) hello + compression json 8KB + compression, identity gzip / br
Node flat, −3.4% to +2.5% −1.5%, overlapping +9.3%, overlapping flat
Deno flat, −2.6% to +0.9% +33.0% (48.4k → 64.3k) +8.9% +1.2% / +3.3%
Bun flat, −2.7% to +1.8% +8.3% +4.8%, overlapping flat

On Deno, hello with compression on now runs at the speed of hello with no compression at all: 64.3k against 64.2k.

Tests

  • T7 (under the threshold) now expects no Vary, in test/compression.test.ts and in the shared Deno/Bun fixture.
  • npm test: 1210 passed.
  • test:deno: 63 passed.
  • test:bun: 52 passed.
  • test:edge: OK.
  • npm audit: clean.
With compression on, a response that was never compressed still cost 17% on Deno and 7% on Bun (Mac, small `hello`). Two things caused it: 1. **The eligibility check** (`e8842e6`). It read four headers through `findHeader`, each one an `Object.keys` plus a `toLowerCase` per key, and then ran two regexes on every response. - It now walks the keys once and keeps the first value of each name, as `findHeader` did. - The content-type decision is memoized for the last type seen. - `no-transform` is tested only when `cache-control` is set. - No behaviour change. 2. **`Vary: Accept-Encoding` on a body under the threshold** (`fe4827c`). - **Why it can go.** Such a body is sent identity whatever the client accepts. `Accept-Encoding` played no part, so RFC 9110 §12.5.5 asks for no `Vary`. A cache that stores the response for everyone is right to, since every client accepts identity. - **Why it mattered.** Deno pays about 10% of a small request for the header, Bun about 3%. The cost is inside Deno: our objects and body type are unchanged, and a bare `Deno.serve` shows part of it. - **Unchanged.** These keep `Vary`: - encoded responses; - responses that are identity for a request-side reason (identity client, `HEAD`); - every `304`; - every compressible edge response, where Cloudflare encodes. - **Byte allocation.** The size is now measured with `Buffer.byteLength`, and the bytes are allocated only when the body is encoded. Compression is not in 26.9.0-beta.2, so this changes no published behaviour. The CHANGELOG entry for `createApp({ compression })` is updated. ## Measured `develop (1935e8e) → this branch` on a Ryzen 3700X, docker `node:22`, 4 pinned cores. 15 cases, 4 rounds, A/A noise band ±4%. | Runtime | Uncompressed cases (11) | `hello + compression` | `json 8KB + compression, identity` | gzip / br | | --- | --- | --- | --- | --- | | Node | flat, −3.4% to +2.5% | −1.5%, overlapping | +9.3%, overlapping | flat | | Deno | flat, −2.6% to +0.9% | **+33.0%** (48.4k → 64.3k) | **+8.9%** | +1.2% / +3.3% | | Bun | flat, −2.7% to +1.8% | **+8.3%** | +4.8%, overlapping | flat | On Deno, `hello` with compression on now runs at the speed of `hello` with no compression at all: 64.3k against 64.2k. ## Tests - T7 (under the threshold) now expects no `Vary`, in `test/compression.test.ts` and in the shared Deno/Bun fixture. - `npm test`: 1210 passed. - `test:deno`: 63 passed. - `test:bun`: 52 passed. - `test:edge`: OK. - `npm audit`: clean.
chrnx added 2 commits 2026-10-29 14:46:25 +00:00
eligible() read four headers through findHeader, each an Object.keys and
a toLowerCase per key, then ran two regexes, on every response while
compression is on. It now walks the keys once, keeps the first value of
each name as findHeader did, memoizes the content-type decision for the
last type seen, and tests no-transform only when cache-control is set.

Measured on Deno, hello with compression on (under the threshold): +2-5%.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
perf(compression): send no Vary on a body under the threshold
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 33s
fe4827ccb3
A buffered body under the threshold is sent identity whatever the client
accepts, so Accept-Encoding played no part in it and RFC 9110 asks for
no Vary. A cache that stores it for everyone is right to: every client
accepts identity. Responses that are encoded, or that are identity for
a request-side reason (identity client, HEAD), and every 304 keep Vary,
as does every compressible edge response, since Cloudflare encodes
there.

Deno pays about 10% of a small request for the header, Bun about 3%.
The size is also measured with Buffer.byteLength instead of allocating
the bytes, which are now built only when the body is encoded.

Measured on the Mac, hello with compression on against compression off:
Deno -17% -> -2%, Bun -7% -> -3%.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
chrnx merged commit 4566f319b5 into develop 2026-10-29 14:47:38 +00:00
Sign in to join this conversation.