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 @@ -9,3 +9,6 @@
## 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.
## 2026-08-16 - [Rust String Concatenation and Display Formatting Overhead]
**Learning:** In hot paths like SQL compilation and AST formatting, relying on `.join()` and intermediate `.map().collect::<Vec<_>>().join()` operations causes substantial unnecessary heap allocation for short-lived Strings and Vecs.
**Action:** When concatenating multiple strings or formatting values iteratively in Rust, iterate directly over the items and write directly to the buffer or `Formatter` rather than building intermediate `Vec`s and using `.join()`. This reduces memory overhead and improves performance.
29 changes: 22 additions & 7 deletions compiler/rockql-ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,32 @@ impl Display for Transform {
match self {
Self::From { source } => write!(formatter, "from {source}"),
Self::Filter { expression } => write!(formatter, "filter {expression}"),
Self::Select { columns } => write!(formatter, "select {}", columns.join(", ")),
Self::Select { columns } => {
// ⚑ Bolt Optimization: Prevent intermediate String and Vec allocation
// when formatting selected columns.
write!(formatter, "select ")?;
for (index, column) in columns.iter().enumerate() {
if index > 0 {
write!(formatter, ", ")?;
}
write!(formatter, "{}", column)?;
}
Ok(())
}
Self::Derive { name, expression } => {
write!(formatter, "derive {name} = {expression}")
}
Self::Sort { items } => {
let values = items
.iter()
.map(ToString::to_string)
.collect::<Vec<_>>()
.join(", ");
write!(formatter, "sort {{{values}}}")
// ⚑ Bolt Optimization: Prevent temporary vectors and string allocations
// by directly writing sort items to the formatter.
write!(formatter, "sort {{")?;
for (index, item) in items.iter().enumerate() {
if index > 0 {
write!(formatter, ", ")?;
}
write!(formatter, "{}", item)?;
}
write!(formatter, "}}")
}
Self::Take { count } => write!(formatter, "take {count}"),
}
Expand Down
18 changes: 16 additions & 2 deletions compiler/rockql-sql/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,28 @@ pub fn compile(query: &Query, dialect: Dialect) -> Result<String, SqlError> {
sql.push('\n');

if !filters.is_empty() {
// ⚑ Bolt Optimization: Avoid intermediate String allocation from `.join()`
// by pushing directly into the output buffer.
sql.push_str("WHERE ");
sql.push_str(&filters.join("\n AND "));
for (index, filter) in filters.iter().enumerate() {
if index > 0 {
sql.push_str("\n AND ");
}
sql.push_str(filter);
}
sql.push('\n');
}

if !sort_items.is_empty() {
// ⚑ Bolt Optimization: Avoid intermediate String allocation from `.join()`
// by pushing directly into the output buffer.
sql.push_str("ORDER BY ");
sql.push_str(&sort_items.join(", "));
for (index, item) in sort_items.iter().enumerate() {
if index > 0 {
sql.push_str(", ");
}
sql.push_str(item);
}
sql.push('\n');
}

Expand Down
Loading