Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@
**Learning:** In `split_segments`, constructing `Segment` previously performed `text.to_owned()` for every split segment. This caused unnecessary memory allocation, as the parsed segment string could just borrow from the original input `&str`.

**Action:** Update parsing intermediate structs (like `Segment`) to carry string slices (`&'a str`) representing chunks of the input string rather than owning `String`s when they are only used briefly to route segments to transformation parsers.
## 2024-08-14 - [Rust String Parsing - Whitespace Semantics Regression]
**Learning:** When optimizing whitespace scanning in Rust parsing loops by replacing `.char_indices()` with byte-level ASCII checks (e.g., `as_bytes().iter().position(|b| b.is_ascii_whitespace())`), it can introduce a subtle functional regression. Rust's `char::is_whitespace()` matches all Unicode whitespace characters (like non-breaking spaces), whereas `is_ascii_whitespace()` only matches standard ASCII whitespace.
**Action:** When exact Unicode semantics must be preserved while optimizing, use `.find()` (e.g., `text.find(char::is_whitespace)`) instead of dropping down to byte-level operations. This leverages internal optimizations while preserving the exact semantic meaning of the original code.
27 changes: 13 additions & 14 deletions compiler/rockql-parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,16 @@ fn split_segments(source: &str) -> Vec<Segment<'_>> {
for (line_index, line) in source.lines().enumerate() {
let mut start = 0;

for (byte_index, character) in line.char_indices() {
if character == '|' {
push_segment(
&mut segments,
&line[start..byte_index],
line_index + 1,
start,
);
start = byte_index + character.len_utf8();
}
// ⚑ Bolt Optimization: Use `match_indices` instead of `char_indices`
// to avoid UTF-8 decoding overhead when searching for an ASCII character.
for (byte_index, _) in line.match_indices('|') {
push_segment(
&mut segments,
&line[start..byte_index],
line_index + 1,
start,
);
start = byte_index + 1; // '|' is 1 byte
}

push_segment(&mut segments, &line[start..], line_index + 1, start);
Expand All @@ -102,10 +102,9 @@ fn push_segment<'a>(segments: &mut Vec<Segment<'a>>, raw: &'a str, line: usize,
}

fn parse_transform(text: &str, span: Span) -> Result<Transform, Diagnostic> {
let keyword_end = text
.char_indices()
.find_map(|(index, character)| character.is_whitespace().then_some(index))
.unwrap_or(text.len());
// ⚑ Bolt Optimization: Use `find(char::is_whitespace)` instead of `char_indices`
// to optimize whitespace scanning while preserving exact Unicode whitespace semantics.
let keyword_end = text.find(char::is_whitespace).unwrap_or(text.len());

let keyword = &text[..keyword_end];
let rest = text[keyword_end..].trim();
Expand Down
Loading