Presentation is loading. Please wait.

Presentation is loading. Please wait.

Multi-Dispatch in the Java™ Virtual Machine

Similar presentations


Presentation on theme: "Multi-Dispatch in the Java™ Virtual Machine"— Presentation transcript:

1 Multi-Dispatch in the Java™ Virtual Machine
Design, Implementation, and Evaluation Christopher Dutchyn Computing Science University of Alberta 11/8/2018 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) The process of selecting code based on types is called “dispatch”. When the compile-time type is used, we call it “static dispatch”, when the execution-time type is used, we call it “dynamic dispatch”. 11/8/2018 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 We need multiple dispatch. It appears in many situations: binary operations: equals, compareTo graphical user interfaces: components & events drag and drop: document type & target cut and paste: clip type & target application 11/8/2018 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 They encode the type hierarchy of the program into many different locations in the program, resulting in brittle, difficult to maintain, and error-prone code. 11/8/2018 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. The code is cleaner; the language runtime is responsible for executing the correct implementation. 11/8/2018 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. 11/8/2018 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 We have to wait until uni-dispatch because (a) we need to know the number of arguments and the size of each one, and (b) we want to ensure that if a subclass implements VMD but superclass does not, then the subclass method invocations still are multi-dispatched. HPJet.print(Circle) 11/8/2018 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? 11/8/2018 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 classes comprising the Java Class Libraries. Our multi-dispatch JVMs host those compilations. 11/8/2018 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 11/8/2018 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) 11/8/2018 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 Working with this real-world application reinforced 1) some of the issues regarding null parameters, return types, visibility, and type promotion. 11/8/2018 Multi-Dispatch Java

13 Multi-Swing Results The multi-dispatch version executes 8% fewer dispatches and the multi-dispatches represent approximately 7.7% of the dispatches. Total execution time is 0.3 seconds shorter (2%). 11/8/2018 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. 11/8/2018 Multi-Dispatch Java


Download ppt "Multi-Dispatch in the Java™ Virtual Machine"

Similar presentations


Ads by Google