Repository navigation
Conversation
Iterating a `Headers` instance — `for (const [k, v] of headers)`,
`headers.entries()`, `[...headers]`, etc. — invokes the
`[_iterableHeaders]` getter via `mixinPairIterable`'s iterator
`next()` *on every step*. The getter rebuilt the lowercase / sorted
entries array from scratch each call, allocating a fresh `seenHeaders`
dict, an `entries` array, and one `[name, value]` tuple per unique
name, and running a sort.
For a 7-header Headers iterated once with `for...of`, that's 8
rebuilds (one per `next()`), so 8 dict allocations + 8 entries
arrays + 56 tuples + 8 sorts — pure waste, since the underlying
list never changed across the iteration.
The existing cache (`_iterableHeadersCache`) was only consulted when
`_guard === "immutable"`, so non-immutable Headers (the default
guard for `Response.headers`, `Request.headers`, `new Headers(...)`)
never benefited.
This commit:
- Caches the result for any guard.
- Tracks `_iterableHeadersCacheLength` alongside the cache so that
direct mutations to the underlying list (e.g.
`initializeAResponse` pushing a Content-Type header) are detected
by length-mismatch and force a rebuild on the next access.
- Explicitly clears the cache in `appendHeader` / `set` / `delete`
so that value-only replacements (which don't change length) are
also covered.
Numbers (cargo bench -p deno_fetch --bench headers, release, mean of
3 baseline / 5 after):
bench_iter_headers_7 8,436 → 810 µs/iter −90.4%
bench_iter_headers_7_after_set 9,342 → 3,156 µs/iter −66.2%
bench_iter_headers_7_fresh 11,975 → 5,769 µs/iter −51.8%
Even the "fresh" case (construct + iterate, throw away) speeds up
because the iterator's per-step `target[dataSymbol]` access now hits
the cache after the first `next()`.
Memory: per typical iteration, allocations drop from N+1 (one per
`next()`) to 1 — a ~7× allocation reduction for a 7-header iteration,
scaling linearly with header count.
Adds `ext/fetch/benches/headers.rs` (and the matching `[[bench]]`
entry plus `deno_bench_util` / `deno_web` / `deno_webidl` dev-deps in
`ext/fetch/Cargo.toml`); `ext/fetch` had no bench harness before.
|
IS this the same as #32728? |
|
Yes, you're right — the core change here (cache Differences worth noting in case any of them are useful for the older PR:
Happy to close this in favour of #32728 — would you like me to do that, or push the harness across as a follow-up if #32728 lands? |
|
Yes — you're right, this is the same core optimization as #32728. Apologies, I should have checked for prior art before opening. A couple of differences if useful for the comparison:
Happy to close in favor of #32728. If the bench harness is useful, I can split it into a separate prep PR (just the scaffolding, no JS change) so future ext/fetch perf work has it. Let me know which you prefer. |
fibibot
left a comment
There was a problem hiding this comment.
Substance is right. The win is real because mixinPairIterable.next() reads target[dataSymbol] on every iteration step (webidl/00_webidl.js:1170), so a 7-header for...of was calling the entries-rebuild getter 8 times — verified.
Walked the invalidation strategy:
- Cache hit gate:
cached !== undefined && _iterableHeadersCacheLength === list.length. Two independent guards. - Method mutations:
appendHeader(line 156),Headers.set(line 452),Headers.delete(line 353) all explicitly clear[_iterableHeadersCache] = undefined. ✓ - Direct
_headerListmutations:23_response.js:223–232initializeAResponsepush ofContent-Type: caught by length-divergence (_iterableHeadersCacheLength = N,list.length = N+1). ✓23_request.js:412–419splice/refill: the splice empties the list, thenfillHeaderscallsappendHeaderwhich clears the cache symbol on first push — and the length is also divergent in the meantime, so even a stale read between the two would rebuild. ✓
- Mid-iteration mutations (
for (const [k,v] of h) { h.delete("x") }): both old and new code give the same behavior — the mutation invalidates the cache, the nextnext()rebuilds, and the iterator'sindexkeeps incrementing across the new array. Identical pre-existing semantics, no regression.
Same fragility concern as #33683
The invalidation strategy catches:
- Mutations through
append/set/delete(cache symbol cleared explicitly). - Mutations that change
_headerList.length(length divergence).
It does not catch in-place same-length mutations like list[i] = ["new-name", val] or list[i][0] = "new-name". None exist in the codebase today, and headersFromHeaderList always replaces the list as a whole on a fresh Headers (cache starts undefined). Same suggestions I made on #33683 apply here too:
-
Add a
// IMPORTANT: if you mutate the returned list in place at the same length, also clear headers[_iterableHeadersCache].comment onheaderListFromHeaders. -
Or expose a small
invalidateHeaderCaches(headers)helper that future callers can use defensively. Coordinated with #33683's_lowerNamescache, both could share the same invalidation function:function invalidateHeaderCaches(headers) { headers[_iterableHeadersCache] = undefined; headers[_lowerNames] = null; // if #33683 lands }
The _iterableHeadersCacheLength field nicely doubles as a defense — but it's silent. A debug-mode assert that cached.length === list.length && every-name-matches on cache hit would catch desyncs loudly without runtime cost in release.
Coordination with #33683
#33683 (lowercased-names cache) and this PR share two concerns:
- Both add identical
[dev-dependencies]scaffolding (deno_bench_util/deno_web/deno_webidl) and the matching[[bench]]entry toext/fetch/Cargo.toml. Whichever lands first wins; the other will need to rebase on a bench harness that already exists. Worth extracting that scaffolding into a separate prep PR — it would unblock both perf PRs cleanly. - Both add cache invalidation to
appendHeader/set/delete. If both land, those methods will end up with two separateheaders[_someCache] = undefinedlines back-to-back. Easy to miss one in a future edit. A singleinvalidateHeaderCaches(headers)helper covers both.
Suggest landing this and #33683 sequentially with the second one rebased to use a shared helper.
Smaller nits
_iterableHeadersCacheLengthis declared inline (set on first cache write, undefined initially). Same pattern as_iterableHeadersCache. Consistent. ✓bench_iter_headers_7description says "subsequent calls hit the cache" — strictly the bench harness measures one fullfor...ofloop per iteration, and the cache hits within a single loop after the firstnext(). The "subsequent calls" framing in the bench comment is slightly off; the cache hits between iter-steps within one for-of, which is the real win. Cosmetic.
CI
Only license/cla and lint title have run — the full fetch/headers + WPT shards haven't started. Holding COMMENT per the CI-must-complete rule. Substance is clean; happy to flip to APPROVE once CI lands and either the cache-invalidation comment is added or the shared-helper consolidation with #33683 is decided.
|
Closing as duplicate of #32728 — confirmed by @bartlomieju and walking the diffs the core change (cache |
Summary
Iterating a
Headersinstance —for (const [k, v] of headers),headers.entries(),[...headers], etc. — invokes the[_iterableHeaders]getter viamixinPairIterable's iteratornext()on every step. The getter rebuilt the lowercase / sortedentries array from scratch each call, allocating a fresh
seenHeadersdict, anentriesarray, and one[name, value]tuple per unique name, and running a sort.
For a 7-header
Headersiterated once withfor...of, that's 8rebuilds (one per
next()) — 8 dict allocations + 8 entries arrayslist never changes across the iteration.
The existing cache (
_iterableHeadersCache) was only consulted when_guard === "immutable", so non-immutable Headers — which is whatResponse.headers,Request.headers, andnew Headers(...)alldefault to — never benefited.
This PR:
_iterableHeadersCacheLengthalongside the cache so thatdirect mutations to the underlying list (e.g.
initializeAResponsepushing a
Content-Typeheader into_headerList) are detected bylength-mismatch and force a rebuild on the next access.
appendHeader/set/deleteso that value-only replacements (which don't change length) are
also covered.
Numbers
cargo bench -p deno_fetch --bench headers(release, mean of 3baseline / 5 after — variance verified across both):
bench_iter_headers_7(reused, hits cache after first iter)bench_iter_headers_7_after_set(mutate, then iterate; cache invalidated)bench_iter_headers_7_fresh(construct + iterate, discard)Even the "fresh" case (construct, iterate once, throw away) speeds
up by ~2× because the iterator's per-step
target[dataSymbol]access now hits the cache after the first
next()within a singlefor...of.Memory
Allocations per typical iteration of a 7-header instance drop from
~80 (8
seenHeadersdicts + 8entriesarrays + 8×7 tuples + 8sort scratch) to ~10 (one rebuild + one cache slot read per
next()). Scales linearly with header count.Test plan
cargo bench -p deno_fetch --bench headersx5 before / afterset, delete, has, get, getSetCookie, iteration order,
duplicate-name combine, set-cookie split, immutable behaviour)
fetch/api/headerssuiteext/fetchhad no bench harness before this PR —ext/fetch/benches/headers.rsis new (plus the matching[[bench]]entry anddeno_bench_util/deno_web/deno_webidldev-deps inext/fetch/Cargo.toml). Happy to split the harness scaffolding into a separate prep PR if a maintainer prefers.