Skip to content

fix: #3211 avoid quadratic search highlighting in large paragraphs - #5856

Merged
Jocs merged 4 commits into
developfrom
fix/3211-large-document-search
Oct 23, 2026
Merged

Jocs merged 4 commits into
developfrom
fix/3211-large-document-search

Conversation

@Jocs

@Jocs Jocs commented Oct 23, 2026 •

Copy link
Copy Markdown
Member

Closes #3211

Summary

Searching a large paragraph with many matches checks every highlight against every inline token. A 1500-line paragraph with one match per line performs 22.5 million range reads, so searching and moving between matches can stall the renderer.

Move search annotation into applySearchHighlights, separating token-tree traversal, range intersections and binary lookup from lexing. Use binary lookup to find the first candidate highlight for each token, then stop after the token's range. Search supplies ranges with nondecreasing starts and ends, including after grapheme alignment. Document that input contract and test it at the match producer so rendering can use binary lookup directly. Add regression coverage for the 1500-line search/navigation/replacement/clear flow, a deterministic range-access budget, and nested tokens, ordered overlapping ranges and zero-width matches.

Type of change

  • Bug fix (non-breaking, fixes an issue)
  • New feature (non-breaking, adds functionality)
  • Breaking change (causes existing functionality to change)
  • Documentation update

Test plan

  • New tests added: 16 regression cases; the performance budget fails on the original implementation (22,500,000 reads versus a limit of 300,000) and passes with this change.
  • pnpm -C packages/muya test — 2850 tests in 302 files passed.
  • pnpm -C packages/muya test:spec — 1347 conformance tests passed.
  • pnpm -C packages/muya check-circular — passed.
  • pnpm run lint — passed with existing warnings.
  • pnpm run typecheck — passed.
  • pnpm -C packages/muya lint:types — passed.
  • Package-local ESLint on changed files — passed with existing complexity warnings.
  • Manually tested on: macOS / Windows / Linux desktop UI. Validation here uses automated happy-dom tests on macOS ARM64.

Notes for reviewers

Measured against develop commit 55d19dc0 on the same macOS ARM64 machine using Node 22.22.3 and happy-dom. The fixture repeats a line containing bold needle and inline code, with one search hit per line in a single paragraph. The versions alternate execution order; each measurement uses two warmups and the median of seven samples.

Lines Operation Before After Speedup
1500 Tokenization with search highlights 111.4 ms 11.1 ms 10.1x
3000 Tokenization with search highlights 418.5 ms 19.4 ms 21.6x
6000 Tokenization with search highlights 1630.2 ms 40.6 ms 40.2x
1500 Full search flow 345.2 ms 241.2 ms 1.43x
1500 Next match 571.7 ms 494.5 ms 1.16x

The full search and next-match measurements include simulated DOM rendering. They are comparative benchmark results, not Electron UI latency measurements. This change addresses the confirmed search-highlighting bottleneck in #3211; the original report's general editing slowdown across platforms has not been fully verified.


By submitting this pull request, I confirm that my contribution is made under the terms of the MIT license.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No unresolved blocking issues were identified, and all approval-readiness assessments support approval.

No open issues 🎉

What changed in this PR

This pull request fixes quadratic search highlighting for large paragraphs by applying highlights after tokenization and using efficient range lookup.

Changes:

  • Adds ordered-range binary lookup with early termination.
  • Preserves fallback handling for unordered ranges.
  • Adds performance and correctness regression coverage for search flows and range edge cases.
File Description
packages/​muya/​src/​inlineRenderer/​searchHighlights.ts Implements optimized search-range intersection and annotation.
packages/​muya/​src/​inlineRenderer/​lexer.ts Applies highlights through the optimized post-tokenization path.
packages/​muya/​src/​inlineRenderer/​__tests__/​searchHighlights3211.spec.ts Adds performance-budget and correctness regression tests.

Review effort: Lite

@Jocs
Jocs merged commit bc83455 into develop Oct 23, 2026
8 checks passed
@Jocs
Jocs deleted the fix/3211-large-document-search branch October 23, 2026 01:14
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.

Reaction become slow when the markdown file is large.

2 participants