Skip to content

Commit 44f7889

Browse files
committed
perf: Improve capitalize from N allocs to 3.
Thank you Code Rabbit for noticing, and Gemini Pro 2.5 for the detailed perf comparison. What a beautiful future.
1 parent 9b7666f commit 44f7889

File tree

1 file changed

+5
-10
lines changed

1 file changed

+5
-10
lines changed

lib/llm/src/model_card.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -746,16 +746,11 @@ pub(crate) fn load_gguf(gguf_file: &Path) -> anyhow::Result<Content> {
746746
}
747747

748748
fn capitalize(s: &str) -> String {
749-
s.chars()
750-
.enumerate()
751-
.map(|(i, c)| {
752-
if i == 0 {
753-
c.to_uppercase().to_string()
754-
} else {
755-
c.to_lowercase().to_string()
756-
}
757-
})
758-
.collect()
749+
let mut chars = s.chars();
750+
match chars.next() {
751+
None => String::new(),
752+
Some(first) => first.to_uppercase().collect::<String>() + &chars.as_str().to_lowercase(),
753+
}
759754
}
760755

761756
impl ModelInfoType {

0 commit comments

Comments
 (0)