Merge pull request 'perf(compression): stop charging uncompressed responses for compression being on' (#179) from perf/compression-skip-buffer into develop
CI / dco (push) Has been skipped
CI / build (push) Successful in 29s
CI / runtimes (bun) (push) Successful in 20s
CI / runtimes (deno) (push) Successful in 33s
CI / runtimes (edge) (push) Successful in 23s
Promote → GitHub / push-github (push) Successful in 7s
CI / dco (push) Has been skipped
CI / build (push) Successful in 29s
CI / runtimes (bun) (push) Successful in 20s
CI / runtimes (deno) (push) Successful in 33s
CI / runtimes (edge) (push) Successful in 23s
Promote → GitHub / push-github (push) Successful in 7s
This commit was merged in pull request #179.
This commit is contained in:
commit
4566f319b5
4 files changed
+51
-21
No files matched your search
+4
-1
@@ -163,7 +163,10 @@ npm treats versions as semver and semver forbids leading zeros.
|
|||||||
preference. Text, JSON, JavaScript, ndjson, SVG, XML and wasm are compressed. A response that
|
preference. Text, JSON, JavaScript, ndjson, SVG, XML and wasm are compressed. A response that
|
||||||
already has `Content-Encoding` or `Content-Range`, has no `Content-Type`, or says
|
already has `Content-Encoding` or `Content-Range`, has no `Content-Type`, or says
|
||||||
`Cache-Control: no-transform` is left alone. `Vary: Accept-Encoding` is added wherever the request
|
`Cache-Control: no-transform` is left alone. `Vary: Accept-Encoding` is added wherever the request
|
||||||
decided the outcome: identity clients, `HEAD`, bodies under the threshold and every `304`.
|
decided the outcome: identity clients, `HEAD` and every `304`. A body under the threshold gets
|
||||||
|
none: it is sent identity whatever the client accepts, and Deno pays about 10% of a small request
|
||||||
|
for the header (about 3% on Bun). On the edge, where Cloudflare encodes, every compressible
|
||||||
|
response keeps `Vary`.
|
||||||
`Content-Length` is dropped from an encoded body. SSE and ndjson streams are compressed too,
|
`Content-Length` is dropped from an encoded body. SSE and ndjson streams are compressed too,
|
||||||
flushed after every event, error frame and ping, so each event decodes when it arrives.
|
flushed after every event, error frame and ping, so each event decodes when it arrives.
|
||||||
Per runtime:
|
Per runtime:
|
||||||
|
|||||||
+45
-18
@@ -192,25 +192,47 @@ const COMPRESSIBLE =
|
|||||||
|
|
||||||
type Headers = Record<string, string | string[]>;
|
type Headers = Record<string, string | string[]>;
|
||||||
|
|
||||||
/** A header's value as one string, case-insensitively, or `undefined`. */
|
|
||||||
function read(headers: Headers, name: string): string | undefined {
|
|
||||||
const key = findHeader(headers, name);
|
|
||||||
if (key === undefined) return undefined;
|
|
||||||
const value = headers[key];
|
|
||||||
return Array.isArray(value) ? value.join(', ') : value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Whether the response's representation could vary by `Accept-Encoding` at all. When it cannot —
|
* Whether the response's representation could vary by `Accept-Encoding` at all. When it cannot —
|
||||||
* already encoded, a range, no or an incompressible type, `no-transform` — core neither encodes
|
* already encoded, a range, no or an incompressible type, `no-transform` — core neither encodes
|
||||||
* nor adds `Vary`. Request-side reasons (HEAD, the client's header) do not appear here: those
|
* nor adds `Vary`. A buffered body under the threshold is the same case, decided in
|
||||||
|
* {@link compressBuffered} once the size is known. Request-side reasons (HEAD, the client's header) do not appear here: those
|
||||||
* responses still get `Vary`, so a shared cache keys them correctly.
|
* responses still get `Vary`, so a shared cache keys them correctly.
|
||||||
*/
|
*/
|
||||||
function eligible(headers: Headers): boolean {
|
function eligible(headers: Headers): boolean {
|
||||||
if (read(headers, 'content-encoding') !== undefined || read(headers, 'content-range') !== undefined) return false;
|
// One pass over the keys rather than one `read` per header: every response with compression on
|
||||||
const type = read(headers, 'content-type');
|
// comes through here, and four case-insensitive scans plus two regexes cost ~8% of a small Deno
|
||||||
if (!type || !COMPRESSIBLE.test(type)) return false;
|
// request. The first key of each name wins, as `findHeader` would pick it.
|
||||||
return !/(^|[\s,])no-transform\s*(,|$)/i.test(read(headers, 'cache-control') ?? '');
|
let type: string | undefined;
|
||||||
|
let cacheControl: string | undefined;
|
||||||
|
|
||||||
|
for (const key of Object.keys(headers)) {
|
||||||
|
const value = headers[key];
|
||||||
|
if (value === undefined) continue;
|
||||||
|
const lower = key.toLowerCase();
|
||||||
|
if (lower === 'content-encoding' || lower === 'content-range') return false;
|
||||||
|
if (lower === 'content-type') type ??= Array.isArray(value) ? value.join(', ') : value;
|
||||||
|
else if (lower === 'cache-control') cacheControl ??= Array.isArray(value) ? value.join(', ') : value;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!type || !compressibleType(type)) return false;
|
||||||
|
return cacheControl === undefined || !NO_TRANSFORM.test(cacheControl);
|
||||||
|
}
|
||||||
|
|
||||||
|
const NO_TRANSFORM = /(^|[\s,])no-transform\s*(,|$)/i;
|
||||||
|
|
||||||
|
// ponytail: a one-entry memo, since an app answers nearly every response with the same type or two;
|
||||||
|
// a bounded map if a mix of types ever measures.
|
||||||
|
let lastType: string | undefined;
|
||||||
|
let lastCompressible = false;
|
||||||
|
|
||||||
|
function compressibleType(type: string): boolean {
|
||||||
|
if (type !== lastType) {
|
||||||
|
lastType = type;
|
||||||
|
lastCompressible = COMPRESSIBLE.test(type);
|
||||||
|
}
|
||||||
|
|
||||||
|
return lastCompressible;
|
||||||
}
|
}
|
||||||
|
|
||||||
function withVary(headers: Headers): Headers {
|
function withVary(headers: Headers): Headers {
|
||||||
@@ -291,15 +313,20 @@ function compressBuffered(
|
|||||||
if (status === 304) return { ...outcome, headers: withVary(headers) };
|
if (status === 304) return { ...outcome, headers: withVary(headers) };
|
||||||
if (status === 204 || !eligible(headers)) return outcome;
|
if (status === 204 || !eligible(headers)) return outcome;
|
||||||
|
|
||||||
const vary = { ...outcome, headers: withVary(headers) };
|
const vary = (): Buffered => ({ ...outcome, headers: withVary(headers) });
|
||||||
if (status < 200 || req.method === 'HEAD' || !cfg.encode) return vary;
|
if (status < 200 || req.method === 'HEAD' || !cfg.encode) return vary();
|
||||||
|
|
||||||
const bytes = typeof body === 'string' ? Buffer.from(body) : body;
|
// Under the threshold the body is sent identity whatever the client accepts, so `Accept-Encoding`
|
||||||
if (bytes.byteLength < cfg.threshold) return vary;
|
// played no part and there is no `Vary` to send (RFC 9110 §12.5.5). A cache that stores this
|
||||||
|
// response for everyone is right to: every client accepts identity. Skipping it matters on Deno,
|
||||||
|
// which pays ~10% of a small request for a `Vary` header. Measured without allocating the bytes.
|
||||||
|
const size = typeof body === 'string' ? Buffer.byteLength(body) : body.byteLength;
|
||||||
|
if (size < cfg.threshold) return outcome;
|
||||||
|
|
||||||
const encoding = negotiate(acceptOf(req), cfg.encodings);
|
const encoding = negotiate(acceptOf(req), cfg.encodings);
|
||||||
if (!encoding) return vary;
|
if (!encoding) return vary();
|
||||||
|
|
||||||
|
const bytes = typeof body === 'string' ? Buffer.from(body) : body;
|
||||||
return encodeBody(bytes, encoding).then((encoded) => ({
|
return encodeBody(bytes, encoding).then((encoded) => ({
|
||||||
...outcome,
|
...outcome,
|
||||||
headers: encodedHeaders(headers, encoding),
|
headers: encodedHeaders(headers, encoding),
|
||||||
|
|||||||
@@ -135,7 +135,7 @@ for (const adapter of ['node', 'fetch'] as const) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it.each([
|
it.each([
|
||||||
['/c/small', 'T7 under threshold', true],
|
['/c/small', 'T7 under threshold', false],
|
||||||
['/c/png', 'T8 image/png', false],
|
['/c/png', 'T8 image/png', false],
|
||||||
['/c/notype', 'T8 missing content-type', false],
|
['/c/notype', 'T8 missing content-type', false],
|
||||||
['/c/range', 'T8 content-range', false],
|
['/c/range', 'T8 content-range', false],
|
||||||
|
|||||||
Vendored
+1
-1
@@ -60,7 +60,7 @@ async function negotiation(port: number): Promise<void> {
|
|||||||
/** T7, T8, T11, T15: skips, and the 304 shape. */
|
/** T7, T8, T11, T15: skips, and the 304 shape. */
|
||||||
async function skips(port: number): Promise<void> {
|
async function skips(port: number): Promise<void> {
|
||||||
for (const [path, vary] of [
|
for (const [path, vary] of [
|
||||||
['/c/small', true],
|
['/c/small', false],
|
||||||
['/c/png', false],
|
['/c/png', false],
|
||||||
['/c/notype', false],
|
['/c/notype', false],
|
||||||
['/c/range', false],
|
['/c/range', false],
|
||||||
|
|||||||
Reference in new issue
Block a user