Skip to content

Commit 6cf9bef

Browse files
authored
refactor(operator): replace vec! allocations with array literals in init (#44)
1 parent 5576973 commit 6cf9bef

4 files changed

Lines changed: 16 additions & 18 deletions

File tree

.jules/bolt.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## 2024-05-24 - Avoid vec! for constant collections in initialization loops
2+
**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.
3+
**Action:** Always prefer iterating over array literals instead of `vec![...]` for statically known collections, especially in hot paths or initialization loops.

benches/display_expression.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use criterion::{criterion_group, criterion_main, Criterion, black_box};
1+
use criterion::{black_box, criterion_group, criterion_main, Criterion};
22
use expression_engine::{parse_expression, ExprAST};
33

44
fn bench_display_expression(c: &mut Criterion) {

src/operator.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ impl InfixOpManager {
5757
use InfixOpType::*;
5858
self.register("=", 20, SETTER, RIGHT, Arc::new(|_, right| Ok(right)));
5959

60-
for op in vec!["+=", "-=", "*=", "/=", "%="] {
60+
// Iterate over an array literal `[...]` instead of `vec![...]` to avoid heap allocations
61+
for op in ["+=", "-=", "*=", "/=", "%="] {
6162
self.register(
6263
op,
6364
20,
@@ -88,7 +89,7 @@ impl InfixOpManager {
8889
);
8990
}
9091

91-
for op in vec!["<<=", ">>=", "&=", "^=", "|="] {
92+
for op in ["<<=", ">>=", "&=", "^=", "|="] {
9293
self.register(
9394
op,
9495
20,
@@ -109,7 +110,7 @@ impl InfixOpManager {
109110
);
110111
}
111112

112-
for (op, precedence) in vec![("||", 40), ("&&", 50)] {
113+
for (op, precedence) in [("||", 40), ("&&", 50)] {
113114
self.register(
114115
op,
115116
precedence,
@@ -127,7 +128,7 @@ impl InfixOpManager {
127128
);
128129
}
129130

130-
for op in vec!["<", "<=", ">", ">="] {
131+
for op in ["<", "<=", ">", ">="] {
131132
self.register(
132133
op,
133134
60,
@@ -148,7 +149,7 @@ impl InfixOpManager {
148149
);
149150
}
150151

151-
for op in vec!["==", "!="] {
152+
for op in ["==", "!="] {
152153
self.register(
153154
op,
154155
60,
@@ -166,7 +167,7 @@ impl InfixOpManager {
166167
);
167168
}
168169

169-
for (op, precedence) in vec![("|", 70), ("^", 80), ("&", 90), ("<<", 100), (">>", 100)] {
170+
for (op, precedence) in [("|", 70), ("^", 80), ("&", 90), ("<<", 100), (">>", 100)] {
170171
self.register(
171172
op,
172173
precedence,
@@ -187,7 +188,7 @@ impl InfixOpManager {
187188
);
188189
}
189190

190-
for (op, precedence) in vec![("+", 110), ("-", 110), ("*", 120), ("/", 120), ("%", 120)] {
191+
for (op, precedence) in [("+", 110), ("-", 110), ("*", 120), ("/", 120), ("%", 120)] {
191192
self.register(
192193
op,
193194
precedence,

src/parser.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,16 @@ impl<'a> fmt::Display for ExprAST<'a> {
5252
Self::Unary(op, rhs) => {
5353
write!(f, "Unary AST: Op: {}, Rhs: {}", op, rhs)
5454
}
55-
Self::Binary(op, lhs, rhs) => write!(
56-
f,
57-
"Binary AST: Op: {}, Lhs: {}, Rhs: {}",
58-
op,
59-
lhs,
60-
rhs
61-
),
55+
Self::Binary(op, lhs, rhs) => {
56+
write!(f, "Binary AST: Op: {}, Lhs: {}, Rhs: {}", op, lhs, rhs)
57+
}
6258
Self::Postfix(lhs, op) => {
6359
write!(f, "Postfix AST: Lhs: {}, Op: {}", lhs, op,)
6460
}
6561
Self::Ternary(condition, lhs, rhs) => write!(
6662
f,
6763
"Ternary AST: Condition: {}, Lhs: {}, Rhs: {}",
68-
condition,
69-
lhs,
70-
rhs
64+
condition, lhs, rhs
7165
),
7266
Self::Reference(name) => write!(f, "Reference AST: reference: {}", name),
7367
Self::Function(name, params) => {

0 commit comments

Comments
 (0)