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 string collection allocations
**Learning:** Rust's `.collect::<Vec<_>>().join("")` allocates an unnecessary intermediate vector, and `.to_lowercase().collect::<String>()` misses out on `Display` optimizations compared to `.to_lowercase().to_string()`.
**Action:** Use `.collect::<String>()` directly when concatenating strings from an iterator, and `.to_string()` for char transformations to save memory and CPU cycles.
5 changes: 3 additions & 2 deletions xdk-lib/src/casing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ impl Casing {
Casing::Pascal => words
.iter()
.map(|w| pascal_case(w))
.collect::<Vec<_>>()
.join(""),
// Bolt optimization: avoiding `.collect::<Vec<_>>().join("")`
// prevents allocating an intermediate vector just to concatenate strings.
.collect::<String>(),
Casing::Kebab => words.join("-").to_lowercase(),
Casing::ScreamingSnake => words.join("_").to_uppercase(),
}
Expand Down
Loading