From d91519f6ca20d3a2aea9f38cc11c9c6a5b9ce52e Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 8 Sep 2026 08:22:18 +0000 Subject: [PATCH] perf: optimize suboptimal line search in lsp Replace O(N) `chars().nth()` operations with O(1) slice access and `.chars().next_back()` / `.chars().next()` for finding boundary characters using byte indices returned by `.find()`. This ensures the operation is fast and works correctly for non-ASCII characters without causing out-of-bounds panics or logical errors. Co-authored-by: Tcode-Motion <188012755+Tcode-Motion@users.noreply.github.com> --- .jules/bolt.md | 3 +++ tools/lsp/src/lib.rs | 8 ++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/.jules/bolt.md b/.jules/bolt.md index c80a25c7..670131a6 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -8,3 +8,6 @@ ## 2024-05-18 - Removed redundant clone of VM stack during trace logs **Learning:** In `runtime/vm/src/executor.rs`, the debugging instruction trace `self.debugger.trace_instruction` was cloning the entire VM stack using `&self.stack.get_dump()` for every single instruction executed. This caused significant `O(N)` overhead inside the main fetch-decode-execute loop just to format debug output. A new `data_slice()` method was added to `ValueStack` to provide zero-copy slice access (`&[RuntimeValue]`) instead, completely eliminating the allocation overhead. **Action:** Always scrutinize deep clones in logging, tracing, or hot path loops. Use slice references (`&[T]`) instead of `Vec::clone` when the caller only needs read-only access to a collection. +## 2024-06-25 - Suboptimal Line Search in LSP +**Learning:** Using `chars().nth()` with a byte offset (such as one returned by `.find()`) inside a loop over a string creates an O(N) penalty and may result in an incorrect character lookup if multi-byte unicode characters are present. +**Action:** Use string slicing with the byte index to create a subset string slice, and call `.chars().next_back()` or `.chars().next()` on it for an O(1) and UTF-8 safe boundary lookup. diff --git a/tools/lsp/src/lib.rs b/tools/lsp/src/lib.rs index 7ef4fad8..15caf177 100644 --- a/tools/lsp/src/lib.rs +++ b/tools/lsp/src/lib.rs @@ -752,11 +752,11 @@ impl LanguageServer for Backend { while let Some(pos_in_line) = line[start_pos..].find(&word) { let actual_pos = start_pos + pos_in_line; let char_before = if actual_pos > 0 { - line.chars().nth(actual_pos - 1) + line[..actual_pos].chars().next_back() } else { None }; - let char_after = line.chars().nth(actual_pos + word_len); + let char_after = line[actual_pos + word_len..].chars().next(); let is_boundary_before = char_before.map_or(true, |c| !c.is_alphanumeric() && c != '_'); @@ -803,11 +803,11 @@ impl LanguageServer for Backend { while let Some(pos_in_line) = line[start_pos..].find(&word) { let actual_pos = start_pos + pos_in_line; let char_before = if actual_pos > 0 { - line.chars().nth(actual_pos - 1) + line[..actual_pos].chars().next_back() } else { None }; - let char_after = line.chars().nth(actual_pos + word_len); + let char_after = line[actual_pos + word_len..].chars().next(); let is_boundary_before = char_before.map_or(true, |c| !c.is_alphanumeric() && c != '_');