Adapton: Composable Demand-Driven Incremental Computation Matthew A. Hammer Khoo Yit Phang, Michael Hicks and Jeffrey S. Foster
Incremental Computation Input Output Application Application Code IC IC Framework Framework
☞ Incremental Computation Input Output Trace Trace records dynamic data Application dependencies IC Framework
☞ Incremental Computation Input Output Trace Run 1 Mutations Input Trace Output
☞ Incremental Computation Input Output Trace Run 1 Mutations Inconsistencies Input Trace Output
Incremental Computation Input Output Trace Run 1 Change- Propagation Run 2 Input Trace Output
Incremental Computation Input Output Trace Run 1 Change- Updates Propagation Run 2 Input Trace Output
Incremental Computation Input Output Trace Run 1 Change- Updates Propagation Run 2 Input Trace Output
Incremental Computation Input Output Trace Run 1 Change- Updates Propagation Run 2 Input Trace Output
Incremental Computation Input Output Trace Run 1 Run 2 Input Trace Output
☞ Incremental Computation Observations Run 2 Input Trace Output
☞ Incremental Computation loop.. Mutations Observations Run 2 Input Trace Output
Incremental Computation Propagation respects program semantics: Theorem: Equivalently: Trace and output are Change propagation is “from-scratch”- History consistent independent Run 2 Input Trace Output
Existing Limitations (self-adjusting computation) ‣ Change propagation is eager ? Not driven by output observations ‣ Trace representation = Total ordering Limits reuse, excluding certain patterns Interactive settings suffer in particular
Adapton : Composable, Demand-Driven IC • Key concepts: Lazy thunks: programming interface Demanded Computation Graph (DCG) : represents execution trace • Formal semantics, proven sound • Implemented in OCaml (and Python) • Speedups for all patterns (unlike SAC) • Freely available at http://ter.ps/adapton
Interaction Pattern: Laziness Do not (re)compute obscured sheets Sheet A Sheet B Sheet C Legend Consistent No cache (Independent sheets) Inactive
Interaction Pattern: Laziness Do not (re)compute obscured sheets Sheet A Sheet B Sheet C Legend Consistent No cache (Independent sheets) Inactive
Interaction Pattern: Laziness Do not (re)compute obscured sheets Sheet A Sheet B Sheet C Legend Consistent No cache (Independent sheets) Inactive
Interactive Pattern: Switching Demand / control-flow change Sheet A Sheet B Legend Sheet C = f ( A ) Consistent No cache Inactive
Interactive Pattern: Switching Demand / control-flow change Sheet A Sheet B Legend Sheet C = f ( A ) Consistent C = g ( B ) No cache Inactive
Interactive Pattern: Switching Demand / control-flow change Sheet A Sheet B Legend Sheet C = f ( A ) Consistent C = g ( B ) No cache Inactive
Interaction Pattern: Sharing B and C share work for A Sheet A Sheet Sheet B = f ( A ) C = g ( A ) Legend Consistent No cache Inactive
Interaction Pattern: Sharing B and C share work for A Sheet A Sheet Sheet B = f ( A ) C = g ( A ) Legend Consistent No cache Inactive
Interaction Pattern: Sharing B and C share work for A Sheet A Sheet Sheet B = f ( A ) C = g ( A ) Legend Consistent No cache Inactive
Interactive Pattern: Swapping Swaps input / evaluation order Sheet A Sheet B Sheet Legend C = f (A, B) Consistent No cache Inactive
Interactive Pattern: Swapping Swaps input / evaluation order Sheet A Sheet B Sheet Legend C = f (A, B) Consistent C = f (B, A) No cache Inactive
Interactive Pattern: Swapping Swaps input / evaluation order Sheet A Sheet B Sheet Legend C = f (A, B) Consistent C = f (B, A) No cache Inactive
Adapton’s Approach • When we mutate an input , we mark dependent computations as dirty • When we demand a thunk : • Memo-match equivalent thunks • Change-propagation repairs inconsistencies, on demand
Spread Sheet Evaluator type cell = formula ref and formula = | Leaf of int | Plus of cell * cell
Spread Sheet Evaluator Mutable type cell = formula ref and formula = | Leaf of int | Plus of cell * cell Depends on cells
Spread Sheet Evaluator Example let n 1 = ref (Leaf 1) let n 2 = ref (Leaf 2) let n 3 = ref (Leaf 3) let p 1 = ref (Plus (n 1 , n 2 )) type cell = formula ref let p 2 = ref (Plus (p 1 , n 3 )) and formula = | Leaf of int | Plus of cell * cell
Spread Sheet Evaluator n 1 n 2 1 1 1 2 Example n 3 p 1 1 1 let n 1 = ref (Leaf 1) + 3 let n 2 = ref (Leaf 2) p 2 1 + let n 3 = ref (Leaf 3) let p 1 = ref (Plus (n 1 , n 2 )) type cell = formula ref let p 2 = ref (Plus (p 1 , n 3 )) and formula = | Leaf of int “User interface” (REPL) | Plus of cell * cell
Spread Sheet Evaluator Evaluator logic n 1 n 2 1 1 1 2 eval : cell → (int thunk) n 3 p 1 1 1 eval c = thunk (( + 3 case (get c) of p 2 | Leaf n ⇒ n 1 + | Plus(c1, c2) ⇒ type cell = formula ref force (eval c1) + force (eval c2) and formula = )) | Leaf of int | Plus of cell * cell
Spread Sheet Evaluator n 1 n 2 set : cell x formula → unit 1 1 1 2 eval : cell → (int thunk) n 3 p 1 1 1 + 3 display : (int thunk) → unit “User interface” (REPL) p 2 1 + type cell = formula ref and formula = | Leaf of int | Plus of cell * cell
Spread Sheet Evaluator n 1 n 2 set : cell x formula → unit 1 1 1 2 eval : cell → (int thunk) n 3 p 1 1 1 + 3 display : (int thunk) → unit “User interface” (REPL) p 2 1 + Demands evaluation type cell = formula ref and formula = | Leaf of int | Plus of cell * cell
Spread Sheet Evaluator n 1 n 2 set : cell x formula → unit 1 1 1 2 eval : cell → (int thunk) n 3 p 1 1 1 + 3 display : (int thunk) → unit “User interface” (REPL) p 2 1 + type cell = formula ref and formula = | Leaf of int | Plus of cell * cell
Spread Sheet Evaluator n 1 n 2 set : cell x formula → unit 1 1 1 2 eval : cell → (int thunk) n 3 p 1 1 1 + 3 display : (int thunk) → unit “User interface” (REPL) p 2 1 + type cell = formula ref ☞ let t 1 = eval p 1 and formula = | Leaf of int | Plus of cell * cell
☞ Spread Sheet Evaluator n 1 n 2 set : cell x formula → unit 1 1 1 2 eval : cell → (int thunk) n 3 p 1 1 1 + 3 display : (int thunk) → unit “User interface” (REPL) p 2 1 + n 1 n 2 ☞ let t 1 = eval p 1 p 1 n 3 t 1 p 2
☞ Spread Sheet Evaluator n 1 n 2 set : cell x formula → unit 1 1 1 2 eval : cell → (int thunk) n 3 p 1 1 1 + 3 display : (int thunk) → unit “User interface” (REPL) p 2 1 + n 1 n 2 ☞ let t 1 = eval p 1 ☞ let t 2 = eval p 2 p 1 n 3 t 1 p 2 t 2
☞ Spread Sheet Evaluator n 1 n 2 set : cell x formula → unit 1 1 1 2 eval : cell → (int thunk) n 3 p 1 1 1 + 3 display : (int thunk) → unit get “User interface” (REPL) p 2 1 + ☞ let t 1 = eval p 1 n 1 n 2 3 force ☞ let t 2 = eval p 2 p 1 n 3 ☞ display t 1 t 1 demand! p 2 t 2
☞ Spread Sheet Evaluator n 1 n 2 set : cell x formula → unit 1 1 1 2 eval : cell → (int thunk) n 3 p 1 1 1 + 3 display : (int thunk) → unit get “User interface” (REPL) p 2 1 + ☞ let t 1 = eval p 1 n 1 n 2 3 force ☞ let t 2 = eval p 2 p 1 n 3 Demanded ☞ display t 1 t 1 Computation p 2 Graph (DCG) t 2
☞ Spread Sheet Evaluator n 1 n 2 set : cell x formula → unit 1 1 1 2 eval : cell → (int thunk) n 3 p 1 1 1 + 3 display : (int thunk) → unit “User interface” (REPL) p 2 1 + ☞ let t 1 = eval p 1 n 1 n 2 get ☞ let t 2 = eval p 2 6 ☞ display t 1 p 1 n 3 t 1 ☞ display t 2 force p 2 demand! t 2 DCG
☞ Spread Sheet Evaluator n 1 n 2 set : cell x formula → unit 1 1 1 2 eval : cell → (int thunk) n 3 p 1 1 1 + 3 display : (int thunk) → unit Memo “User interface” (REPL) match ! p 2 1 + ☞ let t 1 = eval p 1 n 1 n 2 get ☞ let t 2 = eval p 2 Memo 6 ☞ display t 1 p 1 n 3 match ! t 1 ☞ display t 2 force Sharing p 2 t 2 DCG
Spread Sheet Evaluator n 1 n 2 set : cell x formula → unit 1 1 1 2 eval : cell → (int thunk) n 3 p 1 1 1 + 3 display : (int thunk) → unit “User interface” (REPL) p 2 1 + ☞ let t 1 = eval p 1 n 1 n 2 get ☞ let t 2 = eval p 2 6 ☞ display t 1 p 1 n 3 t 1 ☞ display t 2 force p 2 ☞ clear t 2 DCG
☞ Spread Sheet Evaluator n 1 n 2 set : cell x formula → unit 1 1 1 2 eval : cell → (int thunk) n 3 p 1 1 1 + 3 display : (int thunk) → unit “User interface” (REPL) p 2 1 + n 1 n 2 get p 1 n 3 t 1 force p 2 t 2 DCG
Spread Sheet Evaluator n 1 n 2 set : cell x formula → unit 1 1 1 2 eval : cell → (int thunk) n 3 p 1 1 1 + 3 display : (int thunk) → unit “User interface” (REPL) p 2 1 + n 1 n 2 get ☞ set n 1 ← Leaf 5 p 1 n 3 t 1 force p 2 t 2 DCG
☞ Spread Sheet Evaluator n 1 n 2 set : cell x formula → unit 1 1 5 2 eval : cell → (int thunk) n 3 p 1 Dirty 1 1 + 3 display : (int thunk) → unit dep “User interface” (REPL) p 2 1 + n 1 n 2 Dirty get ☞ set n 1 ← Leaf 5 phase p 1 n 3 t 1 force p 2 t 2 DCG
Recommend
More recommend