Skip to content

handlers_dispatcher: make end tag dispatch linear - #340

Open
remusao wants to merge 1 commit into
cloudflare:mainfrom
remusao:endtag-scan-fix
Open

remusao wants to merge 1 commit into
cloudflare:mainfrom
remusao:endtag-scan-fix

Conversation

@remusao

@remusao remusao commented Oct 8, 2026 •

Copy link
Copy Markdown

Every end tag rescans the pending end tag handlers of all still-open elements, so documents with many open elements take quadratic time. With an end tag handler on every element, "<div><b></b>".repeat(100_000) (1.2 MB) takes 1.34s; with this change, 0.05s.

Real pages hit this through unclosed <tr>, <td> or <dd>. On a corpus of slow real-world pages the scan is 46% of extraction CPU; removing it halves the total and makes the worst pages 66-86% faster. Typical pages are unchanged.

Find the handlers to run from the back instead, using the existing active count, so open elements are never visited. Reset the count before handlers run, so it stays correct if one fails or panics. Dispatch order and output are unchanged (identical on 104k pages).

Every end tag rescans the pending end tag handlers of all still-open
elements, so documents with many open elements take quadratic time.
With an end tag handler on every element, "<div><b></b>".repeat(100_000)
(1.2 MB) takes 1.34s; with this change, 0.05s.

Real pages hit this through unclosed <tr>, <td> or <dd>. On a corpus of
slow real-world pages the scan is 46% of extraction CPU; removing it
halves the total and makes the worst pages 66-86% faster. Typical pages
are unchanged.

Find the handlers to run from the back instead, using the existing
active count, so open elements are never visited. Reset the count
before handlers run, so it stays correct if one fails or panics.
Dispatch order and output are unchanged (identical on 104k pages).
@remusao
remusao requested review from a team, Noah-Kennedy, jasnell and orium as code owners October 8, 2026 10:16
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