Repository navigation
Conversation
87f7987 gave `Stack` a map of open-item counts per name, so that `pop_up_to` can refuse a stray end tag without a scan of the stack. The map is updated for every element: a hash and an insert on the push, a lookup on the end tag, a hash and a remove on the pop. On an ordinary page that costs more than the scans it saves. A 1 MiB page of short rows with one element handler, against the same tree without 87f7987: `*` +11%, `div p` +20%, a selector that never matches +20%, `a[href]` +20%. The other half of that commit, `active_hereditary_jumps`, costs nothing there. A scan is only expensive on a deep stack, so the counts now exist only then. `push_item` builds them from the items when the stack gets deeper than 64, and `pop_up_to` drops them when it gets shallower than 32. The gap keeps a document from having them rebuilt at every tag. The same page is now within 3% of the tree without 87f7987 for all four selectors. The inputs that 87f7987 made linear stay linear: `"<a></b>".repeat(n)` takes 9.7 ms for n = 100,000 and 22 ms for 200,000 (the tree without 87f7987 takes 389 ms for 20,000), and `div div` over 3,000 and 6,000 nested `<div>` takes 0.9 ms and 1.6 ms. A stack that moves between 31 and 65 items for a million tags takes 55 ms (49 ms before). A million stray end tags over 50 open elements take 87 ms, as they did before 87f7987 (94 ms). With the counts always on they took 21 ms.
robobun
added a commit
to oven-sh/bun
that referenced
this pull request
Sep 22, 2026
…ep stack lol-html 87f7987 (v3.0.1) updates a map of open-element names for every element. On an ordinary 1 MiB page that is +11% to +20% in lol-html alone. oven-sh/lol-html#11 keeps the map only while the open stack is deeper than 64, so ordinary pages pay nothing and the pathological inputs stay linear.
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.
Stacked on #9. The base of this PR is that branch, so the diff is one commit.
Problem
open_name_countsmap is updated for every element: a hash and an insert on the push, a lookup on the end tag, a hash and a remove on the pop.bunheade1a306dagainst the same tree with 87f7987 reverted:*+11%,div p+20%, a selector that never matches +20%,a[href]+20%. In Bun the same commit measured +6% to +19%.active_hereditary_jumps, costs nothing: with only the map removed, the numbers are at or below the tree without 87f7987.Fix
push_itembuilds them from the items when the stack gets deeper than 64.pop_up_todrops them when it gets shallower than 32. The gap keeps a document from having them rebuilt at every tag.rpositionscan that was there before 87f7987. The scan is at most 64 items, and a well-formed end tag finds its element at the top.Numbers
Release build,
lto = "fat", min of 90 runs, microseconds. "no 87f7987" ise1a306dwith that commit reverted.e1a306d*div pnomatch-taga[href]e1a306d"<a></b>".repeat(100_000)"<a></b>".repeat(200_000)div divover 3,000 nested<div>div divover 6,000 nested<div>The last row is the cost of the change: a stray end tag on a shallow stack is a scan again (at most 64 items), as it was before 87f7987. It stays linear in the input.
The ordinary-page bench parses the selector once. Each run builds
Settingswith one element handler, thenHtmlRewriter::new,write(page),end(). The page repeats<div class="row"><h2>Title</h2><p>Some text with a <a href="/x">link</a> and <b>bold</b> words.</p><ul><li>one</li><li>two <span>x</span></li></ul><img src="a.png"></div>.Tests
cargo test --features=_integration_test: 213 lib (1 new:open_name_counts_exist_only_while_the_stack_is_deep), 3 integration, 38 and 2 doc tests.cargo clippy --all-targetsreports nothing instack.rs.cloudflare/lol-html has the same commit since v3.0.1, so the same change applies there.