Repository navigation
Conversation
`for (const [k, v] of headers)`, `Object.fromEntries(headers)`, and the implicit iteration that runs when an HTTP server sends response headers all flow through the `[_iterableHeaders]` getter. Today that getter short-circuits to a cached array only when `_guard === "immutable"`. For the dominant cases (request headers, response headers, default "none"-guarded Headers) every call walks the full list: byteLowerCase every name, dedupe via a hash map, and `Array.prototype.sort` the resulting entries with a JS comparator. V8 --prof on a 5M-iter hot loop iterating a 7-header Headers attributes the cost like this: 13.3% ArrayTimSort 8.5% the iteration code itself (20_headers.js:270) 7.4% KeyedStoreIC_Megamorphic 5.5% StringToLowerCaseIntl 3.5% KeyedLoadIC_Megamorphic 3.3% StringLessThan 2.6% ArrayPrototypeSort i.e. ~30% of the iteration runtime is in work that's identical from one call to the next on an unmutated Headers. Drop the immutable-only restriction. Mutator methods (append, set, delete, plus the appendHeader helper used by fillHeaders) clear the cache explicitly. As a safety net for callers that mutate via `headerListFromHeaders` (push / splice / empty-and-refill -- the Request constructor's refill block and `initializeAResponse` both do this), the cache also records the header list's length when it's built and the getter falls through when the recorded length no longer matches. Equal-length in-place mutations of `_headerList` would silently keep the stale cache; no current caller does that.
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The
[_iterableHeaders]getter onHeadersrebuilds a sorted +lowercased entries array on every call -- except when
_guard === "immutable". Every other guard (the dominant cases:"request","response","none") redoes the full walk. This PRcaches for all guards and invalidates on mutation.
Where the cost lives
V8
--profon a 5M-iter hot loop offor (const [k, v] of headers)on a 7-header
Headersobject attributes the cost like this:~30% of total runtime is in work that's identical from one call to the
next on an unmutated
Headers: the byteLowerCase per name, theseenHeadersdedupe map, and theArray.prototype.sortover thededuped entries.
How
Drop the immutable-only restriction from the
[_iterableHeaders]getter. The cache field already exists; just stop gating it.
Mutator methods (
append,set,delete, plus theappendHeaderhelper that
fillHeaderscalls) clear the cache explicitly. As asafety net for callers that mutate via
headerListFromHeaders(theRequestconstructor's empty-and-refill block,initializeAResponse),the cache also records the header list's length at build time. On the
next read, the getter checks that the recorded length still matches
the current
_headerList.lengthand falls through to a rebuildotherwise. Equal-length in-place mutations of
_headerList(e.g.list[i][0] = newName) would silently keep the stale cache; no currentcaller does that.
Benchmarks
Same-host release builds (
cargo build --bin deno --release), 5 runseach at 1M iterations, reporting median (range). VPS jitter ~10 -- 15%.
for (const [k, v] of headers)Object.fromEntries(headers)for (const k of headers.keys())The
for-ofshape sees the biggest win because the getter is its onlywork-per-call after the cache hit;
Object.fromEntriesallocates aresult object on top so the relative speedup is smaller. The
keys()shape is essentially a degenerate
for-ofover the same cached array.Bench:
bench_headers_iter_for_of,bench_headers_iter_object_from_entries,bench_headers_iter_keysinext/fetch/benches/headers.rs.What changed
ext/fetch/20_headers.js[_iterableHeaders]getter: cache check is now guard-independent;falls through to rebuild if the cached entries are missing OR if
_headerList.lengthdiffers from the cached length.appendHeader/set/delete: clear_iterableHeadersCacheon mutation.
_iterableHeadersCacheLento store the length.Test plan
cargo check -p deno_snapshots-- snapshot loads, runtime initializes.cargo build --bin deno --releaseon both main and this branch.cargo check -p deno_fetch --benches-- bench compiles.dprint check-- clean.test unit::headers, WPTfetch/api/headers/headers-*(covers iteration + mutationordering).
bench release linux-x86_64records the after numbers.