diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 00000000..06cb026e --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-05-24 - Avoid string collection allocations +**Learning:** Rust's `.collect::>().join("")` allocates an unnecessary intermediate vector, and `.to_lowercase().collect::()` misses out on `Display` optimizations compared to `.to_lowercase().to_string()`. +**Action:** Use `.collect::()` directly when concatenating strings from an iterator, and `.to_string()` for char transformations to save memory and CPU cycles. diff --git a/xdk-lib/src/casing.rs b/xdk-lib/src/casing.rs index 6463487e..6d860bab 100644 --- a/xdk-lib/src/casing.rs +++ b/xdk-lib/src/casing.rs @@ -32,8 +32,9 @@ impl Casing { Casing::Pascal => words .iter() .map(|w| pascal_case(w)) - .collect::>() - .join(""), + // Bolt optimization: avoiding `.collect::>().join("")` + // prevents allocating an intermediate vector just to concatenate strings. + .collect::(), Casing::Kebab => words.join("-").to_lowercase(), Casing::ScreamingSnake => words.join("_").to_uppercase(), }