Title: Dynamic Partial Evaluation
1Dynamic Partial Evaluation
- Gregory T. Sullivan
- MIT AI Lab
- gregs_at_ai.mit.edu
2The Challenge
- Metaobject protocols - reflection plus dynamism
(as opposed to Java reflection). - MOP ? More indirection, less optimization (ouch!)
- How to optimize when, in principle, everything
may change?
3The Solution
- Optimize at Runtime.
- Optimize Optimistically.
- JIT approach traditional - (re)analyze,
regenerate - analysis walks the code.
- Dynamic PE gather data as code is executed
- execution walks the code.
- Motto Dont Model -- Observe.
4Ex Method Dispatch With a MOP
Env foo?GF42 , x?CP101 , y?CP302
dyn-call(foo, x, y)
5Example, Continued
Env foo?GF42 , x?CP101 , y?CP302
eq(GF42)
ColorPoint
ColorPoint
dyn-call(foo, x, y)
l1find-applicable-methods(foo, x, y)
foo1(Point, Point), foo2(Line,
Line), foo3(ColorPoint, ColorPoint)
l2generic-methods(foo)
eq(l2)
instance?(x, Point) instance?(x,
Line) instance?(x, ColorPoint)
true ,false ,true
eq(true)
eq(true)
eq(false)
true ,false ,true
instance?(y, Point) instance?(y,
Line) instance?(y, ColorPoint)
eq(true)
eq(false)
eq(true)
foo1(Point, Point), foo3(ColorPoint, ColorPoint)
l1
eq(l1)
l3sort-applicable-methods(l2)
foo3(ColorPoint, ColorPoint), foo1(Point, Point),
eq(l3)
ffirst(l3)
foo3(ColorPoint, ColorPoint)
eq(foo3)
direct-call(f, x, y)
6Example, Continued
Two Scenarios
- Straight Optimization
fun bar (x ColorPoint, y ColorPoint)
dyn-call(foo, x, y)
fun bar (x ColorPoint, y ColorPoint)
direct-call(foo3, x, y) // or inline
rewrites to
- Profile-based method customization
fun bar (x Object, y Object) dyn-call(foo,
x, y)
add-fun bar (x ColorPoint, y ColorPoint)
direct-call(foo3, x, y) // or inline
adds a new method
7Machinery
- Updating runtime with optimized methods.
- we use generic functions and multiple dispatch.
- Extended values regular value, expression,
type - environment maps ids to extended values.
- Eval(exp, env) ? value, exp, type
- (residual) expression represents in the current
environment, this expression will produce this
value. - the type encodes what we can assume when using
this value.
8Context
- Dynamic Virtual Machine (DVM)
- Multiple dispatch, dynamically typed, subtypes,
highly reflective, predicate types. - Native language DVML Scheme plus types,
generic functions, cells
Caveats
Paper
- ?DVM, a dynamically-typed lambda-calculus with
generic functions and subtyping.
9Dynamic PE for Expressions
- Literals n ? n, n , eq(n)
- Variable reference x if Env(x) ? V ? v, -,
eq(v), and v is expressible as a literal, return
v, v, eq(v)else return V.
10Conditional Expressions
- if e0 then e1 else e2 , e0 ? V0 ? v0, e0,
t0, if true?(v0) and e1 ? V ? v, e1, t, - If v0 is fully static, eliminate the if return
V. - If v0 is not static, must rebuild conditional
return v, if e0 then e1 else e2, T. No
info. about type. - Likewise for false and e2.
11Call expressions
- call e0 e1 ... en, ei ? Vi ? vi, ei, ti,
v0 (?xi txi . ef)tf, check!(vi, txi)
efxi?vi, ei,glb(ti, txi) ? V ? v, e, t
- If V0 not static, return v, call e0 e1 ...
en, T. - If V0 static, may return v, e, glb(t,tf).
12Generic Function Call
- gf-call e0 e1 ... en, ei ? Vi ? vi, ei, ti,
v0 f1,...,fm, find-mam(f1,...,fm,V1...Vn)
(?xi txi . ef)tf, static?(spec?,
spec-types) choose-spec(V0...Vn)arg-typei if
spec? spec-typesi else glb(ti, txi) efxi?vi,
ei,arg-typei ? V ? v, e, t
selection relies only on static types?
- if spec?, add-method(v0, (?xi spec-typesi .
e)tf) - if V0 static and static?, fold as in non-generic
function call. - if V0 static and not static?, v, gf-call
e0...en, restype(v0)
13Swept Under the Rug Earlier
dyn-call(foo,x,y) ? direct-call(foo3,x,y)
- Depends on foos method set staying constant.
Also, other elements of MOP. - Runtime PE must track dependencies on generic
function method sets and cells.
14Ex Reflection in Java
public static void dumpFields(Object anObj)
throws java.lang.IllegalAccessException
Field fields anObj.getClass().getFields()
for (int i 0 i lt fields.length i)
System.out.println(fieldsi.getName()
fieldsi.get(anObj))
specialized to Point class, should yield
public static void dumpFieldsPoint(Point anObj)
System.out.println(x anObj.x)
System.out.println(y anObj.y)
15Dynamic PE -- What Its Best At
- Collapsing indirection.
- Removing dynamic type checks.
gf-call(g, v)
?
call(f, v) dependencies
call(f, v)
16Future Work
- Tuning
- when to specialize?
- minimize overhead, gauge performance.
- Gotchas
- customization introducing ambiguity
- mutual recursion/specialization
- Applied at machine code level?
17The End
- http//www.ai.mit.edu/gregs/dynamic-pe.html
18Harnessing Dynamic PE
- Suppose foo(x Object)e called with x a
Point. - Run e with x ? -,-,Point and get v,e,t,
- Can define foo(x Point) e .
- But ...Where to put it? How to use it?
- Generic function contains a set of methods.
- Multiple dispatch chooses the most applicable
based on types of all arguments.