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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ harness = false
[[bench]]
name = "display_expression"
harness = false

[[bench]]
name = "value_display"
harness = false
23 changes: 23 additions & 0 deletions benches/value_display.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use expression_engine::Value;
use rust_decimal::Decimal;

fn value_display_benchmark(c: &mut Criterion) {
let mut map_vec = Vec::new();
for i in 0..10 {
map_vec.push((
Value::String(format!("key{}", i)),
Value::Number(Decimal::from(i)),
));
}
let val = Value::Map(map_vec);

c.bench_function("value_display", |b| {
b.iter(|| {
let _ = format!("{}", black_box(&val));
Copy link

Copilot AI Apr 3, 2026

Choose a reason for hiding this comment

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

The benchmark closure discards the format! result (let _ = ...), which can allow the optimizer to remove most/all of the work and produce misleading timings. Return the formatted String from the b.iter closure (as done in benches/display_expression.rs) or pass the result through black_box so Criterion can reliably prevent dead-code elimination.

Suggested change
let _ = format!("{}", black_box(&val));
format!("{}", black_box(&val))

Copilot uses AI. Check for mistakes.
})
});
}

criterion_group!(benches, value_display_benchmark);
criterion_main!(benches);
28 changes: 17 additions & 11 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,39 +315,45 @@
condition.expr() + " ? " + &lhs.expr() + " : " + &rhs.expr()
}

#[cfg(not(tarpaulin_include))]

Check warning

Code scanning / clippy

unexpected cfg condition name: tarpaulin_include Warning

unexpected cfg condition name: tarpaulin_include
// ⚡ Bolt Optimization: Prevent redundant clones by borrowing elements and using single character push over push_str for delimiters.
fn list_expr(&self, params: Vec<ExprAST>) -> String {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

Taking Vec<ExprAST> by value forces the caller to clone the vector (as seen in the expr() method), which is expensive for complex ASTs. Changing the signature to take a slice &[ExprAST] allows the caller to avoid these unnecessary allocations while remaining compatible with the existing logic.

Suggested change
fn list_expr(&self, params: Vec<ExprAST>) -> String {
fn list_expr(&self, params: &[ExprAST]) -> String {

let mut s = String::from("[");
for i in 0..params.len() {
s.push_str(params[i].expr().as_str());
s.push_str(&params[i].expr());
Comment on lines +319 to +323
Copy link

Copilot AI Apr 3, 2026

Choose a reason for hiding this comment

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

This optimization comment is misleading for list_expr: both before and after the change, each params[i].expr() call still allocates a fresh String (and there wasn’t an ExprAST clone here). Consider rewording to reflect the actual change (delimiter push via char) or limiting the clone-avoidance claim to map_expr, where m[i].clone() was removed.

Copilot uses AI. Check for mistakes.
if i < params.len() - 1 {
s.push_str(",");
s.push(',');
}
}
s.push_str("]");
s.push(']');
s
}

#[cfg(not(tarpaulin_include))]

Check warning

Code scanning / clippy

unexpected cfg condition name: tarpaulin_include Warning

unexpected cfg condition name: tarpaulin_include
// ⚡ Bolt Optimization: Prevent redundant clones by borrowing elements and using single character push over push_str for delimiters.
fn map_expr(&self, m: Vec<(ExprAST, ExprAST)>) -> String {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

This method should take a slice &[(ExprAST, ExprAST)] instead of a Vec by value to prevent redundant cloning of the map entries during expression generation.

Suggested change
fn map_expr(&self, m: Vec<(ExprAST, ExprAST)>) -> String {
fn map_expr(&self, m: &[(ExprAST, ExprAST)]) -> String {

let mut s = String::from("{");
for i in 0..m.len() {
let (key, value) = m[i].clone();
s.push_str(key.expr().as_str());
s.push_str(":");
s.push_str(value.expr().as_str());
let (key, value) = &m[i];
s.push_str(&key.expr());
s.push(':');
s.push_str(&value.expr());
if i < m.len() - 1 {
s.push_str(",");
s.push(',');
}
}
s.push_str("}");
s.push('}');
s
}

#[cfg(not(tarpaulin_include))]

Check warning

Code scanning / clippy

unexpected cfg condition name: tarpaulin_include Warning

unexpected cfg condition name: tarpaulin_include
// ⚡ Bolt Optimization: Prevent redundant clones by borrowing elements and using single character push over push_str for delimiters.
fn chain_expr(&self, exprs: Vec<ExprAST>) -> String {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

Using a slice &[ExprAST] here would avoid the need for the caller to clone the expression chain, improving performance in line with the 'Bolt' optimization goals.

Suggested change
fn chain_expr(&self, exprs: Vec<ExprAST>) -> String {
fn chain_expr(&self, exprs: &[ExprAST]) -> String {

let mut s = String::new();
for i in 0..exprs.len() {
s.push_str(exprs[i].expr().as_str());
s.push_str(&exprs[i].expr());
Comment on lines +350 to +354
Copy link

Copilot AI Apr 3, 2026

Choose a reason for hiding this comment

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

This optimization comment is misleading for chain_expr: the implementation still allocates a new String for each exprs[i].expr() call, and this change didn’t remove any cloning of ExprAST values. Consider rewording the comment to focus on delimiter handling (push(';')) rather than clone avoidance.

Copilot uses AI. Check for mistakes.
if i < exprs.len() - 1 {
s.push_str(";");
s.push(';');
}
}
s
Expand Down
24 changes: 12 additions & 12 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,27 @@ pub enum Value {

#[cfg(not(tarpaulin_include))]
impl fmt::Display for Value {
// ⚡ Bolt Optimization: Avoid intermediate String allocations when formatting collections.
// Instead of using `format!` and `push_str` which cause unnecessary heap allocations and copying,
// we write directly to the `fmt::Formatter` via `write!`. This significantly improves formatting performance.
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, "]")
}
Comment on lines 26 to 32
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 of Value::List formatting results in a trailing comma (e.g., [1,2,3,]). It is more idiomatic and cleaner to only include separators between elements.

            Self::List(values) => {
                write!(f, "value list: [")?;
                for (i, value) in values.iter().enumerate() {
                    if i > 0 {
                        write!(f, ",")?;
                    }
                    write!(f, "{}", value)?;
                }
                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 33 to 39
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 map formatting implementation introduces a trailing semicolon and space (e.g., {key: k,value: v; }). Conditionally adding the separator improves the output quality and consistency.

            Self::Map(m) => {
                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, "}}")
            }

Self::None => write!(f, "None"),
}
Expand Down
Loading