Skip to content

perf(parser): link chrome and position walks are memoized per document, not per link - #1135

Closed
nc9 wants to merge 3 commits into
mainfrom
perf/3356-parse-depth
Closed

nc9 wants to merge 3 commits into
mainfrom
perf/3356-parse-depth

Conversation

@nc9

@nc9 nc9 commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

parsePage was quadratic in nesting depth (squirrelscan/repo#3356). It runs on every audited page, in the CLI and in the cloud.

The step that grew faster than linear

I timed each step of parsePage on 5,000 to 20,000-deep pages. The HTML parse itself stays near linear: 38 ms at 20,000 levels. The cost is in the link extraction.

  • extractLinks (html.ts) asks every anchor whether it sits in sitewide chrome (isInSiteChrome, rules(links): pages with zero CONTEXTUAL inbound links pass orphan-pages #109). It answers by walking the anchor's ancestors up to the root.
  • A page of nested links therefore walks the tree once per link. At 20,000 levels, links took 34 s of a 35 s parse.
  • The crawler's own extractor (extractors/links.ts) makes that walk twice per link: once for the chrome flag, and once for detectLinkPosition.

Changes

  • createSiteChromeTest() (extractors/chrome.ts) and createLinkPositionTest() (extractors/links.ts) memoize the two walks for one document.
    • Each anchor walks up only until it reaches an element an earlier anchor already answered for, and records the answer on every element it passed.
    • Each extractor makes one per call.
  • isInSiteChrome keeps its single-element form.
  • detectLinkPosition's precedence moved unchanged into ownLinkPosition, which is what one element says about itself.

Results

page nesting parsePage before after extractLinks before after
role="text" spans 5,000 754 ms 39 ms 2,378 ms 5 ms
role="text" spans 20,000 12,491 ms 250 ms 64,404 ms 44 ms
aria-hidden divs 20,000 13,391 ms 305 ms 111,977 ms 108 ms
blockquotes 20,000 30,016 ms 747 ms 65,146 ms 96 ms

Both sets of numbers were measured at a load average around 12. Method: benchmarks/2026-10-parse-depth.md, with the harness in benchmarks/harness/parse-depth.ts.

Tests

packages/parser/tests/parse-depth-3356.test.ts:

  • Parity. On 600 seeded random pages, every anchor from both extractors gets the chrome flag and the position that the single walks gave. The pages mix landmarks, ARIA roles, class and id names, svg, and links at every level. The old position walk is kept as the oracle in tests/fixtures/reference-3356/.
  • Time budgets. For each of 3 shapes, 20,000 nested levels parse in under 1 s (best of 3). The crawler's extractLinks also takes 20,000 nested links in under 1 s. At origin/main the first budget fails: 18 s.

The existing parser suites pass: link-chrome, attribute-case, contact-links, dom-text, headings, noscript-resources, visible-meta, soft404, hreflang, fonts and schema-graph.

Docs and skill

The CHANGELOG has an entry. No docs or skill change is needed: output is unchanged.

Gates

  • tsgo on packages/parser
  • tests run by path
  • scripts/test-suites.ts --check
  • detect-secrets on the changed files

@nc9
nc9 marked this pull request as ready for review October 26, 2026 02:44
@claude

claude Bot commented Oct 26, 2026

Copy link
Copy Markdown

Reviewed the diff. I didn't run the tests.

The memoization looks correct. Both the chrome flag and the link position are "first self-answer on the path to the root", so caching the answer on every element passed gives the same result as the old walks. The 600-page seeded parity test against the kept oracle covers that well.

  • (medium) packages/parser/tests/parse-depth-3356.test.ts: the 1 s wall-clock budget has little headroom. By the PR's own numbers, the blockquote case takes 747 ms at a load average around 12. A shared CI runner could push it past 1 s and make the test flaky. Consider a looser budget, such as 3 to 5 s, which still sits far below the 18 s regression. Alternatively, assert the effect, for example that the number of ancestor visits is linear.
  • (low) packages/parser/src/extractors/links.ts: createLinkPositionTest is a local helper that is also named detectLinkPosition at the call site. The old exported or standalone name is gone. If anything else imported detectLinkPosition, it would break. It was a non-exported function, so this is probably fine. Please confirm with a grep.
  • (nit) The chrome walk and the position walk duplicate the memoize-up-the-ancestors loop. A small shared memoizeAncestorWalk(own) helper would remove that duplication.
  • (nit) ownLinkPosition calls getAttribute('role') and .toLowerCase() on every element visited. That is fine now that each element is visited once.

No security concerns. The memo maps are per call and keyed by element, so nothing site-controlled is used as a key.

…t, not per link

Profiling parsePage on a 20,000-deep page named the step: both link
extractors walked every anchor's ancestors to the root, for its sitewide
chrome flag (and, in extractors/links.ts, its position). A page of nested
links walked the tree once per link: 20,000 levels took 12.5 s to parse, and
the crawler's extractLinks 64 s. Each walk now stops at the first element an
earlier anchor answered for. Every anchor gets the answer it got before.

squirrelscan/repo#3356

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 force-pushed the perf/3356-parse-depth branch from a27fd75 to f12e92d Compare October 26, 2026 06:11
@github-actions

Copy link
Copy Markdown
Contributor

Auto-review did not complete

The claude-review job failed before posting a review. This PR has not been auto-reviewed. Run a review manually (/review) before merging.

  • Result: success / is_error=true after 1 turns, 0 permission denial(s), $0.00
  • Job log

Partial findings recovered from the session (incomplete, unverified, and NOT a merge signal):

Show partial review

You've hit your session limit · resets 7:40am (UTC)

… page is never kept alive

CI's page-rule-residency-2343 read 1.82 MB for the bounded arm on f12e92d (budget 1.5 MB, about one 384 KB page's DOM). It passes locally on macOS and on Linux arm64 at 0.07-0.09 MB, and main and this PR's earlier head passed CI, but a memo Map keyed by elements is exactly what a stale conservative stack slot can pin, with the page's whole DOM behind it. Both memos are WeakMaps now. A test keeps the memos alive past their page and asserts the page is collected; with a Map it fails.

Signed-off-by: Nik Cubrilovic <[email protected]>
@github-actions

Copy link
Copy Markdown
Contributor

Auto-review did not complete

The claude-review job failed before posting a review. This PR has not been auto-reviewed. Run a review manually (/review) before merging.

  • Result: success / is_error=true after 1 turns, 0 permission denial(s), $0.00
  • Job log

Partial findings recovered from the session (incomplete, unverified, and NOT a merge signal):

Show partial review

You've hit your session limit · resets 7:40am (UTC)

…passed

A stale slot from the latest call can pin its own page in any test, the same conservative scan the test guards against; a memo holding its keys strongly keeps every page, so asserting the first two of three is as sharp and cannot flake on the last.

Signed-off-by: Nik Cubrilovic <[email protected]>
@github-actions

Copy link
Copy Markdown
Contributor

Auto-review did not complete

The claude-review job failed before posting a review. This PR has not been auto-reviewed. Run a review manually (/review) before merging.

  • Result: success / is_error=true after 1 turns, 0 permission denial(s), $0.00
  • Job log

Partial findings recovered from the session (incomplete, unverified, and NOT a merge signal):

Show partial review

You've hit your session limit · resets 7:40am (UTC)

@nc9 nc9 mentioned this pull request Oct 26, 2026
@nc9

nc9 commented Oct 26, 2026

Copy link
Copy Markdown
Contributor Author

Landed in the v0.0.134 batch (#1149), main commits 9ab8896..9469f57.

@nc9 nc9 closed this Oct 26, 2026
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