Repository navigation
selectors_vm: make stray end tags and descendant matching linear in depth (cherry-pick of upstream 87f7987) - #2
Merged
Conversation
…epth
The selector-VM open-element stack had two per-token scans that go
quadratic (or worse) on pathological nesting:
* Stack::pop_up_to did a full rposition() over the open stack for
every end tag. A stray end tag that matches nothing still walked
the whole stack, so N unclosed opens plus N stray closes was O(N^2):
"<a></b>".repeat(100_000) (~700 KB) burned ~25s of CPU.
* try_exec_hereditary_jumps_without_attrs walked every ancestor and
re-executed its hereditary_jumps for every start tag. For a selector
like "div div" over N nested <div>s every item carries the same
address range, so the walk is O(depth) per tag and O(N^2) overall;
k-arm chains ("div div div") compound to O(N^k).
Both paths are reachable from HtmlRewriter with any element-handler
registration, so untrusted HTML can pin a CPU core via sub-MB input.
Memory stays linear so max_allowed_memory_usage does not bound it.
Changes:
* Stack now keeps open_name_counts: HashMap<LocalName, usize> so
pop_up_to rejects a name not on the stack in O(1). When the name is
present the rposition scan length equals the subsequent drain
length, so total scan work over a document is bounded by total
pushes.
* Stack now keeps active_hereditary_jumps: Vec<(AddressRange, usize)>,
the distinct hereditary-jump ranges currently contributed by any
open item, each paired with the shallowest depth that introduced it.
Descendant matching iterates this flat list (bounded by program
size, not depth) and pop_up_to prunes it by depth. The per-item
has_ancestor_with_hereditary_jumps flag and the
HereditaryJumpPtr.stack_offset resume coordinate are removed.
* LocalName gets a manual Hash impl that ASCII-lowercases the Bytes
arm so it agrees with the existing case-insensitive PartialEq.
Without this, keying a HashMap on LocalName is unsound for any name
that falls off LocalNameHash (custom elements, >12 chars, chars
outside a-zA-Z1-6). This also fixes the same latent contract
violation in TypedChildCounterMap.
Re-executing the same address range against the same start tag was
already a no-op after the matched-id set union in add_execution_branch,
so dedup-once execution is observationally equivalent.
This was referenced Sep 6, 2026
This was referenced Sep 8, 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.
Problem
"div div div div"over N nested<div>elements runs in O(N^4). A 6.6 KB document (depth 600) takes about two minutes in Bun. A stray end tag scans the whole open-element stack, so"<a></b>"x 100k (700 KB) runs in O(N^2) and takes about 15 s.Fix
bun. The conflicts were mechanical: this branch still passesmatch_handlerthrough the VM andStack::newtakesOption<DefaultHashBuilder>. The upstream logic is unchanged.cargo test --lib(172 pass),cargo test --features integration_test(3 pass),cargo clippy --all-targets(no new warnings). In Bun, the depth 600 case drops from about 2 minutes to about 50 ms.Please merge without squash, or tell me the new sha. Bun pins this branch by commit in
scripts/build/deps/lolhtml.ts.