Repository navigation
perf(ext/fetch): cache Headers iteration for all guard types - #32728
bartlomieju wants to merge 2 commits into
Conversation
The `_iterableHeaders` getter was rebuilding the sorted entries array on every access for non-immutable Headers (guard !== "immutable"), which is the common case for user-created Headers objects. This caused ~60x slower iteration compared to a Map. Cache the computed entries for all guard types and invalidate on mutation (append, set, delete). This reduces the gap from ~60x to ~3x. Closes #20980 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
is this safe from reentrancy? |
lucacasonato
left a comment
There was a problem hiding this comment.
Are there no places we mutate the header list internally?
|
@lucacasonato Good catch — there are two places that mutate the header list internally without going through
In practice these are safe today because they happen during construction before the |
|
@devsnek Yes, it's safe from reentrancy. The The cache is written only after the entries array is fully computed. The mutation methods ( The iterator ( |
fibibot
left a comment
There was a problem hiding this comment.
Substance is right (cache _iterableHeaders for all guard types and invalidate on mutation), and the three sites added in this PR (appendHeader, Request._init splice, initializeAResponse Content-Type push) close the cases @lucacasonato flagged. But there are at least three more direct request.headerList mutation sites in ext/fetch/26_fetch.js that bypass Headers-level mutation methods — pre-PR these were safe because non-immutable guards never cached, post-PR they leave a stale cache.
Concrete failure mode
const req = new Request("http://example.com", { headers: { Foo: "bar" } });
[...req.headers]; // populates _iterableHeadersCache (guard="request")
await fetch(req); // mainFetch pushes Accept/Accept-Language onto request.headerList
[...req.headers]; // STALE: returns the pre-fetch entries, no AcceptPre-PR, the second iteration rebuilt entries from _headerList, picking up the new Accept/Accept-Language. Post-PR the cache lookup at 20_headers.js:236 short-circuits with stale data.
User visibility: edge case (post-fetch header inspection on the same Request object) but a behavioral regression vs current main.
Sites the PR misses
All mutate the raw request.headerList/inner.headerList array bypassing the Headers instance:
ext/fetch/26_fetch.js:316 ArrayPrototypeSplice(request.headerList, i, 1); // redirect: drop body headers
ext/fetch/26_fetch.js:337 ArrayPrototypeSplice(request.headerList, i, 1); // redirect: drop sensitive headers
ext/fetch/26_fetch.js:415 ArrayPrototypePush(request.headerList, ["Accept", "*/*"]);
ext/fetch/26_fetch.js:419 ArrayPrototypePush(request.headerList, ["Accept-Language", "*"]);request.headerList here is the InnerRequest's raw array; the user-facing Headers wrapper is created via headersFromHeaderList(request.headerList, "request") at 23_request.js:31 (and shares the array by reference). The wrapper's _iterableHeadersCache doesn't see these direct array mutations.
23_response.js:281 (ArrayPrototypePush(inner.headerList, ["Location", ...])) is safe because the Headers wrapper is created via headersFromHeaderList(inner.headerList, "immutable") after the push — cache is populated post-mutation.
Fix direction
Two options:
-
Invalidate at each direct mutation. In
26_fetch.js, after each splice/push onrequest.headerList, callinvalidateHeaderListCache(requestObject.headers). TherequestObjectis in scope at:415,419(it's the outer-scope variable), but the redirect handler at:316,337runs inmainFetch/httpRedirectFetchwhererequestObjectmay not be reachable — would need a back-reference fromInnerRequestto its wrapping Headers, or pass the wrapper through. -
Restrict the cache to guards where direct-mutation cannot happen. Cache for
"none","immutable","request-no-cors"(no internal mutations on these). Skip caching for"request"and"response". This narrows the perf win but eliminates the staleness window — the perf-sensitive case in the PR's motivation (Headersconstructed in user code) is"none"guard, so the user-code benefit is preserved.
Option 2 is simpler and matches the original "immutable"-only design intent. Option 1 is more thorough but needs the invalidation plumbing across the fetch redirect path.
Other notes
mergeable_state: dirty— needs a rebase.- @bartlomieju's response to @devsnek ("safe from reentrancy" — only primordials, no user callbacks) is correct; that line of concern is closed.
- No test additions in this PR. A regression test for the Accept-injection staleness would be straightforward —
[...req.headers]before and after fetch on a server that stalls long enough to inspect.
|
Closing as part of an open-PR backlog cleanup. The underlying idea is sound, but the branch needs a rebase and the flagged issues addressed. Closing is not a rejection — reopen or re-push if/when this gets prioritized. Thanks! |
… pushes Covers the staleness scenario from the denoland#32728 review: populate the iteration entries cache on a Request, fetch it (fetch pushes Accept and Accept-Language directly onto the inner request's raw header list), iterate again. The inner request is a clone, so the original must iterate unchanged while the pushes reach the wire. Co-authored-by: Cursor <cursoragent@cursor.com>
Summary
_iterableHeaderscomputed entries for all guard types, not just"immutable"append,set,delete)Root cause
The
_iterableHeadersgetter rebuilds the sorted entries array on every access for non-immutable Headers (guard !== "immutable"), which is the common case for user-createdHeadersobjects. Each rebuild lowercases all header names, builds a dedup map, creates entry arrays, and sorts — all discarded on the next iteration.Benchmark: release build vs system Deno 2.7.5 (8 headers × 1KB values)
for...ofThe remaining ~3x gap vs Map is inherent overhead from the WebIDL iterator protocol.
Closes #20980
Test plan
cargo test -p specs_tests -- fetch(13 tests passed)cargo test -p specs_tests -- headers(1 test passed)🤖 Generated with Claude Code