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 != '_');