From 946371a1bbb9efe4799998093e4c84ec50d3c6fa Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 22 Oct 2026 01:26:18 +0000 Subject: [PATCH 1/2] perf(search): optimize search match counting using pre-computed state Replace O(N) manual document traversal using `findNext` with O(1) retrieval of the pre-computed `DecorationSet` via `getMatchHighlights` in `countMatches`. This significantly reduces time complexity when typing in the search panel or traversing large documents, avoiding continuous recalculation. Co-authored-by: dezren39 <11225574+dezren39@users.noreply.github.com> --- .jules/bolt.md | 3 +++ pkgs/id/web/src/search-panel.ts | 11 ++--------- 2 files changed, 5 insertions(+), 9 deletions(-) create mode 100644 .jules/bolt.md diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000000..0c35e0cb5b --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2025-02-18 - ProseMirror Search Match Counting +**Learning:** In ProseMirror, repeatedly calling `findNext` to count search matches traverses the entire document resulting in an O(N) operation per search input change. `prosemirror-search` (v1.1.1+) maintains a pre-computed `DecorationSet` in its state which can be accessed instantly using `getMatchHighlights(view.state)`. +**Action:** Always prefer retrieving pre-computed state from ProseMirror plugins using their getter functions (like `getMatchHighlights`) instead of recalculating state by manually traversing the document or re-running search algorithms. diff --git a/pkgs/id/web/src/search-panel.ts b/pkgs/id/web/src/search-panel.ts index 4313cda00e..87d46e3094 100644 --- a/pkgs/id/web/src/search-panel.ts +++ b/pkgs/id/web/src/search-panel.ts @@ -14,6 +14,7 @@ import { keymap } from "prosemirror-keymap"; import { findNext, findPrev, + getMatchHighlights, getSearchState, replaceAll, replaceNext, @@ -54,15 +55,7 @@ function countMatches(view: EditorView): number { const ss = getSearchState(view.state); if (!ss || !ss.query.valid) return 0; - let count = 0; - let result = ss.query.findNext(view.state, 0); - const seen = new Set(); - while (result && !seen.has(result.from)) { - seen.add(result.from); - count++; - result = ss.query.findNext(view.state, result.to); - } - return count; + return getMatchHighlights(view.state).find().length; } /** Update match count label. */ From e01c981ec7e91d27599a801b449f85335ff69800 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 22 Oct 2026 01:29:52 +0000 Subject: [PATCH 2/2] perf(search): optimize search match counting using pre-computed state Replace O(N) manual document traversal using `findNext` with O(1) retrieval of the pre-computed `DecorationSet` via `getMatchHighlights` in `countMatches`. This significantly reduces time complexity when typing in the search panel or traversing large documents, avoiding continuous recalculation. Co-authored-by: dezren39 <11225574+dezren39@users.noreply.github.com> --- .jules/bolt.md | 1 + 1 file changed, 1 insertion(+) diff --git a/.jules/bolt.md b/.jules/bolt.md index 0c35e0cb5b..83b7f91efb 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -1,3 +1,4 @@ ## 2025-02-18 - ProseMirror Search Match Counting + **Learning:** In ProseMirror, repeatedly calling `findNext` to count search matches traverses the entire document resulting in an O(N) operation per search input change. `prosemirror-search` (v1.1.1+) maintains a pre-computed `DecorationSet` in its state which can be accessed instantly using `getMatchHighlights(view.state)`. **Action:** Always prefer retrieving pre-computed state from ProseMirror plugins using their getter functions (like `getMatchHighlights`) instead of recalculating state by manually traversing the document or re-running search algorithms.