TODO: move this elsewhere? Recursively memoize parts of `expr`, storing those parts in `memoized_parts`. A part of `expr` that is memoized is replaced by a reference to column `(input_arity + pos)`, where `pos` is the position of the memoized part in `memoized_parts`, and `input_arity` is the arity of the input that `expr` refers to.
(
expr: &mut E,
memoized_parts: &mut Vec<E>,
input_arity: usize,
)
| 1441 | /// `memoized_parts`, and `input_arity` is the arity of the input that `expr` |
| 1442 | /// refers to. |
| 1443 | pub fn memoize_expr<E: OptimizableExpr>( |
| 1444 | expr: &mut E, |
| 1445 | memoized_parts: &mut Vec<E>, |
| 1446 | input_arity: usize, |
| 1447 | ) { |
| 1448 | expr.visit_mut_pre_post(&mut |e| e.eager_children(), &mut |e| { |
| 1449 | if E::is_literal(e) { |
| 1450 | // Literals do not need to be memoized. |
| 1451 | return; |
| 1452 | } |
| 1453 | if let Some(col) = e.as_column_mut() { |
| 1454 | // Column references do not need to be memoized, but may need to be |
| 1455 | // updated if they reference a column reference themselves. |
| 1456 | if *col > input_arity { |
| 1457 | if let Some(col2) = memoized_parts[*col - input_arity].as_column() { |
| 1458 | // Update the column index in place, preserving any name information. |
| 1459 | *col = col2; |
| 1460 | } |
| 1461 | } |
| 1462 | return; |
| 1463 | } |
| 1464 | // TODO: OOO (Optimizer Optimization Opportunity): |
| 1465 | // we are quadratic in expression size because of this .iter().position |
| 1466 | if let Some(position) = memoized_parts.iter().position(|e2| e2 == e) { |
| 1467 | // Any complex expression that already exists as a prior column can |
| 1468 | // be replaced by a reference to that column. |
| 1469 | *e = E::column(input_arity + position); |
| 1470 | } else { |
| 1471 | // A complex expression that does not exist should be memoized, and |
| 1472 | // replaced by a reference to the column. |
| 1473 | memoized_parts.push(std::mem::replace( |
| 1474 | e, |
| 1475 | E::column(input_arity + memoized_parts.len()), |
| 1476 | )); |
| 1477 | } |
| 1478 | }); |
| 1479 | } |
| 1480 | |
| 1481 | pub mod util { |
| 1482 | use std::collections::BTreeMap; |
no test coverage detected