Adopting GraalVM
@petr_zapletal
GraalVM: Run Programs Faster Anywhere
Graal CE x Graal EE
Compiling with GraalVM
sbt --java-home <path-to-graal-vm>
-XX:+UnlockExperimentalVMOptions
-XX:+EnableJVMCI
-XX:+UseJVMCICompiler
● JDK 10/11 requires Scala 2.13
Limitations & Challenges
● JDK 10/11 requires Scala 2.13
● Limited library support
Limitations & Challenges
● JDK 10/11 requires Scala 2.13
● Limited library support
● Scaladex
Limitations & Challenges
● JDK 10/11 requires Scala 2.13
● Limited library support
● Scaladex
● Disabling compiler optimization
Limitations & Challenges
JDK 11
[success] Total time: 571 s, ...
real 12m48.492s
user45m40.264s
sys 2m31.045s
vs.
Graal
(using the flags)
[success] Total time: 507 s, ...
real 11m34.191s
user41m52.512s
sys 2m12.863s
Stopwatch Compiling
GraalVM Runtime
Just-in-time compilation
Compilation & JVM
Just-in-time (JIT)
● Compiles while running
● Dynamic compilation
● Runtime information
● De-optimization
● Typical for JVM
Ahead-of-time (AOT)
● Compiles before running
● Static compilation
● More resources
● Way more time
● Uncommon for JVM (JDK 9+)
java -XX:+UnlockExperimentalVMOptions
-XX:+EnableJVMCI
-XX:+UseJVMCICompiler
object Main extends App {
val max = 10000000
def isPrime(n: Int): Boolean =
primes.takeWhile(p => p*p <= n).forall(n % _ != 0)
val primes = 2 #:: Stream.from(3,2).filter(isPrime)
primes.takeWhile(_ <= max).toList
}
Primes
Primes
object Main extends App {
val max = 10000000
def isPrime(n: Int): Boolean =
primes.takeWhile(p => p*p <= n).forall(n % _ != 0)
val primes = 2 #:: Stream.from(3,2).filter(isPrime)
primes.takeWhile(_ <= max).toList
}
Primes
object Main extends App {
val max = 10000000
def isPrime(n: Int): Boolean =
primes.takeWhile(p => p*p <= n).forall(n % _ != 0)
val primes = 2 #:: Stream.from(3,2).filter(isPrime)
primes.takeWhile(_ <= max).toList
}
Primes
object Main extends App {
val max = 10000000
def isPrime(n: Int): Boolean =
primes.takeWhile(p => p*p <= n).forall(n % _ != 0)
val primes = 2 #:: Stream.from(3,2).filter(isPrime)
primes.takeWhile(_ <= max).toList
}
jdk 1.8.0_161-b12
time <path-to-java>/java -jar
primes.jar
real 0m11.646s
user0m14.286s
sys 0m0.657s
vs.
graalvm-ee-1.0.0-rc8
time <path-to-graal>/java
-XX:+UnlockExperimentalVMO
ptions
-XX:+EnableJVMCI
-XX:+UseJVMCICompiler
-jar primes.jar
real 0m13.478s
user0m29.199s
sys 0m0.975s
Primes
class Primes {
@Benchmark
@BenchmarkMode(Array(Mode.AverageTime))
@OutputTimeUnit(TimeUnit.MICROSECONDS)
def getPrimes() = primes.takeWhile(_ <= max).toList
}
object Main extends App {
new Primes().getPrimes()
}
Primes II
class Primes {
@Benchmark
@BenchmarkMode(Array(Mode.AverageTime))
@OutputTimeUnit(TimeUnit.MICROSECONDS)
def getPrimes() = primes.takeWhile(_ <= max).toList
}
object Main extends App {
new Primes().getPrimes()
}
Primes II
jdk 1.8.0_161-b12
jmh:run -i 10 -wi 10 -f4 -t4
Primes
[info] Benchmark Mode Cnt
Score Error Units
**[info] Primes.getPrimes
avgt 40 45652.951 ±
2083.281 us/op**
vs.
graalvm-ee-1.0.0-rc8
jmh:run -i 10 -wi 10 -f4 -t4
Primes
[info] Benchmark Mode Cnt
Score Error Units
**[info] Primes.getPrimes
avgt 40 40535.844 ±
1977.021 us/op**
Primes II
(Early) Adopters
GraalVM Runtime
Ahead-of-time compilation
<path-to-graal>/bin/native-image
● JVM starts slowly
● Scala makes it worse
● JARs bloat up quickly
JVM Shortcomings
JDK 1.8 (JIT)
time java -jar primes.jar
real 0m0.555s
user 0m0.613s
sys 0m0.075s
vs.
GraalVM (AOT)
<path-to-graal>/bin/native-i
mage -jar primes.jar
time ./primes
real 0m0.039s
user 0m0.004s
sys 0m0.010s
Counting Primes
JDK 1.8 (JIT)
time java -jar primes.jar
real 0m11.954s
user 0m14.077s
sys 0m0.739s
vs.
GraalVM (AOT)
<path-to-graal>/bin/native-i
mage -jar primes.jar
time ./primes
Segmentation fault: 11
real 0m0.324s
user 0m0.245s
sys 0m0.077s
Counting Primes
Limitations & Challenges
● Dynamic class loading
Limitations & Challenges
● Dynamic class loading
● InvokeDynamic
Limitations & Challenges
● Dynamic class loading
● InvokeDynamic
● Reflection
Limitations & Challenges
● Dynamic class loading
● InvokeDynamic
● Reflection
● JVMTI, JMX, other native VM
interfaces
Limitations & Challenges
● Dynamic class loading
● InvokeDynamic
● Reflection
● JVMTI, JMX, other native VM
interfaces
● And the list goes on
Limitations & Challenges
● Small programs/scripts
● Alexa apps
● AWS Lambdas (using GO
runtime)
Use Cases
Do you want to use Scala
in cases like these?
There is More
There is More
● Truffle (~ language implementation framework)
○ Implementations of JavaScript, Ruby, R, Python or C
○ Helps when designing additional languages
○ Polyglot programming made easy
○ Common tooling
There is More
● Truffle (~ language implementation framework)
○ Implementations of JavaScript, Ruby, R, Python or C
○ Helps when designing additional languages
○ Polyglot programming made easy
○ Common tooling
● Sulong (~ an engine for running LLVM bitcode)
There is More
● Truffle (~ language implementation framework)
○ Implementations of JavaScript, Ruby, R, Python or C
○ Helps when designing additional languages
○ Polyglot programming made easy
○ Common tooling
● Sulong (~ an engine for running LLVM bitcode)
● Multilingual Engine (MLE) (~ using GraalVM languages and
modules from SQL)
Summary
Questions
References
References
● https://github.com/scala/make-release-notes/blob/2.13.x/projects-2.13.md
● https://index.scala-lang.org/search?q=fullScalaVersion%3A2.13.0-M5
● https://github.com/oracle/graal/blob/master/substratevm/LIMITATIONS.md
● https://www.youtube.com/watch?v=Rvb-kvuPzqI
● https://github.com/chrisseaton/graalvm-ten-things
● https://www.youtube.com/watch?v=wBegU4d4GRc
● https://engineering.opsgenie.com/run-native-java-using-graalvm-in-aws-lambda-with-golan
g-ba86e27930bf
● https://medium.com/graalvm/instant-netty-startup-using-graalvm-native-image-generation-
ed6f14ff7692
● https://www.infoq.com/articles/Graal-Java-JIT-Compiler
● https://medium.com/graalvm/compiling-scala-faster-with-graalvm-86c5c0857fa3
Images
● https://cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50
425/d7eaeb73-4b9e-4764-ad10-b5785d99d114/Image/497cab5d0638a34c8a08194ef
b83b2fc/announcing_graalvm_version7.png
● https://cdn-images-1.medium.com/max/2000/1*rHZFwj8uu-x8KVCItuu53Q.png
● https://unsplash.com/photos/4m7gmLNr3M0
● https://www.tecflow.com/uk/images/slechte-koude-start-tecflow.jpg

Adopting GraalVM - Scale by the Bay 2018