Skip to content

Commit 13df1c4

Browse files
⚡ Bolt: Optimize Value Display formatting
Refactor `fmt::Display` implementation for `Value` to write directly to the `fmt::Formatter`, eliminating multiple `String` allocations, `format!` usage, and `.clone()` calls during display formatting. Co-authored-by: ashyanSpada <22587148+ashyanSpada@users.noreply.github.com>
1 parent 5576973 commit 13df1c4

1 file changed

Lines changed: 12 additions & 12 deletions

File tree

src/value.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,27 @@ pub enum Value {
1515

1616
#[cfg(not(tarpaulin_include))]
1717
impl fmt::Display for Value {
18+
// ⚡ Bolt: Optimize Display by writing directly to formatter instead of allocating Strings
19+
// Expected impact: Eliminates heap allocations and redundant .clone() calls during display formatting,
20+
// reducing time per format and GC/allocator pressure.
1821
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1922
match self {
20-
Self::String(val) => write!(f, "value string: {}", val.clone()),
21-
Self::Number(val) => write!(f, "value number: {}", val.clone()),
22-
Self::Bool(val) => write!(f, "value bool: {}", val.clone()),
23+
Self::String(val) => write!(f, "value string: {}", val),
24+
Self::Number(val) => write!(f, "value number: {}", val),
25+
Self::Bool(val) => write!(f, "value bool: {}", val),
2326
Self::List(values) => {
24-
let mut s = String::from("[");
27+
write!(f, "value list: [")?;
2528
for value in values {
26-
s.push_str(format!("{},", value.clone()).as_str());
29+
write!(f, "{},", value)?;
2730
}
28-
s.push_str("]");
29-
write!(f, "value list: {}", s)
31+
write!(f, "]")
3032
}
3133
Self::Map(m) => {
32-
let mut s = String::from("{");
34+
write!(f, "value map: {{")?;
3335
for (k, v) in m {
34-
s.push_str(format!("key: {},", k.clone()).as_str());
35-
s.push_str(format!("value: {}; ", v.clone()).as_str());
36+
write!(f, "key: {},value: {}; ", k, v)?;
3637
}
37-
s.push_str("}");
38-
write!(f, "value map: {}", s)
38+
write!(f, "}}")
3939
}
4040
Self::None => write!(f, "None"),
4141
}

0 commit comments

Comments
 (0)