From 8e573b94eb858b321cf9d506b72cf0fa82030ff9 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 27 Oct 2026 01:17:50 +0000 Subject: [PATCH 1/3] =?UTF-8?q?=E2=9A=A1=20Bolt:=20optimize=20line=20start?= =?UTF-8?q?=20lookups=20in=20indent=20and=20goto-line?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Replace O(N) includes() and O(N) filter() in indent.ts with O(log N) binary search and O(M) range slicing. - Eliminate O(N^2) filter + indexOf loop in dedentCommand for single-cursor selections. - Avoid duplicate document AST traversal in goto-line dialog on submission. Co-authored-by: dezren39 <11225574+dezren39@users.noreply.github.com> --- .jules/bolt.md | 5 ++++ pkgs/id/web/src/goto-line.ts | 6 ++-- pkgs/id/web/src/indent.ts | 55 +++++++++++++++++++++++++----------- 3 files changed, 48 insertions(+), 18 deletions(-) diff --git a/.jules/bolt.md b/.jules/bolt.md index 5e8af1501e..c40a624f83 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -7,3 +7,8 @@ **Learning:** `EditorView.coordsAtPos()` queries DOM layout/geometry and triggers browser reflows. Calling it repeatedly per remote cursor in `createCursorDecorations` (for same-line check, line Y-position grouping, and horizontal overlap clustering) results in redundant layout reads during collaborative editor updates. **Action:** Cache `view.coordsAtPos` calls per synchronous decoration calculation pass in a `Map` to reduce DOM layout reads by ~75% while keeping calculations strictly scoped to the current render pass. + +## 2026-10-27 - [Leverage Sorted Line Starts for O(log N) Range Slicing] + +**Learning:** Line start position arrays in editors are strictly monotonically increasing. Using `lineStarts.includes()` or `Array.prototype.filter()` iterates over the entire document's lines (O(N) time and O(N) allocations), and nested calls like `filter` with `indexOf` create O(N^2) bottlenecks. +**Action:** Use binary search to locate line boundaries in O(log N) time and `Array.prototype.slice()` to extract selected line ranges in O(M) time without full array scans. diff --git a/pkgs/id/web/src/goto-line.ts b/pkgs/id/web/src/goto-line.ts index 1c410d5e5e..484e0bede5 100644 --- a/pkgs/id/web/src/goto-line.ts +++ b/pkgs/id/web/src/goto-line.ts @@ -97,10 +97,12 @@ function openGotoLine(view: EditorView): void { // the user can correct it; the cursor does not move. const raw = (dialogInput?.value ?? "").trim(); const line = Number(raw); - const lineCount = activeView ? getLineCount(activeView) : 0; + // ⚡ Bolt optimization: compute line starts once instead of traversing doc twice + const lineStarts = activeView ? getLineStarts(activeView) : []; + const lineCount = Math.max(1, lineStarts.length); const pos = activeView && raw !== "" && Number.isInteger(line) && line >= 1 && line <= lineCount - ? getLineStartPos(activeView, line) + ? lineStarts[line - 1] ?? null : null; if (pos === null || !activeView) { dialogInput?.select(); diff --git a/pkgs/id/web/src/indent.ts b/pkgs/id/web/src/indent.ts index a44fb9eaa0..d464423012 100644 --- a/pkgs/id/web/src/indent.ts +++ b/pkgs/id/web/src/indent.ts @@ -18,18 +18,45 @@ const INDENT = " "; // 2 spaces * (the standard editor rule), unless the selection is empty. */ export function selectedLineStarts(lineStarts: number[], from: number, to: number): number[] { - const lineContaining = (pos: number): number => { - let found = lineStarts[0]; - for (const lineStart of lineStarts) { - if (lineStart > pos) break; - found = lineStart; + if (lineStarts.length === 0) return []; + + // Helper to find index of the line start containing pos via binary search + const lineIndexContaining = (pos: number): number => { + let low = 0; + let high = lineStarts.length - 1; + let ans = 0; + while (low <= high) { + const mid = (low + high) >> 1; + if (lineStarts[mid] <= pos) { + ans = mid; + low = mid + 1; + } else { + high = mid - 1; + } + } + return ans; + }; + + // ⚡ Bolt optimization: lineStarts is sorted in ascending order. + // Binary search for exact match instead of O(N) includes() + const isLineStart = (pos: number): boolean => { + let low = 0; + let high = lineStarts.length - 1; + while (low <= high) { + const mid = (low + high) >> 1; + if (lineStarts[mid] === pos) return true; + if (lineStarts[mid] < pos) low = mid + 1; + else high = mid - 1; } - return found; + return false; }; - const endPos = to > from && lineStarts.includes(to) ? to - 1 : to; - const first = lineContaining(from); - const last = lineContaining(endPos); - return lineStarts.filter((lineStart) => lineStart >= first && lineStart <= last); + + const endPos = to > from && isLineStart(to) ? to - 1 : to; + const firstIdx = lineIndexContaining(from); + const lastIdx = lineIndexContaining(endPos); + + // ⚡ Bolt optimization: Slice contiguous range in O(M) instead of O(N) array filter + return lineStarts.slice(firstIdx, lastIdx + 1); } /** @@ -112,14 +139,10 @@ export const dedentCommand: Command = (state, dispatch) => { let tr = state.tr; let offset = 0; - // For single cursor, only handle current line + // ⚡ Bolt optimization: Avoid O(N^2) filter + indexOf loop for single cursor const linesToDedent = from === to - ? lineStarts.filter( - (ls) => - ls <= from && - (lineStarts.indexOf(ls) === lineStarts.length - 1 || lineStarts[lineStarts.indexOf(ls) + 1] > from), - ) + ? selectedLineStarts(lineStarts, from, to) : selectedLineStarts(lineStarts, from, to); for (const lineStart of linesToDedent) { From 7b4ee0d1f9ea3ac3eee03187647b40bf30b02275 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 27 Oct 2026 01:36:28 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=E2=9A=A1=20Bolt:=20optimize=20line=20start?= =?UTF-8?q?=20lookups=20in=20indent=20and=20goto-line?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Replace O(N) includes() and O(N) filter() in indent.ts with O(log N) binary search and O(M) range slicing. - Eliminate O(N^2) filter + indexOf loop in dedentCommand for single-cursor selections. - Avoid duplicate document AST traversal in goto-line dialog on submission. - Format modified files with Biome formatter. Co-authored-by: dezren39 <11225574+dezren39@users.noreply.github.com> --- pkgs/id/web/src/goto-line.ts | 2 +- pkgs/id/web/src/indent.ts | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/pkgs/id/web/src/goto-line.ts b/pkgs/id/web/src/goto-line.ts index 484e0bede5..7720d840a8 100644 --- a/pkgs/id/web/src/goto-line.ts +++ b/pkgs/id/web/src/goto-line.ts @@ -102,7 +102,7 @@ function openGotoLine(view: EditorView): void { const lineCount = Math.max(1, lineStarts.length); const pos = activeView && raw !== "" && Number.isInteger(line) && line >= 1 && line <= lineCount - ? lineStarts[line - 1] ?? null + ? (lineStarts[line - 1] ?? null) : null; if (pos === null || !activeView) { dialogInput?.select(); diff --git a/pkgs/id/web/src/indent.ts b/pkgs/id/web/src/indent.ts index d464423012..a06d19df1e 100644 --- a/pkgs/id/web/src/indent.ts +++ b/pkgs/id/web/src/indent.ts @@ -141,9 +141,7 @@ export const dedentCommand: Command = (state, dispatch) => { // ⚡ Bolt optimization: Avoid O(N^2) filter + indexOf loop for single cursor const linesToDedent = - from === to - ? selectedLineStarts(lineStarts, from, to) - : selectedLineStarts(lineStarts, from, to); + from === to ? selectedLineStarts(lineStarts, from, to) : selectedLineStarts(lineStarts, from, to); for (const lineStart of linesToDedent) { const adjustedPos = lineStart + offset; From a1cb58c2dca590b2066a359e1cd795108018d5c7 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 27 Oct 2026 01:39:39 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=E2=9A=A1=20Bolt:=20optimize=20line=20start?= =?UTF-8?q?=20lookups=20in=20indent=20and=20goto-line?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Replace O(N) includes() and O(N) filter() in indent.ts with O(log N) binary search and O(M) range slicing. - Eliminate O(N^2) filter + indexOf loop in dedentCommand for single-cursor selections. - Avoid duplicate document AST traversal in goto-line dialog on submission. - Format modified files with Biome formatter. Co-authored-by: dezren39 <11225574+dezren39@users.noreply.github.com>