Skip to content

Commit b837a63

Browse files
andygroveclaude
andcommitted
minor: use format!() and avoid unnecessary string allocations
- Replace `String::from("?") + &s` with `format!("?{s}")` in tokenizer - Replace `String::from("$") + &value` with `format!("${value}")` in tokenizer - Replace `tok.to_string() + &ident.value` with `format!("{tok}{}", ident.value)` in parser - Replace `" ".to_string() + &...` with `format!(" {}", ...)` in AST display - Replace `String::from("")` with `String::new()` - Replace `.to_string()` + `.clear()` with `std::mem::take()` to avoid cloning The format!() macro calculates required capacity upfront and allocates once, avoiding potential reallocation. std::mem::take() moves the String instead of cloning it. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 6550ec8 commit b837a63

3 files changed

Lines changed: 8 additions & 10 deletions

File tree

src/ast/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4921,9 +4921,9 @@ impl fmt::Display for Statement {
49214921
f,
49224922
"{tables}{read}{export}",
49234923
tables = if !tables.is_empty() {
4924-
" ".to_string() + &display_comma_separated(tables).to_string()
4924+
format!(" {}", display_comma_separated(tables))
49254925
} else {
4926-
"".to_string()
4926+
String::new()
49274927
},
49284928
export = if *export { " FOR EXPORT" } else { "" },
49294929
read = if *read_lock { " WITH READ LOCK" } else { "" }

src/parser/mod.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11146,16 +11146,14 @@ impl<'a> Parser<'a> {
1114611146
/// Parse a single tab-separated value row used by `COPY` payload parsing.
1114711147
pub fn parse_tab_value(&mut self) -> Vec<Option<String>> {
1114811148
let mut values = vec![];
11149-
let mut content = String::from("");
11149+
let mut content = String::new();
1115011150
while let Some(t) = self.next_token_no_skip().map(|t| &t.token) {
1115111151
match t {
1115211152
Token::Whitespace(Whitespace::Tab) => {
11153-
values.push(Some(content.to_string()));
11154-
content.clear();
11153+
values.push(Some(core::mem::take(&mut content)));
1115511154
}
1115611155
Token::Whitespace(Whitespace::Newline) => {
11157-
values.push(Some(content.to_string()));
11158-
content.clear();
11156+
values.push(Some(core::mem::take(&mut content)));
1115911157
}
1116011158
Token::Backslash => {
1116111159
if self.consume_token(&Token::Period) {
@@ -11280,7 +11278,7 @@ impl<'a> Parser<'a> {
1128011278
Token::Number(w, false) => Ok(Ident::with_span(next_token.span, w)),
1128111279
_ => self.expected("placeholder", next_token),
1128211280
}?;
11283-
Ok(Value::Placeholder(tok.to_string() + &ident.value)
11281+
Ok(Value::Placeholder(format!("{tok}{}", ident.value))
1128411282
.with_span(Span::new(span.start, ident.span.end)))
1128511283
}
1128611284
unexpected => self.expected(

src/tokenizer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1755,7 +1755,7 @@ impl<'a> Tokenizer<'a> {
17551755
'?' => {
17561756
chars.next();
17571757
let s = peeking_take_while(chars, |ch| ch.is_numeric());
1758-
Ok(Some(Token::Placeholder(String::from("?") + &s)))
1758+
Ok(Some(Token::Placeholder(format!("?{s}"))))
17591759
}
17601760

17611761
// identifier or keyword
@@ -1904,7 +1904,7 @@ impl<'a> Tokenizer<'a> {
19041904
}
19051905
}
19061906
} else {
1907-
return Ok(Token::Placeholder(String::from("$") + &value));
1907+
return Ok(Token::Placeholder(format!("${value}")));
19081908
}
19091909
}
19101910

0 commit comments

Comments
 (0)