-
Notifications
You must be signed in to change notification settings - Fork 0
⚡ Bolt: [Optimize string concatenation] #471
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -7543,8 +7543,12 @@ impl Interpreter { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return Value::Text(Arc::from(s)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let result = format!("{left_val}{right_val}"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Value::Text(Arc::from(result.as_str())) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let left_str = left_val.to_string_fast(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let right_str = right_val.to_string_fast(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let mut s = String::with_capacity(left_str.len() + right_str.len()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| s.push_str(&left_str); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| s.push_str(&right_str); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+7546
to
+7550
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let left_str = left_val.to_string_fast(); | |
| let right_str = right_val.to_string_fast(); | |
| let mut s = String::with_capacity(left_str.len() + right_str.len()); | |
| s.push_str(&left_str); | |
| s.push_str(&right_str); | |
| let mut s = match (&left_val, &right_val) { | |
| (Value::Text(left), _) => { | |
| let mut s = String::with_capacity(left.len()); | |
| s.push_str(left); | |
| std::fmt::Write::write_fmt(&mut s, format_args!("{right_val}")) | |
| .expect("writing to String cannot fail"); | |
| s | |
| } | |
| (_, Value::Text(right)) => { | |
| let mut s = String::with_capacity(right.len()); | |
| std::fmt::Write::write_fmt(&mut s, format_args!("{left_val}")) | |
| .expect("writing to String cannot fail"); | |
| s.push_str(right); | |
| s | |
| } | |
| _ => { | |
| let mut s = String::new(); | |
| std::fmt::Write::write_fmt(&mut s, format_args!("{left_val}{right_val}")) | |
| .expect("writing to String cannot fail"); | |
| s | |
| } | |
| }; |
Copilot
AI
Apr 25, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same intermediate-allocation issue applies in these add arms: b.to_string_fast() / a.to_string_fast() may allocate a temporary String for many variants (numbers, lists, objects, etc.) before copying into s. If you keep the pre-allocation approach, consider an API that appends directly into the output string for non-borrowed variants to avoid double-allocation during Text + non-Text and non-Text + Text additions.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -157,6 +157,17 @@ pub struct ActionSignature { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| impl Value { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub fn to_string_fast(&self) -> std::borrow::Cow<'_, str> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| match self { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Value::Text(s) => std::borrow::Cow::Borrowed(s.as_ref()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Value::Number(n) => std::borrow::Cow::Owned(n.to_string()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Value::Bool(b) => std::borrow::Cow::Borrowed(if *b { "yes" } else { "no" }), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Value::Null => std::borrow::Cow::Borrowed("nothing"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Value::Nothing => std::borrow::Cow::Borrowed("nothing"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _ => std::borrow::Cow::Owned(self.to_string()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+160
to
+167
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub fn to_string_fast(&self) -> std::borrow::Cow<'_, str> { | |
| match self { | |
| Value::Text(s) => std::borrow::Cow::Borrowed(s.as_ref()), | |
| Value::Number(n) => std::borrow::Cow::Owned(n.to_string()), | |
| Value::Bool(b) => std::borrow::Cow::Borrowed(if *b { "yes" } else { "no" }), | |
| Value::Null => std::borrow::Cow::Borrowed("nothing"), | |
| Value::Nothing => std::borrow::Cow::Borrowed("nothing"), | |
| _ => std::borrow::Cow::Owned(self.to_string()), | |
| pub fn write_to(&self, out: &mut dyn fmt::Write) -> fmt::Result { | |
| match self { | |
| Value::Text(s) => out.write_str(s.as_ref()), | |
| Value::Number(n) => write!(out, "{}", n), | |
| Value::Bool(b) => out.write_str(if *b { "yes" } else { "no" }), | |
| Value::Null => out.write_str("nothing"), | |
| Value::Nothing => out.write_str("nothing"), | |
| _ => write!(out, "{}", self), | |
| } | |
| } | |
| pub fn append_to(&self, out: &mut String) { | |
| self.write_to(out) | |
| .expect("writing to a String should never fail"); | |
| } | |
| pub fn to_string_fast(&self) -> std::borrow::Cow<'_, str> { | |
| match self { | |
| Value::Text(s) => std::borrow::Cow::Borrowed(s.as_ref()), | |
| Value::Bool(b) => std::borrow::Cow::Borrowed(if *b { "yes" } else { "no" }), | |
| Value::Null => std::borrow::Cow::Borrowed("nothing"), | |
| Value::Nothing => std::borrow::Cow::Borrowed("nothing"), | |
| _ => { | |
| let mut out = String::new(); | |
| self.append_to(&mut out); | |
| std::borrow::Cow::Owned(out) | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix future-dated changelog entry (
2026-05-15)This PR was opened on April 25, 2026, so the entry date on Line 65 is in the future. Please use an actual date (likely 2026-04-25) to keep the learning log chronologically accurate.
🤖 Prompt for AI Agents