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-24 - [Avoid Unnecessary Vec Allocations for Iterator Output]
**Learning:** Using `.collect::<Vec<_>>().join("")` allocates an unnecessary intermediate `Vec` when building strings from iterators. Using `.collect::<String>()` performs the same logic without intermediate vector allocations. Also replacing `.split("\n").collect::<Vec<&str>>().into_iter()` with `.split('\n')` and similar for space separating eliminates multiple `Vec` allocations during string formatting.
**Action:** Always prefer `.collect::<String>()` over `.collect::<Vec<_>>().join("")` when concatenating string components in Rust. For string splitting, use direct iterator methods over intermediate collection.
17 changes: 7 additions & 10 deletions xdk-build/src/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,13 @@ 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());
}
});
// Optimization: Use character splits and avoid allocating Vecs for lines and parts
for line in stdout.split('\n') {
let mut parts = line.split(' ');
if let (Some(part0), Some(part1), None) = (parts.next(), parts.next(), parts.next()) {
log_info!("{} {}", part0, part1.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>(), // Optimization: Use collect::<String>() directly instead of collecting to Vec and joining
Casing::Kebab => words.join("-").to_lowercase(),
Casing::ScreamingSnake => words.join("_").to_uppercase(),
}
Expand Down
Loading