2018 Jul 25th LINE Developer Meetup #41 in Fukuoka
Session Slide in English / セッションスライドです。
Graal in GraalVM - A New JIT Compiler
オラクル社からGraalVMというものが発表され、話題を呼んでいます。GraalVMはHotSpot VM上に新しいJITコンパイラGraalと言語実装用フレームワーク/ASTインタプリタであるTruffle、さらにネイティブイメージ作成機能とその実行に使われるSubstrateVMを併せ持ったものです。すでにTruffleを使用したJavaScriptやRuby、R、Pythonの実装も提供されており、これらの言語とJavaはコードから相互に呼び出しができます。このセッションではGraalVMを概観したあと、JITコンパイラGraalにとくに注力して解説します。GraalとTruffleはOracle Labsとヨハネス・ケプラー大学で共同研究されており、多くの論文が発表されています。HotSpotのJITコンパイラとパフォーマンスや構造などを比較しつつ、GraalのJITコンパイルのテクニックについてもいくつか触れます。とにかく、私がGraalをとても好きなのです。デモも実施しつつ、Graalのすごさを伝えられればと考えています。
1. Graal in GraalVM
- A New JIT Compiler -
KanJava JUG
PONOS Corporation
Koichi Sakata(@jyukutyo)
#LINE_DM
2. About Me
• Koichi Sakata / 阪田 浩一
– @jyukutyo
• JUG Leader (KanJava JUG)
• JVM Devotee
– Want to Be A JVM / JVMになりたい
• PONOS Corporation
– Kyoto
3. About KanJava
• Kansai Java Engineer Group
– Kansai Region (Osaka/Kyoto/Kobe)
– JCP (Java Community Process)
Member
• Founded in 2009
• Over 800 Members
• Event Every Two Month
9. GraalVM
• Graal
– JIT Compiler
• Truffle
– Language Implementation Framework
• Substrate VM
– Runtime Library and a Set of Tools for
Building Java AOT Compiled Code
10. Top 10 Things To Do With GraalVM
1. High-performance modern
Java
2. Low-footprint, fast-
startup Java
3. Combine JavaScript, Java,
Ruby, and R
4. Run native languages on
the JVM
5. Tools that work across
all languages
6. Extend a JVM-based
application
7. Extend a native
application
8. Java code as a native
library
9. Polyglot in the database
10.Create your own language
12. Top 10 Things To Do With GraalVM
1. High-performance modern
Java
2. Low-footprint, fast-
startup Java
3. Combine JavaScript, Java,
Ruby, and R
4. Run native languages on
the JVM
5. Tools that work across
all languages
6. Extend a JVM-based
application
7. Extend a native
application
8. Java code as a native
library
9. Polyglot in the database
10.Create your own language
45. Twitter & Graal
• Twitter’s JDK
– Based on OpenJDK 8u
– JEP 243 (JVMCI) Backport, Graal etc.
• Twitter's Quest for a Wholly Graal Runtime – YouTube
• Improve Performance & Save $$$
50. Graal
• Actively Developed/Researched by
– Oracle Labs
– Johannes Kepler University of Linz
• Institute for System Software
• Compiler and JVM Research at JKU
63. Escape Analysis
• Analyze where references
to new objects flow
– Look for “escapes”
• Method call parameter
• Static field
• Return value
• Throw
• etc.
64. Escape Analysis
static Object field;
static Object foo() {
Object a = new Object();
field = a;
Object b = new Object();
method(b);
Object c = new Object();
return c;
}
static void method(Object o) {...}
Object a,b,c
escapes
Optimizing Allocations with Partial Escape Analysis
65. If the object doesn’t escape,
there might be
optimization opportunities
66. Escape Analysis
public Person get(String name, int age) {
Person p = new Person(name, age);
Person cachedPerson = null;
for (int i = 0; i < cache.length(); i++) {
Person c = cache[i];
if (p.name.equals(c.name) && p.age == c.age) {
cachedPerson = c;
break;
}
}
if (cachedPerson != null) {
return cachedPerson;
}
return null;
}
Object p doesn’t
escape
Optimizing Allocations with Partial Escape Analysis
67. Scalar Replacement
public Person get(String name, int age) {
Person cachedPerson = null;
for (int i = 0; i < cache.length(); i++) {
Person c = cache[i];
if (name.equals(c.name) && age == c.age) {
cachedPerson = c;
break;
}
}
if (cachedPerson != null) {
return cachedPerson;
}
return null;
}
• Field loads replaced
with local variables
• Allocation was
removed
Optimizing Allocations with Partial Escape Analysis
68. Partial Escape Analysis
public Person get(String name, int age) {
Person p = new Person(name, age);
Person cachedPerson = null;
for (int i = 0; i < cache.length(); i++) {
Person c = cache[i];
if (p.name.equals(c.name) && p.age == c.age) {
cachedPerson = c;
break;
}
}
if (cachedPerson != null) {
return cachedPerson;
}
addToCache(p);
return p;
}
Object p
escapes...
Optimizing Allocations with Partial Escape Analysis
69. Partial Escape Analysis
public Person get(String name, int age) {
Person p = new Person(name, age);
Person cachedPerson = null;
for (int i = 0; i < cache.length(); i++) {
Person c = cache[i];
if (p.name.equals(c.name) && p.age == c.age) {
cachedPerson = c;
break;
}
}
if (cachedPerson != null) {
return cachedPerson;
}
addToCache(p);
return p;
}
If the if statement is true,
object p doesn’t escape
Optimizing Allocations with Partial Escape Analysis
70. Partial Escape Analysis
public Person get(String name, int age) {
Person p = new Person(name, age);
Person cachedPerson = null;
for (int i = 0; i < cache.length(); i++) {
Person c = cache[i];
if (name.equals(c.name) && age == c.age) {
cachedPerson = c;
break;
}
}
if (cachedPerson != null) {
return cachedPerson;
}
Person p = new Person(name, age);
addToCache(p);
return p;
}
Optimizing Allocations with Partial Escape Analysis
No allocation in this path
(It might be a frequent path)