diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 00000000..34d24f53 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-05-18 - Optimize string allocations in Casing conversions +**Learning:** In Rust, avoid `.collect::>().join("")` when you just want to concatenate strings. Collecting into a `Vec` allocates unnecessary intermediate memory. +**Action:** Use `.collect::()` directly on an iterator that yields string-like objects to prevent unnecessary `Vec` allocations. diff --git a/xdk-lib/src/casing.rs b/xdk-lib/src/casing.rs index 6463487e..9bf78a4b 100644 --- a/xdk-lib/src/casing.rs +++ b/xdk-lib/src/casing.rs @@ -29,11 +29,8 @@ impl Casing { result } } - Casing::Pascal => words - .iter() - .map(|w| pascal_case(w)) - .collect::>() - .join(""), + // Performance optimization: Using `.collect::()` instead of `.collect::>().join("")` avoids an intermediate Vec allocation + Casing::Pascal => words.iter().map(|w| pascal_case(w)).collect::(), Casing::Kebab => words.join("-").to_lowercase(), Casing::ScreamingSnake => words.join("_").to_uppercase(), } @@ -118,6 +115,7 @@ pub fn camel_case(value: &str) -> String { let mut chars = pascal.chars(); match chars.next() { None => String::new(), - Some(first) => first.to_lowercase().collect::() + chars.as_str(), + // Performance optimization: Avoids iterator collection overhead. + Some(first) => first.to_lowercase().to_string() + chars.as_str(), } }