1 / 14

Multi-Dispatch in the Java ™ Virtual Machine

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 :

Télécharger la présentation

Multi-Dispatch in the Java ™ Virtual Machine

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Multi-Dispatch in the Java™ Virtual Machine Design, Implementation, and Evaluation Christopher Dutchyn Computing Science University of Alberta Multi-Dispatch Java

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. Multi-Swing Results Multi-Dispatch Java

  14. 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

More Related