Skip to content

⚡ Bolt: Optimize Display implementation for Value by removing heap allocations#52

Open
ashyanSpada wants to merge 1 commit intomasterfrom
bolt-optimize-value-display-14944960043651433426
Open

⚡ Bolt: Optimize Display implementation for Value by removing heap allocations#52
ashyanSpada wants to merge 1 commit intomasterfrom
bolt-optimize-value-display-14944960043651433426

Conversation

@ashyanSpada
Copy link
Copy Markdown
Owner

💡 What: Optimized the fmt::Display implementation for the Value enum in src/value.rs by removing unnecessary .clone() calls and intermediate String allocations (e.g., String::from, format!(), and push_str()).
🎯 Why: Writing directly to the Formatter using write! 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 test and cargo bench, which show no regressions in behavior and maintain exact output formatting.


PR created automatically by Jules for task 14944960043651433426 started by @ashyanSpada

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>
@google-labs-jules
Copy link
Copy Markdown
Contributor

👋 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 @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Copilot AI review requested due to automatic review settings March 31, 2026 21:24
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 31, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.55%. Comparing base (9a4a6cc) to head (6022f8f).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +24 to +28
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, "]")
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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, "]")

Comment on lines +31 to +35
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, "}}")
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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, "}}")

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 scalar Value variants.
  • Reworks List and Map formatting to write directly into the fmt::Formatter instead of building intermediate strings.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 23 to +35
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, "}}")
Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants