190 likes | 308 Vues
This work by Greg Sullivan delves into dynamic partial evaluation (DPE) within the context of metaobject protocols. Unlike Java reflection, DPE offers a more dynamic approach that focuses on runtime optimization. It emphasizes data gathering as the code executes and adopts an optimistic approach to optimization. Through examples and machine concepts, the paper illustrates the mechanics of runtime optimization, generic functions, and multiple dispatch with a practical emphasis on efficient method handling in reflective environments. It raises key questions about performance and flexibility in programming languages.
E N D
Dynamic Partial Evaluation Gregory T. Sullivan MIT AI Lab gregs@ai.mit.edu
The 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? Dynamic Partial Evaluation - Greg Sullivan - NEPLS - December 7, 2000
The 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: “Don’t Model -- Observe.” Dynamic Partial Evaluation - Greg Sullivan - NEPLS - December 7, 2000
eq(GF42) T T l1=find-applicable-methods(foo, x, y); foo1(Point, Point), foo2(Line, Line), foo3(ColorPoint, ColorPoint) l2=generic-methods(foo); eq(l2) instance?(x, Point); instance?(x, Line); instance?(x, ColorPoint); true ,false ,true Bool Bool Bool instance?(y, Point); instance?(y, Line); instance?(y, ColorPoint); true ,false ,true Bool Bool Bool foo1(Point, Point), foo3(ColorPoint, ColorPoint) l1= list(fun) l3=sort-applicable-methods(l1) foo3(ColorPoint, ColorPoint), foo1(Point, Point), list(fun) f=first(l3) foo3(ColorPoint, ColorPoint) fun direct-call(f, x, y); Ex: Method Dispatch With a MOP Env = fooGF42 , xCP101 , yCP302 dyn-call(foo, x, y); Dynamic Partial Evaluation - Greg Sullivan - NEPLS - December 7, 2000
Example, Continued Env = fooGF42 , xCP101 , yCP302 eq(GF42) ColorPoint ColorPoint dyn-call(foo, x, y); l1=find-applicable-methods(foo, x, y); foo1(Point, Point), foo2(Line, Line), foo3(ColorPoint, ColorPoint) l2=generic-methods(foo); eq(l2) instance?(x, Point); instance?(x, Line); instance?(x, ColorPoint); true ,false ,true eq(true) eq(false) eq(true) instance?(y, Point); instance?(y, Line); instance?(y, ColorPoint); true ,false ,true eq(true) eq(false) eq(true) foo1(Point, Point), foo3(ColorPoint, ColorPoint) l1= eq(l1) l3=sort-applicable-methods(l2) foo3(ColorPoint, ColorPoint), foo1(Point, Point), eq(l3) f=first(l3) foo3(ColorPoint, ColorPoint) eq(foo3) direct-call(f, x, y); Dynamic Partial Evaluation - Greg Sullivan - NEPLS - December 7, 2000
Example, Continued Two Scenarios • Straight Optimization fun bar (x : ColorPoint, y : ColorPoint) dyn-call(foo, x, y); rewrites to fun bar (x : ColorPoint, y : ColorPoint) direct-call(foo3, x, y); // or inline • Profile-based method customization fun bar (x : Object, y : Object) dyn-call(foo, x, y); adds a new method add-fun bar (x : ColorPoint, y : ColorPoint) direct-call(foo3, x, y); // or inline Dynamic Partial Evaluation - Greg Sullivan - NEPLS - December 7, 2000
Machinery • 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. Dynamic Partial Evaluation - Greg Sullivan - NEPLS - December 7, 2000
Context • Dynamic Virtual Machine (DVM) • Multiple dispatch, dynamically typed, subtypes, highly reflective, predicate types. • “Native” language DVML: Scheme plus types, generic functions, cells Caveats • Only a prototype so far. Paper • DVM, a dynamically-typed lambda-calculus with generic functions and subtyping. Dynamic Partial Evaluation - Greg Sullivan - NEPLS - December 7, 2000
Dynamic 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. Dynamic Partial Evaluation - Greg Sullivan - NEPLS - December 7, 2000
Conditional Expressions { if e0 then e1 else e2 }, e0 V0 [v0, e’0, t0],if true?(v0) and e1 V [v, e’1, t], • If v0 is fully static, eliminate the if & return V. • If v0 is not static, must rebuild conditional: return [v, {if e’0 then e’1 else e2}, T]. No info. about type. • Likewise for false and e2. * * Dynamic Partial Evaluation - Greg Sullivan - NEPLS - December 7, 2000
Some Details • This amounts to inlining, and is done heuristically. • Must handle conflicting bindings, closed-over variables. • Primitive functions handled specially. Call expressions * {call e0 e1 ... en}, ei Vi [vi, e’i, ti], v0 = (xi : txi . ef):tf, check!(vi, txi) ef[xi[vi, e’i,glb(ti, txi)]] V [v, e’, t] * • If V0 not static, return [v, {call e’0e’1 ... e’n}, T]. • If V0 static, may return [v, e’, glb(t,tf)]. Dynamic Partial Evaluation - Greg Sullivan - NEPLS - December 7, 2000
Generic Function Call {gf-call e0 e1 ... en}, ei Vi [vi, e’i, 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-types[i] else glb(ti, txi) ef[xi[vi, e’i,arg-typei]] V [v, e, t] selection relies only on static types? • if spec?, add-method(v0, (xi : spec-types[i] . e):tf) • if V0 static and static?, fold as in non-generic function call. • if V0 static and not static?, [v, {gf-call e’0...e’n}, restype(v0)] Dynamic Partial Evaluation - Greg Sullivan - NEPLS - December 7, 2000
Swept Under the Rug Earlier dyn-call(foo,x,y) direct-call(foo3,x,y) • Depends on foo’s method set staying constant. Also, other elements of MOP. • Runtime PE must track dependencies on generic function method sets and cells. Dynamic Partial Evaluation - Greg Sullivan - NEPLS - December 7, 2000
Ex: Reflection in Java • From Braux & Noyé, PEPM00 public static void dumpFields(Object anObj) throws java.lang.IllegalAccessException { Field[] fields = anObj.getClass().getFields(); for (int i = 0; i < fields.length; i++) System.out.println(fields[i].getName()+“: “ +fields[i].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); } Dynamic Partial Evaluation - Greg Sullivan - NEPLS - December 7, 2000
MOP Dynamic PE -- What It’s Best At • Collapsing indirection. • Removing dynamic type checks. gf-call(g, v) call(f, v) + dependencies call(f, v) Dynamic Partial Evaluation - Greg Sullivan - NEPLS - December 7, 2000
Future Work • Tuning • when to specialize? • minimize overhead, gauge performance. • Gotchas: • customization introducing ambiguity • mutual recursion/specialization • Applied at machine code level? Dynamic Partial Evaluation - Greg Sullivan - NEPLS - December 7, 2000
The End • http://www.ai.mit.edu/~gregs/dynamic-pe.html Dynamic Partial Evaluation - Greg Sullivan - NEPLS - December 7, 2000
Harnessing 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. Dynamic Partial Evaluation - Greg Sullivan - NEPLS - December 7, 2000