Java has been constantly criticized for poor performance ever since its inception, but not as much in recent years. Thanks to optimizing dynamic native code compilers, Java performance today is very close to the performance of low-level languages such as C/C++. Along with dynamic compilers, static compilers for Java have been evolving as well, so there is still no clear winner between these two approaches. It is not a surprise that an AOT compiler is finally going to appear in Java 9 via JEP-295. This session’s speaker would like to dispel common myths about the old dispute over whether dynamic or static compilation is better, show that both approaches have their strengths and weaknesses, and explain why the future is the hybrid approach.
43. A a;
…
a.foo();
if (RT_Type(a) in CHA-set) {
inlined body of foo()
} else {
a.foo();
}
Idea: method not overriden in subclasses
=>
non-virtual call
Class Hierarchy Analysis (CHA)
43
compiled to
45. Type Analysis
A a = b ? new B() : new C();
a.foo();// non-virtual call too, because
// B, C extend A and do not override A.foo
45
46. Type Analysis
A a = b ? bar() : baz();
…
a.foo();
If bar() only returns new B,
baz() only returns new С,
then
a.foo() is non-virtual and can be inlined
46
53. Example
ArrayList list = getCollection();
ArrayList.Itr iter = new ArrayList.Itr(list);
while (iter.hasNext()) {
Object o = iter.next();
doSomething(o);
}
53
54. Example
ArrayList list = getCollection();
ArrayList.Itr iter = onStack Itr();
iter.this$0 = list;
iter.cursor = 0;
iter.size = list.elemData.length;
while (iter.hasNext()) {
Object o = iter.next();
doSomething(o);
}
54
55. Example
ArrayList list = getCollection();
ArrayList.Itr iter = onStack Itr(list);
iter.this$0 = list;
iter.cursor = 0;
iter.size = list.elemData.length;
while (iter.cursor < iter.size) {
int index = iter.cursor++;
Object o = iter.this$0.elemData[index];
doSomething(o);
}
55
56. Example
ArrayList list = getCollection();
int cursor = 0;
int size = list.elemData.length;
while (cursor < size) {
Object o = list.elemData[cursor++];
doSomething(o);
}
56
57. Example
ArrayList list = getCollection();
int size = list.elemData.length;
for (int i = 0; i < size; i++) {
doSomething(list.elemData[i]);
}
57