Evict from the resolver caches without Map.delete - #185
Open
jdalton wants to merge 1 commit into
Open
Conversation
jdalton
force-pushed
the
perf/cache-two-generation
branch
from
September 4, 2026 18:00
d18e3f8 to
c9b0997
Compare
A strict LRU reorders on use and evicts one entry per insertion, both with Map.delete, and V8 keeps a deleted entry in the backing store until the map rehashes — so keys().next(), the way the oldest entry is found, walks the tombstones every earlier eviction left. Profiling 8000 selectors cycling through a 4096-entry cache put Map.set at 28% of total run time. Entries are now written to a young generation. When it fills, it becomes the old generation and the previous old one is dropped whole: no per-insertion delete, no iteration, and eviction is a pointer swap. A hit in the old generation carries the entry back, so anything still in use survives the next swap. Capacity is unchanged, half the limit per generation. get() also stops calling has() first. A cached value is never undefined, so one lookup answers both whether the entry exists and what it holds. Measured with both builds in one process, matching a sweep of distinct selectors against one element: 30 selectors, all hit 3.13us 4.68us 1.50x 2000 selectors 732us 690us 0.94x 3000 selectors 973us 22.72ms 23.34x 8000 selectors 34.86ms 67.60ms 1.94x The loss is a working set that straddles a generation: it no longer fits the young one, so a pass takes old-generation hits and pays to carry them across. At 3000 the comparison inverts, because a selector is not one cache entry — a ':not()' argument takes its own — so 3000 selectors overflow a 4096-entry LRU while the segmented cache degrades instead of thrashing. References: - MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map — the delete semantics this replaces - Reading: https://zod.dev/blog/reducing-memory-footprint — the same measurement discipline applied to a library that caches heavily
jdalton
force-pushed
the
perf/cache-two-generation
branch
from
September 5, 2026 02:48
c9b0997 to
33ee8bd
Compare
Collaborator
Author
This was referenced Sep 5, 2026
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.
A strict LRU reorders on use and evicts one entry per insertion, both with Map delete, and V8 keeps a deleted entry until the map rehashes — so finding the oldest entry walks the tombstones every earlier eviction left, which profiled at 28% of an eviction-heavy run. Two generations replace it: the young one fills, becomes the old one, and the previous old one is dropped whole, so eviction is a pointer swap.
Detail, and how it was checked
A strict LRU reorders on use and evicts one entry per insertion, both with Map.delete, and V8 keeps a deleted entry in the backing store until the map rehashes — so
keys().next(), the way the oldest entry is found, walks the tombstones every earlier eviction left. Profiling 8000 selectors cycling through a 4096-entry cache put Map.set at 28% of total run time.Entries are now written to a young generation. When it fills, it becomes the old generation and the previous old one is dropped whole: no per-insertion delete, no iteration, and eviction is a pointer swap. A hit in the old generation carries the entry back, so anything still in use survives the next swap. Capacity is unchanged, half the limit per generation.
get()also stops callinghas()first. A cached value is never undefined, so one lookup answers both whether the entry exists and what it holds.Measured with both builds in one process, matching a sweep of distinct selectors against one element:
The loss is a working set that straddles a generation: it no longer fits the young one, so a pass takes old-generation hits and pays to carry them across. At 3000 the comparison inverts, because a selector is not one cache entry — a
:not()argument takes its own — so 3000 selectors overflow a 4096-entry LRU while the segmented cache degrades instead of thrashing.Extracted from #167 as a standalone change: one file, applies to master on its own, and checked against the benchmark fixture to confirm every selector still agrees with the native engine.
References: the spec, the browser source, and what each part was reasoned from
This patch applies to master on its own. The sixteen in this series were checked by cherry-picking them onto master one after another, in this order and in reverse, and all sixteen land without a conflict.
Map— the delete semantics this replaces.