Skip to content

perf(ext/fetch): cache iteration entries for all Headers guards - #34284

Closed
crowlbot wants to merge 2 commits into
denoland:mainfrom
crowlbot:perf/fetch-headers-iter-cache
Closed

crowlbot wants to merge 2 commits into
denoland:mainfrom
crowlbot:perf/fetch-headers-iter-cache

Conversation

@crowlbot

Copy link
Copy Markdown
Contributor

Summary

The [_iterableHeaders] getter on Headers rebuilds 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 PR
caches for all guards and invalidates on mutation.

Where the cost lives

V8 --prof on a 5M-iter hot loop of for (const [k, v] of headers)
on a 7-header Headers object attributes the cost like this:

   26.2%  /target/release/deno
   13.3%  ArrayTimSort
   12.0%   bench loop
    8.5%  20_headers.js:270  (sort comparator)
    7.4%  KeyedStoreIC_Megamorphic
    5.5%  StringToLowerCaseIntl
    3.6%  /lib/libc.so.6
    3.5%  KeyedLoadIC_Megamorphic
    3.3%  StringLessThan
    3.2%  Store_FastObjectElements
    3.0%  Load_FastObjectElements
    2.6%  ArrayPrototypeSort

~30% of total runtime is in work that's identical from one call to the
next on an unmutated Headers: the byteLowerCase per name, the
seenHeaders dedupe map, and the Array.prototype.sort over the
deduped 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 the appendHeader
helper that fillHeaders calls) clear the cache explicitly. As a
safety net for callers that mutate via headerListFromHeaders (the
Request constructor'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.length and falls through to a rebuild
otherwise. Equal-length in-place mutations of _headerList (e.g.
list[i][0] = newName) would silently keep the stale cache; no current
caller does that.

Benchmarks

Same-host release builds (cargo build --bin deno --release), 5 runs
each at 1M iterations, reporting median (range). VPS jitter ~10 -- 15%.

op (7 headers) main (ns/op) this PR (ns/op) speedup
for (const [k, v] of headers) 16 835 (16 356 -- 17 139) 1 322 (1 306 -- 1 334) ~12.7x
Object.fromEntries(headers) 18 425 (17 502 -- 19 021) 2 855 (2 751 -- 2 935) ~6.5x
for (const k of headers.keys()) 16 356 (16 211 -- 16 495) 1 198 (1 164 -- 1 214) ~13.7x

The for-of shape sees the biggest win because the getter is its only
work-per-call after the cache hit; Object.fromEntries allocates a
result object on top so the relative speedup is smaller. The keys()
shape is essentially a degenerate for-of over the same cached array.

Bench: bench_headers_iter_for_of, bench_headers_iter_object_from_entries,
bench_headers_iter_keys in ext/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.length differs from the cached length.
    • appendHeader / set / delete: clear _iterableHeadersCache
      on mutation.
    • New private symbol _iterableHeadersCacheLen to store the length.

Test plan

  • cargo check -p deno_snapshots -- snapshot loads, runtime initializes.
  • cargo build --bin deno --release on both main and this branch.
  • cargo check -p deno_fetch --benches -- bench compiles.
  • dprint check -- clean.
  • CI: test unit::headers, WPT
    fetch/api/headers/headers-* (covers iteration + mutation
    ordering).
  • CI: bench release linux-x86_64 records the after numbers.

claude added 2 commits May 21, 2026 08:59
`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.
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants