From df1a918bc862ca8a4cf54b0cb89b59b15e329d34 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 14 Jun 2026 21:27:39 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Optimize=20string=20allocat?= =?UTF-8?q?ions=20in=20casing=20utility?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Replace `.collect::>().join("")` with `.collect::()` to avoid an unnecessary intermediate vector allocation. * Replace `.collect::()` with `.to_string()` when lowercasing single characters to directly utilize the Display implementation. Co-authored-by: cloudesize67-cmd <237356855+cloudesize67-cmd@users.noreply.github.com> --- .jules/bolt.md | 3 +++ xdk-lib/src/casing.rs | 10 ++++------ 2 files changed, 7 insertions(+), 6 deletions(-) create mode 100644 .jules/bolt.md 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(), } }