Skip to content

fix(page-classifier): comments rule no longer quadratic in comment-class containers x h1s - #1193

Merged
nc9 merged 2 commits into
mainfrom
fix/classifier-h1-quadratic
Oct 27, 2026
Merged

nc9 merged 2 commits into
mainfrom
fix/classifier-h1-quadratic

Conversation

@nc9

@nc9 nc9 commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

A page of 40,000 <div class="comments"><h1>x</h1></div> took about 8 s of CPU in the classifier (1.7 MB page; 20,000: 1.7 s). The comments rule tested each comment-class container against every h1 on the page. It now marks the ancestors of each h1 once per root; the answers are unchanged, and the cost is linear in the nodes touched.

  • Hostile-input test (40,000 containers, under 1.5 s; 8.4 s on the old code) and a semantics test (a container holding an h1 is not a thread, one without is, an <h1 class=comments-title> does not disqualify itself).
  • predict.ts probed with 200,000 anchors, 50,000 comment ids, a 2M-character text node and deep nesting: all under 0.8 s, no change.

Docs: none. Skill: no change. Refs squirrelscan/repo#3548 (private).

@github-actions

Copy link
Copy Markdown
Contributor

Auto-review did not complete

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

  • Result: success / is_error=false after 2 turns, 0 permission denial(s), $0.08
  • Job log

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

Show partial review

I found no bugs in this PR. I read the diff only and did not run the tests or the benchmark.

  • The new check is equivalent to the old one. An element is marked exactly when it is a proper ancestor of some <h1>, which matches h1 !== el && el.contains(h1).

  • Stopping the upward walk at an already-marked element is sound, because everything above a marked element is also marked. The cost is linear in the nodes touched.

  • The per-root cache is kept, so detached subtrees still find their own <h1>s.

  • (nit) packages/page-classifier/tests/comments-rule.test.ts: the semantics test names the <h1 class="comments-title" id="c"> case but never asserts on it. Adding expect(ids).toContain("c") would lock in that an <h1> does not disqualify itself, if that element is meant to count as a section.

  • (nit) The 1500 ms timing assertion could be flaky on a loaded CI runner. The 8.4 s regression leaves plenty of margin, so this is acceptable as is.

@claude

claude Bot commented Oct 27, 2026

Copy link
Copy Markdown

Reviewed the diff. I found no correctness problems and ran nothing locally.

  • The new check marks only strict ancestors of each <h1>, which matches the old h1 !== el && el.contains(h1). The early stop at an already-marked ancestor is safe because everything above a marked node is also marked.
  • rootFor memoises the root of every node on the walked path, so nested containers walk the chain once. Detached subtrees still get their own root and their own marker set.
  • The semantics test covers the <h1 class=comments-title> case and the shared-ancestor case.

(nit) comments-rule.test.ts: the third test is named "many

s under one container, and a deep chain above them", but it has no deep chain. It is one container with 40,000 headings. Rename it, or add the chain.

(nit) The 2 s timing bounds could flake on a heavily loaded CI runner. The 4x margin over the old 8 s case looks reasonable, but an effect-based assertion would be more robust, as AGENTS.md suggests.

(low) The nested-containers test builds 40,000 levels of nesting. If the parser or a later rule recurses on depth, that could overflow the stack on other code paths. It passes now, so it is fine as it stands.

@nc9
nc9 force-pushed the fix/classifier-h1-quadratic branch from 433d8b8 to c7048d9 Compare October 27, 2026 10:01
@github-actions

Copy link
Copy Markdown
Contributor

Auto-review did not complete

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

  • Result: success / is_error=false after 2 turns, 0 permission denial(s), $0.09
  • Job log

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

Show partial review

I found no correctness bugs in this change, so none of the comments below are tagged high or critical. I read the diff but did not run the tests or check CI.

  • The new logic matches the old behaviour. holders holds every proper ancestor of some <h1>, which is the same as the old h1 !== el && el.contains(h1). An <h1 class="comments-title"> is not its own ancestor, so it is still not disqualified.

  • The early stop in the marking walk, !holders.has(up), is correct. Once an ancestor is marked, everything above it is already marked.

  • rootFor caches the root along the whole path, so a nested chain is walked once. A detached subtree still gets its own root, because querySelectorAll on the root excludes the root itself.

  • The marking pass costs time and memory proportional to the nodes it touches. The extra memory is the rootOf and holders maps, which is acceptable. For a 40,000-deep chain this is iterative, so there is no stack risk.

  • The CHANGELOG entry sits in ### Fixed, but it has no (#123) issue reference at the end, which AGENTS.md asks for. The PR body points to a private issue, so this may be intentional.

  • (nit) comments-rule.test.ts, the "many <h1>s under one container" test: the title says "and a deep chain above them", but the fixture has no chain. Rename it or drop the clause.

  • (nit) The 2 s timing bounds are generous, but they are still wall-clock asserts. If you want to avoid flakes on loaded runners, you could also assert on an effect, as AGENTS.md prefers. The 4x margin is reasonable as it stands.

  • (nit) The semantics test has a stray comment, "(not a comment container)", on the <section> case. Otherwise it covers the two-level, sibling, shared-ancestor and self-heading shapes well.

nc9 added 2 commits October 27, 2026 21:18
…1 once instead of testing every container against every h1

A page of 40,000 comment-class containers each holding an h1 took 8 s of CPU. Cost is now linear in the nodes touched; hostile-input test included.
Docs: none. Skill: no change. Refs repo#3548.

Signed-off-by: Nik Cubrilovic <[email protected]>
…oot once; tests for the nested and shared-ancestor shapes

Review (Sonnet-high): the root lookup per container was still quadratic on a chain of nested comment-class divs (8 s at 40,000).
Docs: none. Skill: no change.

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 force-pushed the fix/classifier-h1-quadratic branch from c7048d9 to 9112040 Compare October 27, 2026 10:18
@claude

claude Bot commented Oct 27, 2026

Copy link
Copy Markdown

Reviewed the diff. I did not run the tests.

The change looks correct. The ancestor-marking pass gives the same answers as the old h1s.some(contains) check:

  • Marking starts at h1.parentElement, so an h1 is never counted as holding itself. That keeps the <h1 class=comments-title> case right.

  • Stopping the walk at an already-marked ancestor is safe, because everything above a marked element is already marked.

  • rootFor caches the root on every element along the path, so a nested chain is walked once. It is iterative, so deep nesting cannot overflow the stack.

  • There is no DOM mutation, and the memo maps are local to each rule instance.

  • (nit) comments-rule.test.ts: the third test is titled "many

    s under one container, and a deep chain above them", but it only has one container and no deep chain. Rename it, or add the chain.

  • (nit) comments-rule.test.ts: the timing bounds (2 s) are generous and could flake on a very loaded runner. The AGENTS.md guidance prefers asserting an effect over elapsed time. They do separate the old 8 s from the new cost, so I would keep them.

  • (nit) CHANGELOG: the bullet ends without an issue reference, as the repo convention asks. The private issue link in the PR body may be the reason, so ignore this if so.

No blocking issues.

@nc9
nc9 merged commit e3c8ccd into main Oct 27, 2026
15 checks passed
@nc9
nc9 deleted the fix/classifier-h1-quadratic branch October 27, 2026 10:36
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