140 likes | 265 Views
Multi-Dispatch in the Java ™ Virtual Machine. Design, Implementation, and Evaluation. Christopher Dutchyn Computing Science University of Alberta. The Problem. Production OO languages ( C++ , Java) select the method to run based on the dynamic type of a single argument only :
E N D
Multi-Dispatch in the Java™ Virtual Machine Design, Implementation, and Evaluation Christopher Dutchyn Computing Science University of Alberta Multi-Dispatch Java
The Problem • Production OO languages (C++, Java) select the method to run based on the dynamic type of a single argument only: • We call this uni-dispatch } Color HPJet PScript .draw(Shape) Multi-Dispatch Java
The Goal • We want method selection based on the dynamic types of more than one, and possibly all of the arguments: • We call this multiple dispatch ( ) } Circle Ellipse Square Rectangle Color HPJet PScript .print Multi-Dispatch Java
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 Multi-Dispatch Java
Another Solution • Perform a single dynamic dispatch operation that considers the dynamic types of all of the arguments • We call this multi-dispatch. Multi-Dispatch Java
Research Contributions • Added dynamic multi-dispatch to Java • without changing syntax or compiler, • allowing programmer to select classes supporting multi-dispatch, • 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. Multi-Dispatch Java
Multi-Dispatch At a Call Site • Look up the uni-dispatch method • If multi-dispatchable • Walk the operand stack to obtain precise types HPJet and Circle instead of Printer and Shape • Select the method that most closely matches the argument types: • Verify return types conform • Invoke new method HPJet.print(Circle) Multi-Dispatch Java
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? Multi-Dispatch Java
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. Multi-Dispatch Java
Uni-Dispatch Overhead • individual uni-dispatch times • multi-invoker adds zero • Inlined tests adds 2.5% • sun.tools.* compile • multi-dispatch adds 2-3% • cache contention due to larger data structures • class load overhead exaggerated by repeated loading Multi-Dispatch Java
Double vs. Multi-Dispatch • Using existing event processing kernel from sun.awt.component • 7 event types • 7 components • Comparison of • original kernel • typecases • type fields • Visitor • multi-dispatch (SRP) Multi-Dispatch Java
Multi-Swing (and AWT) • Modified 92 of 846 classes (11%) • Replaced 171 conditionals (5%) • Mean number of decision points reduced from 3.8 to 2.0 per method • Added 57 new event subclasses • Added 123 new multimethods Multi-Dispatch Java
Multi-Swing Results Multi-Dispatch Java
Research Contributions • Added dynamic multi-dispatch to Java • without changing syntax or compiler, • allowing programmer to select classes supporting multi-dispatch, • 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. Multi-Dispatch Java