-
Notifications
You must be signed in to change notification settings - Fork 4
⚡ Bolt: Optimize Display Formatting via Direct Stream Writes #55
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 |
|---|---|---|
| @@ -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)); | ||
| }) | ||
| }); | ||
| } | ||
|
|
||
| criterion_group!(benches, value_display_benchmark); | ||
| criterion_main!(benches); | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -315,39 +315,45 @@ | |||||
| condition.expr() + " ? " + &lhs.expr() + " : " + &rhs.expr() | ||||||
| } | ||||||
|
|
||||||
| #[cfg(not(tarpaulin_include))] | ||||||
Check warningCode 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 { | ||||||
|
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. Taking
Suggested change
|
||||||
| let mut s = String::from("["); | ||||||
| for i in 0..params.len() { | ||||||
| s.push_str(params[i].expr().as_str()); | ||||||
| s.push_str(¶ms[i].expr()); | ||||||
|
Comment on lines
+319
to
+323
|
||||||
| if i < params.len() - 1 { | ||||||
| s.push_str(","); | ||||||
| s.push(','); | ||||||
| } | ||||||
| } | ||||||
| s.push_str("]"); | ||||||
| s.push(']'); | ||||||
| s | ||||||
| } | ||||||
|
|
||||||
| #[cfg(not(tarpaulin_include))] | ||||||
Check warningCode 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 { | ||||||
|
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. |
||||||
| 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 warningCode 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 { | ||||||
|
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. |
||||||
| 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
|
||||||
| if i < exprs.len() - 1 { | ||||||
| s.push_str(";"); | ||||||
| s.push(';'); | ||||||
| } | ||||||
| } | ||||||
| s | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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
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. The current implementation of 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
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. The map formatting implementation introduces a trailing semicolon and space (e.g., 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"), | ||
| } | ||
|
|
||
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 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 formattedStringfrom theb.iterclosure (as done inbenches/display_expression.rs) or pass the result throughblack_boxso Criterion can reliably prevent dead-code elimination.