Perf: propagate outermost/innermost fix to shared dom-utils helpers#154
Merged
Conversation
PR #151 fixed the O(C²) outermost-match dedupe inside selector-hide-rule but left the shared `filterToOutermost` / `filterToInnermost` helpers in lib/dom-utils.ts on the old `candidates.some(other.contains(element))` shape. Those helpers are consumed by hidden-text-strip, prompt-injection-redact, and disguised-ad-flag — so the same fix propagates the perf win across all four call sites. lib/dom-utils.ts: - filterToOutermost: O(C·D). Build a Set of candidate elements once, walk each candidate's parent chain checking Set membership. - filterToInnermost: computed as the dual — for each candidate walk up and mark any candidate-ancestors encountered as "has descendant." Anything not marked is innermost. Also O(C·D). - Drop the now-unused `hasAncestorIn` / `hasDescendantIn` internal helpers — their callers inline the parent walk directly. lib/selector-hide-rule.ts: - Delete the inline `hasCandidateAncestor` from PR #151. Pre-filter candidates via `filterToOutermost(candidates)`, then skip non-outermost matches at the top of the placeholder-skip loop. - Single source of truth: the property tests in lib/__tests__/dom-utils.property.test.ts now cover the outermost logic everywhere it's used. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
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.
Follow-up to #151 (Tier 1 perf). That PR fixed the O(C²) outermost-match dedupe inside
selector-hide-rule.tsbut left the sharedfilterToOutermost/filterToInnermosthelpers inlib/dom-utils.tson the oldcandidates.some(other.contains(element))shape. Those helpers are consumed byhidden-text-strip,prompt-injection-redact, anddisguised-ad-flag— so the same fix propagates the perf win across all four call sites.Summary
lib/dom-utils.tsfilterToOutermost: O(C·D). Build a Set of candidate elements once, walk each candidate's parent chain checking Set membership.filterToInnermost: computed as the dual — for each candidate walk up and mark any candidate-ancestors encountered as "has descendant." Anything not marked is innermost. Also O(C·D).hasAncestorIn/hasDescendantIninternal helpers — the callers inline the parent walk directly.lib/selector-hide-rule.tshasCandidateAncestorfrom Perf: trailing-only throttle, burst flush, O(C^2) outermost-match fix (#150 Tier 1) #151. Pre-filter candidates via the sharedfilterToOutermost, then skip non-outermost matches at the top of the placeholder-skip loop.lib/__tests__/dom-utils.property.test.tsnow cover the outermost / innermost logic everywhere it's used.Correctness
The existing property tests in
dom-utils.property.test.tsexhaustively check the contracts both helpers must satisfy (subset of input, no nested pairs, only-drops-when-related-candidate-exists, idempotence, plus the cross-property intersection check) over random tree shapes and random candidate subsets. Both rewritten helpers pass them unchanged — the algorithmic substitution preserves output exactly.selector-hide-rule.property.test.tsalso covers the rule'sscan()integration with the new shared helper.Test plan
bun run test→ 1190 passed (unchanged from main — pure refactor)bun run typecheckbun run check(biome + eslint)bun run knipbun run test:coverage— held (statements 85.45%, line/branch/function within ratchet)🤖 Generated with Claude Code