Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .jules/bolt.md
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.
21 changes: 9 additions & 12 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
}
Comment on lines 25 to 27
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

This loop will add a trailing comma for any non-empty list. For example, a list with [1, 2] will be displayed as value list: [1,2,]. This is likely not the intended format. A common way to avoid this is to use peekable() on the iterator to check if it's the last element.

Suggested change
for value in values {
s.push_str(format!("{},", value.clone()).as_str());
write!(f, "{},", value)?;
}
let mut iter = values.iter().peekable();
while let Some(value) = iter.next() {
write!(f, "{}", value)?;
if iter.peek().is_some() {
write!(f, ",")?;
}
}

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
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 case, this loop will add a trailing separator (; ) for any non-empty map. To avoid this, you can use peekable() on the iterator.

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