Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Multi-Dispatch in the Java™ Virtual Machine Design, Implementation, and Evaluation Christopher Dutchyn Computing Science University of Alberta 5/24/2017 Multi-Dispatch Java 1 The Problem • Production OO languages (C++, Java) select the method to run based on the dynamic type of a single argument only: } Color HPJet PScript .draw(Shape) • We call this uni-dispatch 5/24/2017 Multi-Dispatch Java 2 The Goal • We want method selection based on the dynamic types of more than one, and possibly all of the arguments: }( Color HPJet PScript .print ) Circle Ellipse Square Rectangle • We call this multiple dispatch 5/24/2017 Multi-Dispatch Java 3 Double Dispatch Solutions • Type fields – switch on constant integers – maintain types encoded as obscure numbers – risk that some type fields might be omitted • Typecases – if …instanceof …else if…else… – risk that some types might not be tested for – instanceof tests contain order dependencies • Visitor pattern – sequence of uni-dispatches – duplicates dispatcher code into many classes 5/24/2017 Multi-Dispatch Java 4 Another Solution • Perform a single dynamic dispatch operation that considers the dynamic types of all of the arguments • We call this multi-dispatch. 5/24/2017 interface Printer implements VirtualMultiDispatchable { void print(Shape s); } class Color implements Printer { void print(Shape s) {/*null*/} void print(Circle c) {/*.cc.*/} void print(Ellipse e){/*.ce.*/} void print(Rectangle r){/*cr*/} } class HPJet implements Printer { void print(Shape s) {/*null*/} void print(Circle c) {/*.hc.*/} void print(Ellipse e){/*.he.*/} void print(Rectangle r){/*hr*/} // similar for other Shapes } // similar for PScript Multi-Dispatch Java 5 Research Contributions • Added dynamic multi-dispatch to Java • without changing syntax or compiler, • allowing programmer to select classes supporting multidispatch, • without penalizing existing uni-dispatch, • and maintaining reflection and existing APIs. • Realizes the following benefits • shorter, simpler, and more maintainable code, • with equal or better performance than complex, error-prone double dispatch. • Published COOTS 2001 • Best student paper award. 5/24/2017 Multi-Dispatch Java 6 Multi-Dispatch At a Call Site 1. Look up the uni-dispatch method 2. If multi-dispatchable a) Walk the operand stack to obtain precise types HPJet and Circle instead of Printer and Shape b) Select the method that most closely matches the argument types: HPJet.print(Circle) c) Verify return types conform 3. Invoke new method 5/24/2017 Multi-Dispatch Java 7 Evaluation: Two Criteria • Compatibility with uni-dispatch – Do existing uni-dispatch Java programs continue to run correctly? – What performance penalty does our additions impose on pure uni-dispatch programs? • Performance versus double dispatch – How does multi-dispatch compare with existing double dispatch techniques? – Does multi-dispatch scale to full applications? 5/24/2017 Multi-Dispatch Java 8 Uni-Dispatch Java Support • The java compiler, javac, is a large Java program that runs over a Java Virtual Machine. • As part of constructing the JDK, javac runs on the just-constructed JVM to compile 5000+ classes comprising the Java Class Libraries. • Our multi-dispatch JVMs host those compilations. 5/24/2017 Multi-Dispatch Java 9 Uni-Dispatch Overhead Uni-dispatch Timing – multi-invoker adds zero – Inlined tests adds 2.5% • sun.tools.* MSA-MI 0.02 Times (us) [lower is better] • individual uni-dispatch times MSA-INL SRP-MI 0.015 SRP-INL 0.01 0.005 0 compile sun.tools Compile Times UNI MSA-MI • cache contention due to larger data structures • class load overhead exaggerated by repeated loading Times (s) [lower is better] – multi-dispatch adds 2-3% 5/24/2017 UNI Multi-Dispatch Java 50 MSA-INL 40 SRP-MI SRP-INL 30 20 10 0 Com pile Tim es (s) 10 Double vs. Multi-Dispatch Double vs. Multi-Dispatch • Using existing event processing kernel from 0.6 sun.awt.component • Comparison of – – – – – 5/24/2017 original kernel typecases type fields Visitor multi-dispatch (SRP) 0.5 Time (us) [lower is better] – 7 event types – 7 components Double Multi- 0.4 0.3 0.2 0.1 0 TypeCase Kernel Typefield Visitor SRP Dispatcher Multi-Dispatch Java 11 Multi-Swing (and AWT) 1. Modified 92 of 846 classes (11%) 2. Replaced 171 conditionals (5%) 3. Mean number of decision points reduced from 3.8 to 2.0 per method 4. Added 57 new event subclasses 5. Added 123 new multimethods 5/24/2017 Multi-Dispatch Java 12 Multi-Swing Results Dispatches Execution Times 34.00 32.00 30.00 28.00 26.00 24.00 22.00 20.00 18.00 16.00 14.00 12.00 10.00 8.00 6.00 4.00 2.00 0.00 14.00 12.00 10.00 8.00 6.00 4.00 2.00 0.00 Swing 5/24/2017 16.00 Uni-dispatches Seconds [lower is better] Dispatches (millions) Multi-dispatches Swing Multi-Swing Multi-Dispatch Java Multi-Swing 13 Research Contributions • Added dynamic multi-dispatch to Java • without changing syntax or compiler, • allowing programmer to select classes supporting multidispatch, • without penalizing existing uni-dispatch, • and maintaining reflection and existing APIs. • Realizes the following benefits • shorter, simpler, and more maintainable code, • with equal or better performance than complex, error-prone double dispatch. • Published COOTS 2001 • Best student paper award. 5/24/2017 Multi-Dispatch Java 14