Skip to content
Draft
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
@@ -0,0 +1,3 @@
## 2024-05-20 - String and Iterator Micro-Optimizations
**Learning:** In hot or frequently executed string manipulations, intermediate `Vec` allocations from `collect()` can be surprisingly costly in Rust. `collect::<String>()` handles direct string concatenation more efficiently. Similarly, `split` on strings vs characters (`"\n"` vs `'\n'`) carries measurable overhead, and chaining iterators directly avoids temporary vector allocations.
**Action:** Always prefer `collect::<String>()` for aggregating strings instead of `.collect::<Vec<_>>().join("")`. Prefer `char`-based over string-slice-based splits, and extract elements via pattern-matching on the iterator's `.next()` to avoid intermediate `.collect::<Vec<_>>()` calls.
16 changes: 6 additions & 10 deletions xdk-build/src/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,12 @@ fn run_formatter(output_dir: &Path, venv_python_path: &Path, script_path: &Path)
let stdout = String::from_utf8_lossy(&output.stdout);
if !stdout.trim().is_empty() {
println!();
stdout
.split("\n")
.collect::<Vec<&str>>()
.into_iter()
.for_each(|line| {
let parts = line.split(" ").collect::<Vec<&str>>();
if parts.len() == 2 {
log_info!("{} {}", parts[0], parts[1].magenta());
}
});
stdout.split('\n').for_each(|line| {
let mut parts = line.split(' ');
if let (Some(p1), Some(p2), None) = (parts.next(), parts.next(), parts.next()) {
log_info!("{} {}", p1, p2.magenta());
}
});
println!();
}

Expand Down
6 changes: 1 addition & 5 deletions xdk-lib/src/casing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@ impl Casing {
result
}
}
Casing::Pascal => words
.iter()
.map(|w| pascal_case(w))
.collect::<Vec<_>>()
.join(""),
Casing::Pascal => words.iter().map(|w| pascal_case(w)).collect::<String>(),
Casing::Kebab => words.join("-").to_lowercase(),
Casing::ScreamingSnake => words.join("_").to_uppercase(),
}
Expand Down
Loading