Repository navigation
fix(parser): read anchor text up to a nested anchor, linear parse on nested links #1844 - #1847
Merged
Merged
Conversation
…nested links #1844 parsePage was quadratic on directly nested anchors: the parse keeps <a> inside <a>, and anchorTextResult read every descendant anchor's text into each ancestor, so 20,000 nested links stored about 1 GB of link text and took 45 s. A browser never nests anchors (the parser closes the open one when the next starts), so anchor text now stops at a nested anchor. Each text node is read once per page: 0.19 s at 20,000. Signed-off-by: Nik Cubrilovic <[email protected]>
nc9
force-pushed
the
boat/nested-anchors-1844
branch
from
November 14, 2026 07:19
37330a3 to
428373a
Compare
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.
Closes #1844
Cause
The quadratic step is link extraction, in the anchor text read (
anchorTextResult,packages/parser/src/extractors/anchor-text.ts). It is not the DOM build or an ancestor walk: those were made linear in repo#3356. linkedom/htmlparser2 keeps<a>inside<a>, and each anchor's text walk descended into every nested anchor, so anchor i read the text of all N-i anchors below it. A CPU profile (bun --cpu-prof, N=10,000) put 10.98 s of the 11.17 s parse inanchorTextResult, called fromextractLinks. The output was quadratic too: N=5,000 (128 KB of HTML) stored 62 MB of link text, about 1 GB at N=20,000, so no linear algorithm could give identical output.Fix
Anchor text now stops at a nested
<a>, which matches the HTML spec: a browser never builds an anchor inside an anchor, because the parser's adoption agency algorithm closes the open one when the next starts. The inner link's text belongs to the inner link alone. A skipped nested anchor separates the outer text on either side of it, like a block, so the words are not fused. Every text node is now read once per page, so the parse is linear in input size (AC option 1, with no budget needed and no content dropped: every link is still found with its own text). Both link extractors and thea11y/link-textDOM re-read share this function, so they stay consistent. Pages without nested anchors are unchanged.Before / after (parsePage,
<a href=/pI>lInested N deep; AMD EPYC 9254, 48 cores, bun 1.4.4)Acceptance criteria
*golden*files), 214 rules tests pass (anchor/link-text/integrity-budget-784 files), and there are no snapshot changes (git statusshows only the source file, the new test and the CHANGELOG).packages/parser/tests/nested-anchors-parse-time-1844.test.ts: parses 20,000 nested anchors with a 5 s bound (25x headroom), checks all 20,000 links each carry their own text (both extractors), and pins the semantics on small shapes. 7 tests pass.Gates
Root
bun run format:check,bun run lintandbun run typecheckall exit 0.scripts/check-released-changelog.sh origin/mainpasses.Notes
## [Unreleased]heading. This PR restores both (text from the previous revision) and adds a Fixed line.squirrelscan/skills): no change needed.