← Advanced course and all videos

Video 9 of 53 · 4:44 · Execution, sharing and reuse

Repeated work in a raw tree

In this video

  1. 0:00 · Introduction
  2. 0:18 · Watch a repeated tree execute
  3. 1:14 · Core explanation
  4. 2:22 · A different input changes values, not duplicated structure
  5. 3:33 · Add a memo and state what it identifies
  6. 4:32 · Direct takeaways

Caption transcript

Show transcript
This overview explains how an occurrence-based syntax-tree evaluator can repeat the same logical computation. A deliberately raw evaluator exposes duplication, but it is not automatically a fair substitute for a stronger memoized comparator. This expression contains A and B twice. Use A one, B one, and C zero. In a raw tree, the left occurrence and the nested occurrence can be separate nodes even though they print identically. Visit the left branch first. Read A and B and combine one with one, producing one. Keep that value visible while following a simple eager tree evaluator into the other branch; an evaluator that short-circuits or simplifies is a different declared model. The raw eager traversal computes A and B again, getting another one. It combines that one with C zero, giving zero, and then combines the two branches by or to obtain one. The duplicate calculation is visible in the trace, not inferred merely from the written formula. A raw occurrence-based evaluator follows written syntax. The left and right branches each contain A AND B, the subexpression named S. There are two occurrences even if the subexpression has the same meaning. At 11011, an occurrence traversal computes A AND B for the left branch and again for the right. The two visits are separate executions before their branches feed the final OR. Our companion teaching evaluator deliberately omits memoization here. The contrast is with a memoized traversal over the same rule, checked across all 32 assignments. This is an ablation that exposes duplication. The current direct BitSet implementation already memoizes shared object identities, so it cannot automatically stand in for this raw occurrence evaluator. Use a deliberately declared raw control, and do not call it the strongest comparator. Return to the opening three-input tree: F equals A and B, or the conjunction of A and B with C. This is separate from the intervening five-input tree. Set A to one, B to zero and C to one, and evaluate both copies under the same eager traversal. Under the declared eager raw traversal, the two occurrences of A and B are both evaluated and both return zero. The nested right branch then combines zero with C one, remaining zero. The root or returns zero. A different input has changed the values but not eliminated the duplicate subexpression from the tree. This case establishes a specific contrast. Eager trace evaluates both copies. Simplification or short-circuiting is a separate model. Keep the input, intermediate values, and conclusion together when comparing it with the previous case. The calculated answer belongs to this complete assignment under the stated rule. A memo keyed by a compatible subexpression and evaluation context can reuse the first result at the second occurrence. That changes the execution strategy. The same source formula can therefore have different costs under a raw traversal and a memoized evaluator. If a memo is keyed only by object identity, two separately allocated nodes may not share an entry. Equal printed text does not guarantee identical runtime objects. A structural sharing or canonicalization step may be needed to connect those identities. State these details before calling a comparator a raw evaluator. Otherwise a comparison can accidentally measure redundant work that another reasonable baseline already removes. The lesson is to inspect the execution mechanism, not just the formula's appearance. Distinguish written occurrences, stored objects, and executed work. Declare the evaluator's reuse behavior before using it as a control.