⚡ Bolt: Optimize Display implementation for Value by removing heap allocations#52
⚡ Bolt: Optimize Display implementation for Value by removing heap allocations#52ashyanSpada wants to merge 1 commit intomasterfrom
Conversation
This replaces `.clone()` calls and intermediate `String` creations with direct `write!` calls to the formatter, saving memory allocations. Co-authored-by: ashyanSpada <22587148+ashyanSpada@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #52 +/- ##
=======================================
Coverage 89.55% 89.55%
=======================================
Files 11 11
Lines 1063 1063
=======================================
Hits 952 952
Misses 111 111 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Code Review
This pull request refactors the Display implementation for the Value enum, improving efficiency by removing redundant clones and replacing manual string concatenation with direct writes to the formatter. The review feedback identifies opportunities to improve the output formatting of lists and maps by eliminating trailing separators.
| write!(f, "value list: [")?; | ||
| for value in values { | ||
| s.push_str(format!("{},", value.clone()).as_str()); | ||
| write!(f, "{},", value)?; | ||
| } | ||
| s.push_str("]"); | ||
| write!(f, "value list: {}", s) | ||
| write!(f, "]") |
There was a problem hiding this comment.
The current implementation results in a trailing comma for non-empty lists (e.g., [1,2,3,]). While the PR aims to maintain parity with the previous implementation, this is a good opportunity to improve the output formatting by removing the trailing separator for a more standard representation.
| write!(f, "value list: [")?; | |
| for value in values { | |
| s.push_str(format!("{},", value.clone()).as_str()); | |
| write!(f, "{},", value)?; | |
| } | |
| s.push_str("]"); | |
| write!(f, "value list: {}", s) | |
| write!(f, "]") | |
| write!(f, "value list: [")?; | |
| for (i, value) in values.iter().enumerate() { | |
| if i > 0 { | |
| write!(f, ",")?; | |
| } | |
| write!(f, "{}", value)?; | |
| } | |
| write!(f, "]") |
| write!(f, "value map: {{")?; | ||
| for (k, v) in m { | ||
| s.push_str(format!("key: {},", k.clone()).as_str()); | ||
| s.push_str(format!("value: {}; ", v.clone()).as_str()); | ||
| write!(f, "key: {},value: {}; ", k, v)?; | ||
| } | ||
| s.push_str("}"); | ||
| write!(f, "value map: {}", s) | ||
| write!(f, "}}") |
There was a problem hiding this comment.
Similar to the list implementation, the map formatting includes a trailing semicolon and space (e.g., {key: k,value: v; }). Consider refactoring the loop to avoid the trailing separator and ensure a cleaner, more consistent output format.
write!(f, "value map: {{")?;
for (i, (k, v)) in m.iter().enumerate() {
if i > 0 {
write!(f, " ")?;
}
write!(f, "key: {},value: {};", k, v)?;
}
write!(f, "}}")There was a problem hiding this comment.
Pull request overview
Optimizes the fmt::Display implementation for the Value enum by removing unnecessary cloning and avoiding intermediate String allocations during formatting.
Changes:
- Removes redundant
.clone()calls when formatting scalarValuevariants. - Reworks
ListandMapformatting to write directly into thefmt::Formatterinstead of building intermediate strings.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| Self::List(values) => { | ||
| let mut s = String::from("["); | ||
| write!(f, "value list: [")?; | ||
| for value in values { | ||
| s.push_str(format!("{},", value.clone()).as_str()); | ||
| write!(f, "{},", value)?; | ||
| } | ||
| s.push_str("]"); | ||
| write!(f, "value list: {}", s) | ||
| write!(f, "]") | ||
| } | ||
| Self::Map(m) => { | ||
| let mut s = String::from("{"); | ||
| write!(f, "value map: {{")?; | ||
| for (k, v) in m { | ||
| s.push_str(format!("key: {},", k.clone()).as_str()); | ||
| s.push_str(format!("value: {}; ", v.clone()).as_str()); | ||
| write!(f, "key: {},value: {}; ", k, v)?; | ||
| } | ||
| s.push_str("}"); | ||
| write!(f, "value map: {}", s) | ||
| write!(f, "}}") |
There was a problem hiding this comment.
The Display output for Value::List/Value::Map is now built via multiple write! calls. Since this is user-visible formatting and can be easy to accidentally change (e.g., trailing commas, braces, separators), please add a unit test that asserts the exact to_string() output for a few representative values (including empty/non-empty list and map) to prevent regressions during future refactors.
💡 What: Optimized the
fmt::Displayimplementation for theValueenum insrc/value.rsby removing unnecessary.clone()calls and intermediateStringallocations (e.g.,String::from,format!(), andpush_str()).🎯 Why: Writing directly to the
Formatterusingwrite!significantly reduces memory allocation and copying overhead, leading to faster string formatting operations for AST evaluation and display.📊 Impact: Eliminates several heap allocations and string copies per formatted list or map value, improving overall formatting performance.
🔬 Measurement: Verified through
cargo testandcargo bench, which show no regressions in behavior and maintain exact output formatting.PR created automatically by Jules for task 14944960043651433426 started by @ashyanSpada