Multi-Dispatch in the Java Virtual Machine: Design and Implementation

Multi-Dispatch in the Java Virtual Machine: Design and Implementation

6th USENIX Conference on Object-Oriented Technologies, January 29-February 2, 2001, San Antonio, Texas, U.S.A. (Minor corrections made February 5, 2001) Multi-Dispatch in the Java Virtual Machine: Design and Implementation ? ? ? } Christopher Dutchyn? Paul Lu Duane Szafron Steven Bromling Wade Holst ? Dept. of Computing Science } Dept. of Computer Science University of Alberta The University of Western Ontario Edmonton, Alberta, Canada, T6G 2E8 London, Ontario, Canada, N6A 5B7 g fdutchyn,paullu,duane,bromling @cs.ualberta.ca [email protected] Abstract selection based upon the types of the arguments. This method selection process is known as dispatch. It can Mainstream object-oriented languages, such as C++ occur at compile-time or at execution-time. In the for- and Java1, provide only a restricted form of polymor- mer case, where only the static type information is phic methods, namely uni-receiver dispatch. In com- available, we have static dispatch (method overload- mon programming situations, developers must work ing). The latter case is known as dynamic dispatch around this limitation. We describe how to extend the (dynamic method overriding or virtual functions) and Java Virtual Machine to support multi-dispatch and ex- object-oriented languages leverage it to provide poly- amine the complications that Java imposes on multi- morphism — the execution of type-specific program dispatch in practice. Our technique avoids changes to code. the Java programming language itself, maintains source We can divide OO languages into two broad categories code and library compatibility, and isolates the perfor- based upon how many arguments are considered dur- mance penalty and semantic changes of multi-method ing dispatch. Uni-dispatch languages select a method dispatch to the program sections which use it. We have based upon the type of one distinguished argument; micro-benchmark and application-level performance re- multi-dispatch languages consider more than one, and sults for a dynamic Most Specific Applicable (MSA) potentially all, of the arguments at dispatch time. For dispatcher, a framework-based Single Receiver Projec- example, Smalltalk [14] is a uni-dispatch language. tions (SRP) dispatcher, and a tuned SRP dispatcher. Our CLOS [23] and Cecil [6] are multi-dispatch languages. general-purpose technique provides smaller dispatch la- Other terms, like multiple dispatch, are used in the liter- tency than programmer-written double-dispatch code ature. However, the term multiple dispatch is confusing with equivalent functionality. since it can mean either successive uni-dispatches or a single multi-dispatch. In fact, in this paper, we compare 1 Introduction multi-dispatch to double dispatch, which uses two uni- dispatches. Object-oriented (OO) languages provide powerful tools for expressing computations. One key abstraction is the C++ [24] and Java [15] are dynamic uni-dispatch lan- concept of a type hierarchy which describes the relation- guages. However, for both languages, the compiler ships among types. Objects represent instances of these considers the static types of all arguments when com- different types. Most existing object-oriented languages piling method invocations. Therefore, we can regard require each object variable to have a programmer- these languages as supporting static multi-dispatch. Fig- assigned static type. The compiler uses this information ure 1 depicts both dynamic uni-dispatch and static multi- to recognize some coding errors. The principle of sub- dispatch in Java. stitutability mandates that in any location where type T Uni-dispatch limits the method selection process to con- is expected, any sub-type of T is acceptable. But, substi- sider only a single argument, usually the receiver. This tutability allows that object variable to have a different is a substantial limitation and standard programming id- (but related) dynamic type at runtime. ioms exist to overcome this restriction. As a motivation Another key facility found in OO languages is method for multi-dispatch, we describe one programming idiom that demonstrates the need for multi-dispatch, describe 1Java is a trademark of Sun Microsystems, Inc. Object class Point f the type of the argument before continuing to int x, y; perform a type-specific comparison. Another common g void draw(Canvas c) f // Point-specific code use for double dispatch is in drag-and-drop applications, g void translate(int t) fx+=t; y+=t; where the result of a user action depends on both the data g void translate(int tX,int tY) fx+=tX; y+=tY; g object dragged and on the target object. A generic drag- and-drop schema forces the programmer to test data class ColorPoint extends Point f Color c; types and re-dispatch to a more specific method. A third g void draw(Canvas C) f // ColorPoint code example is in event-driven programming. As we saw g in Figure 2, applications are written using base classes // same static type, different dynamic types such as Component and Event, but we need to take ac- Point Pp = new Point(); Point Pc = new ColorPoint(); tion based upon the specific types of both Component and Event. Indeed, the need for multi-dispatch is ubiq- // static multi-dispatch Pp.translate(5); // one int version uitous enough that two of the original design patterns, Pp.translate(1,2); // two int version Visitor and Strategy, are work-arounds to supply multi- // dynamic uni-dispatch dispatch functionality within uni-dispatch languages. Pp.draw(aCanvas); // Point::draw() Pc.draw(aCanvas); // ColorPoint::draw() Consider how the AWT example could be re-written if dynamic multi-dispatch was available in Java. An equiv- Figure 1: Dispatch Techniques in Java alent program, partially using multi-dispatch, would re- semble Figure 2(b). For clarity, we have not completely converted the code to use multi-dispatch; we maintain how it can be replaced by multi-dispatch, list the ad- the case statement and double dispatch to select among vantages of using multi-dispatch to replace the idiomatic MouseEvent categories. A more complete factoring code, and measure the cost of using multi-dispatch with of MouseEvent into MouseButtonEvent and Mouse- one of our current multi-dispatch algorithms. MotionEvent would eliminate the remaining double dispatch, resulting in a Full Multi-Dispatch version of 1.1 Double Dispatch the code. The dynamic multi-dispatcher will select the correct method at runtime based upon the dispatchable Double dispatch occurs when a method explicitly checks arguments in addition to the receiver argument (the in- an argument type and executes different code as a re- stance of Component). Individual component types can sult of this check. Double dispatch is illustrated in Fig- still override the methods that accept specific event types ure 2(a) (from Sun’s AWT classes) where the process- (e.g. KeyEvent, FocusEvent) and will do so without Event(AWTEvent) method must process events in dif- invoking the double-dispatch code. ferent ways, since event objects are instances of differ- The multi-dispatch version is shorter and clearer. ent classes. Since all of the events are placed in a queue However, it requires the Java Virtual Machine whose static element type is AWTEvent, the compiler (JVM) [20] to directly dispatch an Event to the loses the more specific dynamic type information. When correct processEvent(AWTEvent) method. Our an element is removed from the queue for processing, its modified JVM provides this facility and correctly dynamic type must be explicitly checked to pick the ap- executes the multi-dispatch code discussed above. propriate action. This is an example of the well-known Furthermore, Table 1, a subset of Table 4, shows that container problem [5]. multi-dispatch is substantially faster than interpreted Double dispatch suffers from a number of disadvan- double dispatch and even faster than JIT-ed double tages. First, double dispatch has the overhead of in- dispatch. Note that the numbers in Table 1 are based on voking a second method. Second, the double-dispatch single-threaded code. program is longer and more complex; this provides more opportunity for coding errors. Third, the double- Our experience with the Swing GUI classes [26] rein- dispatch program is more difficult to maintain since forces our belief that double dispatch in AWT is a sig- adding a new event type requires not only the code to nificant factor in Swing applications. First, Swing does handle the new event, but another cascaded else if not operate without AWT; instead each AWTEvent is statement. accepted by a Swing JComponent. Therefore, every mouse-click and key-press is double dispatched through The need for double dispatch develops naturally in sev- AWT into Swing. Next, Swing type-checks the event eral common situations. Consider binary operations [4], and double dispatches again. Internally, Swing avoids such as the compareTo(Object) method defined in in- further double dispatch by coding the AWTEvent type terface Comparable. The programmer must ascertain package java.awt; package java.awt; f class Component f class Component g // double dispatch events to subComponent void processEvent(AWTEvent e) f... void processEvent(AWTEvent e) f if (e instanceof FocusEvent) f processFocusEvent((FocusEvent)e); f f g else if (e instanceof MouseEvent) void processEvent(MouseEvent e) f switch (e.getID()) f switch (e.getID()) case MouseEvent.MOUSE PRESSED: case MouseEvent.MOUSE PRESSED: ... ... case MouseEvent.MOUSE EXITED: case MouseEvent.MOUSE EXITED: processMouseEvent((MouseEvent)e); processMouseEvent((MouseEvent)e); break; break; case MouseEvent.MOUSE MOVED: case MouseEvent.MOUSE MOVED: case MouseEvent.MOUSE DRAGGED: case MouseEvent.MOUSE DRAGGED: processMouseMotionEvent((MouseEvent)e); processMouseMotionEvent((MouseEvent)e); break; break; g g g g else if (e instanceof KeyEvent) f processKeyEvent((KeyEvent)e); f g else if (e instanceof ComponentEvent) processComponentEvent((ComponentEvent)e); f g else if (e instanceof InputMethodEvent) processInputMethodEvent((InputMethodEvent)e); g // other events ignored by Component g g f g void processFocusEvent(FocusEvent e) f... void processEvent(FocusEvent e) ... g f g void processMouseEvent(MouseEvent e) f..

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    16 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us