-
Notifications
You must be signed in to change notification settings - Fork 4
⚡ Bolt: [performance improvement] optimize Value enum Display allocation #49
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
base: master
Are you sure you want to change the base?
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 |
|---|---|---|
|
|
@@ -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)?; | ||
| } | ||
| s.push_str("}"); | ||
| write!(f, "value map: {}", s) | ||
| write!(f, "}}") | ||
|
Comment on lines
23
to
+35
|
||
| } | ||
|
Comment on lines
30
to
36
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 Self::Map(m) => {
write!(f, "value map: {{")?;
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, "; ")?;
}
}
write!(f, "}}")
} |
||
| Self::None => write!(f, "None"), | ||
| } | ||
|
|
||
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 current implementation for formatting a
Listproduces a trailing comma (e.g.,[v1,v2,]). While this change preserves the original behavior, it's generally better to produce a clean, comma-separated list without a trailing separator. This improves the readability and correctness of the string representation. You can achieve this by using apeekableiterator to check if it's the last element before printing the comma.