Multi-Dispatch in the Java ™ Virtual Machine

14
07/20/22 Multi-Dispatch Java 1 Multi-Dispatch in the Java™ Virtual Machine Design, Implementation, and Evaluation Christopher Dutchyn Computing Science University of Alberta

description

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 : - PowerPoint PPT Presentation

Transcript of Multi-Dispatch in the Java ™ Virtual Machine

Page 1: Multi-Dispatch in the Java ™  Virtual Machine

04/19/23 Multi-Dispatch Java 1

Multi-Dispatch in the Java™ Virtual Machine

Design, Implementation, and Evaluation

Christopher Dutchyn

Computing Science

University of Alberta

Page 2: Multi-Dispatch in the Java ™  Virtual Machine

04/19/23 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

}.draw(Shape)

Color

HPJet

PScript

Page 3: Multi-Dispatch in the Java ™  Virtual Machine

04/19/23 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

}.print

Color

HPJet

PScript ( )Circle

Ellipse

Square

Rectangle

Page 4: Multi-Dispatch in the Java ™  Virtual Machine

04/19/23 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

Page 5: Multi-Dispatch in the Java ™  Virtual Machine

04/19/23 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.

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

Page 6: Multi-Dispatch in the Java ™  Virtual Machine

04/19/23 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.

Page 7: Multi-Dispatch in the Java ™  Virtual Machine

04/19/23 Multi-Dispatch Java 7

Multi-Dispatch At a Call Site

1. Look up the uni-dispatch method

2. If multi-dispatchablea) Walk the operand stack to obtain precise types

HPJet and Circle instead of Printer and Shapeb) Select the method that most closely matches the

argument types:

c) Verify return types conform

3. Invoke new method

HPJet.print(Circle)

Page 8: Multi-Dispatch in the Java ™  Virtual Machine

04/19/23 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?

Page 9: Multi-Dispatch in the Java ™  Virtual Machine

04/19/23 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.

Page 10: Multi-Dispatch in the Java ™  Virtual Machine

04/19/23 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

0

10

20

30

40

50

Tim

es (

s)

[lo

wer

is b

ette

r]

Compile Times (s)

sun.tools Compile Times UNI

MSA-MI

MSA-INL

SRP-MI

SRP-INL

0

0.005

0.01

0.015

0.02

Tim

es (

us)

[l

ow

er is

bet

ter]

Uni-dispatch TimingUNI

MSA-MI

MSA-INL

SRP-MI

SRP-INL

Page 11: Multi-Dispatch in the Java ™  Virtual Machine

04/19/23 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)

0

0.1

0.2

0.3

0.4

0.5

0.6

Tim

e (u

s) [

low

er is

bet

ter]

TypeCase Kernel Typefield Visitor SRP

Dispatcher

Double vs. Multi-Dispatch

Double

Multi-

Page 12: Multi-Dispatch in the Java ™  Virtual Machine

04/19/23 Multi-Dispatch Java 12

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

Page 13: Multi-Dispatch in the Java ™  Virtual Machine

04/19/23 Multi-Dispatch Java 13

Multi-Swing Results

Execution Times

0.00

2.00

4.00

6.00

8.00

10.00

12.00

14.00

16.00

Swing Multi-Swing

Sec

onds

[low

er is

bet

ter]

Dispatches

0.00

2.00

4.00

6.00

8.00

10.00

12.00

14.00

16.00

18.00

20.00

22.00

24.00

26.00

28.00

30.00

32.00

34.00

Swing Multi-Swing

Dis

pat

ches

(mill

ion

s)

Multi-dispatches

Uni-dispatches

Page 14: Multi-Dispatch in the Java ™  Virtual Machine

04/19/23 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.