Skip to content
Merged
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
40 changes: 18 additions & 22 deletions src/optimizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -972,17 +972,15 @@ fn optimize_expr(expr: &mut Expr, state: &mut OptimizerState, _chaining: bool) {
Expr::Dot(x, ..) if !_chaining => match (&mut x.lhs, &mut x.rhs) {
// map.string
(Expr::Map(m, pos), Expr::Property(p, ..)) if m.0.iter().all(|(.., x)| x.is_pure()) => {
if m.1.contains_key(p.2.as_str()) {
// Constant map literal property
state.set_dirty();
*expr = Expr::from_dynamic(mem::take(m.1.get_mut(p.2.as_str()).unwrap()).into(), *pos);
} else {
// Map literal where everything is pure - promote the indexed item.
// All other items can be thrown away.
state.set_dirty();
*expr = mem::take(&mut m.0).into_iter().find(|(x, ..)| x.name == p.2)
.map_or_else(|| Expr::Unit(*pos), |(.., mut expr)| { expr.set_position(*pos); expr });
}
// Map literal where everything is pure - promote the indexed item.
// All other items can be thrown away.
// The template holds every key, with `()` standing in for a value that is
// not a constant, so look among the non-constant values first.
state.set_dirty();
*expr = match mem::take(&mut m.0).into_iter().find(|(x, ..)| x.name == p.2) {
Some((.., mut expr)) => { expr.set_position(*pos); expr }
None => m.1.get_mut(p.2.as_str()).map_or_else(|| Expr::Unit(*pos), |v| Expr::from_dynamic(mem::take(v), *pos)),
};
}
// var.property where var is a constant map
(Expr::Variable(v, _, pos) , Expr::Property(p, ..)) if state.propagate_constants && state.find_literal_constant(&v.1).map_or(false,Dynamic::is_map) => {
Expand Down Expand Up @@ -1045,17 +1043,15 @@ fn optimize_expr(expr: &mut Expr, state: &mut OptimizerState, _chaining: bool) {
}
// map[string]
(Expr::Map(m, pos), Expr::StringConstant(s, ..)) if m.0.iter().all(|(.., x)| x.is_pure()) => {
if m.1.contains_key(s.as_str()) {
// Constant map literal property
state.set_dirty();
*expr = Expr::from_dynamic(mem::take(m.1.get_mut(s.as_str()).unwrap()).into(), *pos);
} else {
// Map literal where everything is pure - promote the indexed item.
// All other items can be thrown away.
state.set_dirty();
*expr = mem::take(&mut m.0).into_iter().find(|(x, ..)| x.name == s)
.map_or_else(|| Expr::Unit(*pos), |(.., mut expr)| { expr.set_position(*pos); expr });
}
// Map literal where everything is pure - promote the indexed item.
// All other items can be thrown away.
// The template holds every key, with `()` standing in for a value that is
// not a constant, so look among the non-constant values first.
state.set_dirty();
*expr = match mem::take(&mut m.0).into_iter().find(|(x, ..)| x.name == s) {
Some((.., mut expr)) => { expr.set_position(*pos); expr }
None => m.1.get_mut(s.as_str()).map_or_else(|| Expr::Unit(*pos), |v| Expr::from_dynamic(mem::take(v), *pos)),
};
}
// map[string] - constant
#[cfg(not(feature = "no_object"))]
Expand Down
6 changes: 6 additions & 0 deletions tests/maps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,16 @@ fn test_map_template_evaluation_and_isolation() {
assert_eq!(engine.eval::<INT>("#{ a: 42, b: () }.a").unwrap(), 42);
assert_eq!(engine.eval_with_scope::<INT>(&mut scope, "#{ a: 42, b: input }.a").unwrap(), 42);

// Values that only become constant after optimization are not in the template yet
assert_eq!(engine.eval::<INT>("#{ a: 42, b: { 1 } }.b").unwrap(), 1);
assert_eq!(engine.eval::<INT>("#{ a: if true { 1 } else { 2 } }.a").unwrap(), 1);
assert_eq!(engine.eval::<INT>("#{ a: 42, b: (() ?? 1) }.b").unwrap(), 1);

#[cfg(not(feature = "no_index"))]
{
assert_eq!(engine.eval::<INT>(r#"#{ a: 42, b: () }["a"]"#).unwrap(), 42);
assert_eq!(engine.eval_with_scope::<INT>(&mut scope, r#"#{ a: 42, b: input }["a"]"#).unwrap(), 42);
assert_eq!(engine.eval::<INT>(r#"#{ a: 42, b: { 1 } }["b"]"#).unwrap(), 1);

let ast = engine.compile("#{ values: [#{ x: 1 }], computed: input }").unwrap();

Expand Down
Loading