Skip to content

selectors_vm: keep the open-name counts only while the stack is deep - #11

Open
robobun wants to merge 1 commit into
robobun/31756292/not-de-morganfrom
robobun/31756292/lazy-open-name-counts
Open

robobun wants to merge 1 commit into
robobun/31756292/not-de-morganfrom
robobun/31756292/lazy-open-name-counts

Conversation

@robobun

@robobun robobun commented Sep 22, 2026

Copy link
Copy Markdown
Collaborator

Stacked on #9. The base of this PR is that branch, so the diff is one commit.

Problem

  • 87f7987 (in v3.0.1) made stray end tags and descendant matching linear in the nesting depth. Its open_name_counts 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 no-op element handler, bun head e1a306d against 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%.
  • The other half of that commit, active_hereditary_jumps, costs nothing: with only the map removed, the numbers are at or below the tree without 87f7987.

Fix

  • The counts exist only while the stack is deep. push_item builds them from the items when the stack gets deeper than 64. pop_up_to drops them when it gets shallower than 32. The gap keeps a document from having them rebuilt at every tag.
  • A shallow stack uses the rposition scan 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" is e1a306d with that commit reverted.

1 MiB ordinary page e1a306d no 87f7987 this branch
* 14,900 13,430 13,600
div p 8,030 6,640 6,840
nomatch-tag 6,320 5,340 5,170
a[href] 7,890 6,590 6,720
pathological input e1a306d no 87f7987 this branch
"<a></b>".repeat(100_000) 8,570 (389,000 for 20,000) 9,670
"<a></b>".repeat(200_000) 18,200 22,000
div div over 3,000 nested <div> 840 52,900 880
div div over 6,000 nested <div> 1,680 1,650
stack moves 31 to 65 to 31 items, 1M tags 49,000 54,700
stack moves 20 to 54 to 20 items, 1M tags 48,700 40,700
1M stray end tags over 50 open elements 20,700 93,600 86,500

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 Settings with one element handler, then HtmlRewriter::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-targets reports nothing in stack.rs.

cloudflare/lol-html has the same commit since v3.0.1, so the same change applies there.

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.
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.

1 participant