-
Notifications
You must be signed in to change notification settings - Fork 4
⚡ Bolt: [performance improvement] Optimize Value enum Display to avoid string allocations #45
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
Open
ashyanSpada
wants to merge
1
commit into
master
Choose a base branch
from
bolt/display-value-optimization-16100803961087245175
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,7 @@ | ||
| ## 2024-05-24 - Avoid vec! for constant collections in initialization loops | ||
| **Learning:** Initializing maps/operator managers by iterating over `vec![...]` causes unnecessary heap allocations. Using array literals `[...]` is significantly more efficient since the size is known at compile time and the arrays can be stack-allocated or embedded directly into the binary. | ||
| **Action:** Always prefer iterating over array literals instead of `vec![...]` for statically known collections, especially in hot paths or initialization loops. | ||
|
|
||
| ## 2024-05-24 - [Format Display Optimizations] | ||
| **Learning:** For `std::fmt::Display` implementations involving collections (like Lists or Maps), building an intermediate `String` via `format!()` and `push_str()` creates unnecessary heap allocations and redundant cloning. Writing directly to the formatter using `write!(f, ...)` avoids intermediate `String` allocations entirely. | ||
| **Action:** Always write directly to the `std::fmt::Formatter` inside `fmt` methods rather than creating an intermediate string representation, particularly when dealing with container-like structures. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,25 +17,22 @@ pub enum Value { | |
| impl fmt::Display for Value { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| match self { | ||
| Self::String(val) => write!(f, "value string: {}", val.clone()), | ||
| Self::Number(val) => write!(f, "value number: {}", val.clone()), | ||
| Self::Bool(val) => write!(f, "value bool: {}", val.clone()), | ||
| Self::String(val) => write!(f, "value string: {}", val), | ||
| Self::Number(val) => write!(f, "value number: {}", val), | ||
| Self::Bool(val) => write!(f, "value bool: {}", val), | ||
| 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)?; | ||
| } | ||
|
Comment on lines
32
to
34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to the let mut iter = m.iter().peekable();
while let Some((k, v)) = iter.next() {
write!(f, "key: {},value: {}", k, v)?;
if iter.peek().is_some() {
write!(f, "; ")?;
}
} |
||
| s.push_str("}"); | ||
| write!(f, "value map: {}", s) | ||
| write!(f, "}}") | ||
| } | ||
| Self::None => write!(f, "None"), | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This loop will add a trailing comma for any non-empty list. For example, a list with
[1, 2]will be displayed asvalue list: [1,2,]. This is likely not the intended format. A common way to avoid this is to usepeekable()on the iterator to check if it's the last element.