Sei sulla pagina 1di 6

Specialization of Object-Oriented

Programs Written in Java Language


Igor DYSKO
Slovak University of Technology
Faculty of Informatics and Information Technologies
Ilkoviova 3, 842 16 Bratislava, Slovakia
igor.dysko@gmail.com

Abstract. Design patterns offer many advantages in software


development, but introduce overheads into programs. These overheads
and inefficiency can be eliminated by specialization, which adapts a
program code to a specific use. In this paper, we show how to
eliminate some of overheads caused by virtual methods calls. We
propose the specialization of abstract factory and prototype design
patterns that are used concurrently and we discuss how aspects can be
used for virtual calls elimination. We have experimentally verified the
usage of designed specialization pattern.

1 Introduction
Using of design patterns in software development offers many advantages, such as
code reusability, easier program extensions or others. It supports development and
maintenance of software applications and systems. On the other side, design patterns
may introduce inefficiency into programs. It is possible to eliminate this disadvantage
by specializing the program, which adapts this program to a specific execution context
[5]. An application that performs specializations is an automatic specializer.
Specialization may optimize object-oriented programs, especially those ones that
are written according to design patterns. In object-oriented programs, overheads
usually result from genericness. Approach, which prefers using generic methods and
components, is in many cases at the expense of effectiveness. One of the factors that
influence program effectiveness is usage of virtual calls.
Specialization of a program is a transformation technique, which adapts a generic
program to a specific execution context. There are various specialization methods that

Supervisor: prof. Ing. Pavol Nvrat, PhD., Institute of Informatics and Software Engineering,
Faculty of Informatics and Information Technologies STU in Bratislava
M. Bielikov (Ed.), IIT.SRC 2005, April 27, 2005, pp. 127-132.

128

Igor Dysko

can be used for program specialization [1, 5, 8]. We will point our interest only to the
specialization technique named partial evaluation.
Execution of an object-oriented program is determined by a sequence of
interactions among objects. Further specification of program context makes it possible
to improve some interactions, so the static interactions will be evaluated in advance.
Specialization substitutes generic method calls by specialized methods calls.

2 Design Patterns
Design patterns represent the way of designing and implementation of a specific
problem. Design patterns were well explained and documented by Gamma et al. [3].
The authors divided all patterns into three categories - creational patterns, structural
patterns and behavioral patterns. Each of these groups focuses on different kind of
problems. The first one describes the design possibilities of creating new objects, the
second one arranges relationships among objects and the third one focuses on
generalizing of object behavior.
Using of design patterns enhances the process of software design and allows
simplified reusability and maintenance but in consequence of excessive generalization
it introduces overheads into programs. These overheads could rise from virtual method
calls, which may be in certain cases eliminated by specialization. In this paper, we will
show the basic principles of virtual calls elimination in the design patterns prototype
and abstract factory.

3 Abstract factory and prototype specialization


Abstract factory and prototype are creational patterns, which main task is to build new
objects. We show a specialization of prototype design pattern which is used
concurrently with abstract factory design pattern. The specialization of abstract factory
design pattern was presented by Dysko [2]. We will show how specialization may be
used when more design patterns are used concurrently. We use the same template for
specialization pattern as Dysko [2].
Name:
Prototype.
Description: In case the system has to be independent on the product creation, it is
suitable to use prototype.
Structure:
The structure of the prototype design pattern was presented by Gamma
et al. [3].
Area:
Specialization is focused on the part of the code where new objects are
being created by calling the method that returns an object clone.
Overhead:
Cloning methods present virtual calls that could be eliminated.
Approach:
Virtual calls of prototype clone method will be replaced by directly by
the method body.
Conditions: Concrete type of the prototype must be known.

Specialization of Object-Oriented Programs Written in Java Language

Example:

129

AbstractPrototype interface represents the common interface for


concrete prototypes.

public interface AbstractPrototype extends Cloneable{


public void enter();
public Object clone() throws
CloneNotSupportedException;
}

AbstractPrototype Interface defines two methods, enter and clone. Significant


method is the method clone that returns the copy of the object. Class PrototypeFactory
contains the set of prototypes and methods that return the clones of the prototypes.
public class PrototypeFactory {
private AbstractPrototype _prototype1;
...
public AbstractPrototype makePrototype1(){
try{
return (AbstractPrototype)_prototype1.clone();
}catch(CloneNotSupportedException e){
return null;
}
}
...
}

Methods of abstract factory that call clone methods are being called in a main
method of class Runner.
public class Runner {
private static PrototypeFactory _factory;
...
public static void main(String[] args) {
...
_floor = (Floor) _factory.makePrototype1();
_door = (Door) _factory.makePrototype2();
_wall = (Wall) _factory.makePrototype3();
_room = (Room) _factory.makePrototype4();
...
}
}

We will specialize the part of the code in class Runner where new objects are
being created.

130

Igor Dysko

All methods named makePrototypeX call method clone, which calls the
constructor of its class for the creation of a new object. Replacing the call of
makePrototypeX method, directly by the constructor call, eliminates two virtual calls.
We have replaced virtual method calls directly by the bodies of called methods.
After specialization the class Runner looks as follows:
public class Runner {
private static Door prototypeDoor = new Door();
private static Wall prototypeWall = new Wall();
private static Room prototypeRoom = new Room("office");
public static void main(String[] args) {
...
_floor = new Floor();
_door = new Door(prototypeDoor);
_wall = new Wall(prototypeWall);
_room = new Room(prototypeRoom);
...
}
}

4 Experimental testing
During the testing of specialization, 10 million objects in case of Floor, Door, Wall and
Room classes and 10 thousand objects in case of class Building were created. The huge
number of objects was created due to the requirement of accurate time slices
measurement. We have measured the time of code execution 5 times for each class.
Results of the testing are listed in the following table (Tab. 1). Measured times are
mentioned in milliseconds. A row for specialized code contains letter s after the
name of the class.
Tab. 1. Results of experimental testing
Class
Floor
Floor s
Door
Door s
Wall
Wall s
Room
Room s
Building
Building s

trial 1

trial2

trial 3

trial 4

trial 5

average

441
310
640
591
1482
1412
7481
7431
1032
1001

440
330
641
591
1492
1442
7581
7480
1011
1011

430
291
681
590
1492
1442
7691
7431
1042
1032

440
300
651
581
1482
1432
7521
7460
1012
981

430
300
661
600
1503
1432
7550
7581
1011
1021

436,2
306,2
654,8
590,6
1490.2
1432
7564,8
7476,6
1021.6
1009.2

speed up

29,8%
9,8%
3.9%
1.2%
1.2%

Specialization of Object-Oriented Programs Written in Java Language

Together
10124
(Floor, Door,
Wall, Room)
Together s
9834

10225

10194

10085

10245

10174,6

9674

9614

9734

9674

9706

131

4,6%

At the end, 10 million objects of the classes Floor, Door, Wall and Room were
created. In this case we measured the acceleration of code that contains the creation of
simple and difficult object together.

5 Results of specialization
The speed up range for the object creation is between 1.2 and 29.8 percents. The lower
limit arose due to the fact that creating a difficult object takes too much time compared
to virtual call evaluation. The highest speed up was achieved by the creation of simple
objects where the time needed for virtual call evaluation is comparable to the time of
simple object creation. The specialization of prototype and abstract factory is beneficial
when it is used for creation of simple objects. The specialization of difficult object
creation reaches the zero speed up.

6 Virtual method calls elimination by using aspects


It is possible to use aspects for eliminating of virtual calls. There are two approaches
that provide virtual call elimination. The first one is dynamic (or run time)
specialization that uses pointcuts and advices. We tested this approach and the results
were not satisfactory. In fact, using aspects for such a dynamic specialization leads to
slow down the speed of a code execution. Usage of aspects for dynamic specialization
introduces overheads into the code, which are bigger than benefit from replacing
virtual calls. Run time specialization by using of aspects is therefore not suitable for
virtual calls elimination.
The second approach (static) is usage of aspects during compilation. New
methods that will be called from original code have to be created and introduced into
original source code. Then the original calls of virtual methods must be replaced by the
created methods.

7 Conclusions
Design patterns offer many advantages in software development but may introduce
overheads into the original code. We presented the way of eliminating some of these
overheads, specifically those ones that are caused by virtual method calls. For virtual
calls replacement, it is convenient to impose the functionality of aspect-oriented
programming.
Introduction of new specialized methods and replacement of virtual calls in the
original source code makes the code less readable and it worsens the efficiency of its
further development. Hence the specializer should be used as a precompiler only.

132

Igor Dysko

Acknowledgement: The work reported here was supported by Science and Technology
Assistance Agency under the contract No. APVT-20-007104.

References
1. Bobeff, G., Noy, J.: Molding components using program specialization
techniques. In: Eighth International Workshop on Component-Oriented
Programming, Bosch, J., Szyperski, C., Weck W. (Eds.), Darmstadt, Germany
(2003)
2. Dysko, I.: Specialization of Design Patterns. In: ISIM05 Conference Proceedings
(2005)
3. Gamma, E., Helm, R., Johnson, R., Vlissides, J.: Design Patterns: Elements of
Reusable Object-Oriented Software. Addison-Wesley, 1995.
4. Nvrat, P., Filkorn, R.: A Note of the Role of Abstraction and Generality in
Software Development, Journal of Computer Science, Vol. 1, No. 1, Science
Publications (2005), 98-102.
5. Schultz, U. P., Lawall, J. L., Consel, Ch.: Automatic program specialization for
Java. ACM Transactions on Programming Languages and Systems (TOPLAS),
Vol. 25, No. 4 (2003), 452-499.
6. Schultz, U. P.: Black-box program specialization. Published Technical Report
17/99, Department of Software Engineering and Computer Science, University of
Karlskrona/Ronneby, In: Proceedings of WCOP'99 (1999)
7. Schultz, U. P.: Partial Evaluation for Class-Based Object-Oriented Languages. In:
Symposium on Programs as Data Objects II. Lecture Notes in Computer Science,
Vol. 2053, O. Danvy and A. Filinski (Eds.), Aarhus, Denmark, 173-197.
8. Schultz, U. P., Lawall, J. L., Consel, Ch.: Specialization patterns. Research report
1242, IRISA, Rennes, France, 1999.
9. Schultz, U. P., Lawall, J. L., Consel, Ch., Miller, G.: Towards automatic
specialization of Java Programs. In: Proceedings of the European Conference on
Object-Oriented Programming (ECOOP99). Lecture Notes in Computer Science,
Vol. 1628 (1999), 367-390. R.Guerraoui (Ed.), Springler-Verlag, Lisbon, Portugal
10. Smolarov, M., Nvrat, P., Bielikov, M.: Abstracting and Generalising with
Design Patterns. In: ISCIS'98 - The 13th Int. Symposium on Computer and
Information Sciences, A. Grsoy U. Gdkbay, T. Dayar and E. Gelenbe (Eds.),
Belek-Antalya, Turkey, IOS Press, Amsterdam (1998), 551-558.
11. Volanschi, E., Consel, C., Muller, G., Cowan, C.: Declarative Specialization of
Object-Oriented Programs. In: OOPSLA97 Conference Proceedings. ACM Press,
Atlanta, GA, USA (1997), 286-300.

Potrebbero piacerti anche