Optimizing object oriented programs Java case study

wakecabbagepatchΛογισμικό & κατασκευή λογ/κού

18 Νοε 2013 (πριν από 3 χρόνια και 10 μήνες)

159 εμφανίσεις


Index


1. Abstract


2. Object
-
oriented programming


3. Dynamic Dispatch


4. Code optimization


5. Java Virtual Machine


6. Conclusions


1. Abstract


OOP languages:


provide many software engineering benefits but with
a performance cost


make extensive use of method invocations and
pointer dereferences


when optimizing compilers for OOP languages one
can apply techniques like:







static class analysis







method specialization







conversion of dynamic method






calls to static method
-
instance






calls







profile
-
quided receiver class




prediction


2. Object
-
oriented programming


consists of designing a set of objects that
model the problem and it’s supposed to make
the design of a program more natural


supports reusable, extensible class libraries
and frameworks by using inheritance and
dynamically
-
bound messages (

performance
overhead)



the cost of message passing can be too great,
forcing the programmer to avoid OOP in the
“hot spots” of their application (
eg
: Java vs. C
arrays of characters)

3. Dynamic Dispatch




the process of mapping a message to a
specific sequence of code at runtime




done to support the cases where the
appropriate method cannot be determined at
compile
-
time (statically)



needed when multiple classes contain
different implementations of the same method
foo(). If the class of an object x is not known at
compile
-
time, then when x.foo() is called, the
program must decide at runtime which
implementation of foo() to invoke, based on
the runtime type of object x

4. Code optimization


compilers can reduce the cost of
dynamically
-
dispatched messages in a
number of ways:

1.
static class analysis

2.
profile
-
guided receiver class prediction

3.
method specialization

4.
class hierarchy analysis

5.
conversion of dynamic method calls to static
method
-
instance calls

6.
optimizations at link
-
time


4
.1 Static class analysis


identifies a superset of the set of possible
classes of objects that can be stored in
variables and returned from expressions


determines that the receiver of a message
can be an instance of only one class, allowing
the dynamically
-
dispatched message to be
replaced with a direct procedure call (i.e.,
statically
-
bound
) at compile
-
time and further
optimized using inline expansion if the target
procedure is small

4
.2 Profile
-
guided receiver
class prediction


supports a type
-
casing
-
style
optimization where static analysis is
unable to determine precise information
about the receiver of a message

4
.3 Method specialization


produces faster specialized versions of
a method for particular inheriting
subclasses;



each specialized version can be
optimized for the particular class or
classes of the receiver for which the
method is being specialized.


4.4 Class hierarchy analysis



another idea for speeding messages


when the compiler compiles a method, it knows
statically that the receiver of the method is some
subclass
S

of the class
C
containing the
method. Unfortunately, in the absence of
additional information, the compiler cannot
optimize messages sent to the method’s
receiver, because the subclass
S
may override
any of C’s dynamically
-
dispatched methods


resolves this dilemma by supplying the compiler
with complete knowledge of the program’s class
inheritance graph and the set of methods
defined on each class

4
.5 Conversion of dynamic method
calls to static method
-
instance
calls


compared with an ordinary function call, at each
method call site there is a dynamic method lookup
to determine the method instance


multiple
-
inheritance and class languages increase
significantly the dynamic method
-
lookup costs


where a method call is always calling the same
method instance, compilers for OOP languages do
global program analysis to determine those places;
then the dynamic method call can be replaced by a
static function call


4.6 Optimizations at link
-
time


an alternative to performing whole
-
program
optimization


similar to class hierarchy analysis


advantages: optimizations operate on
machine code (they can be applied to the
whole program, including libraries for which
source code is unavailable)

5. Java Virtual Machine


defines an abstract machine or processor (an
instruction set, a set of registers, a stack, a
"garbage heap" and a method area)







platform independence



it can either
interpret the bytecode one





instruction at a time, mapping it to


a real processor instruction






compile the bytecode further for the




real processor using what is called




a just
-
in
-
time compiler



The Java HotSpot Compilers



JIT compilers


fast
traditional compilers that
translate the Java technology bytecodes into
native machine code on
-
the
-
fly


(problem: runs on the execution machine in “user time”)


JVM Compiler focuses on object
-
oriented
optimizations like:

1.
Deep inlining

2.
Class hierarchy analysis

3.
Virtual call inlining

4.
Dynamic deoptimization


Inlining



has important benefits


It dramatically reduces the dynamic
frequency of method invocations


is synergistic with other code optimizations
because it makes them more effective


as the Java HotSpot compiler matures, the
ability to operate on large, inlined blocks of
code will open the door to a host of even
more advanced optimizations in the future


6
.
Conclusions


Certain features of the object
-
oriented paradigm are a serious impediment
for the runtime performance of object
-
oriented programs. Although
compiler techniques to alleviate this problem were developed over the
years, there are some real
-
world examples which show that these
solutions fall short in making any significant optimizations to systems that
are required to be very flexible and highly reusable.


In this presentation we showed some of the techniques developed
recently in order to alleviate the e

ciency problem of OOP languages.
The primary focus of all these techniques lies in trying to avoid the
generation of expensive dynamic dispatch code. This is achieved by
statically binding as much message sends as possible. An important
observation that can be made is that no one technique is able to solve the
problem on its own. Depending on the situation and the context of a
message send, one certain technique is better suited than another.




Thank you for your attention!






Questions?