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..7720d840a8 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..a06d19df1e 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,15 +139,9 @@ 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); + from === to ? selectedLineStarts(lineStarts, from, to) : selectedLineStarts(lineStarts, from, to); for (const lineStart of linesToDedent) { const adjustedPos = lineStart + offset;