Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Run Scala Faster with GraalVM on any Platform / GraalVMで、どこでもScalaを高速実行しよう by Amitpal Singh

31 views

Published on

on June 29th at ScalaMatsuri 2019.
http://2019.scalamatsuri.org/index_en

Published in: Software
  • Be the first to comment

  • Be the first to like this

Run Scala Faster with GraalVM on any Platform / GraalVMで、どこでもScalaを高速実行しよう by Amitpal Singh

  1. 1. Run Scala Faster with GraalVM on any Platform Amitpal Singh Director, Oracle Labs Research & Advanced Development
  2. 2. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | Safe Harbor Statement The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, timing, and pricing of any features or functionality described for Oracle’s products may change and remains at the sole discretion of Oracle Corporation. - 2
  3. 3. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | What is GraalVM ● Next Generation VM that runs multiple languages fast with a single runtime 3 JVM languages Light weight Languages LLVM language
  4. 4. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | GraalVM architecture ● Graal and Truffle for a High Performance Multilingual Executable VM Environment 4 Graal Compiler JVM CI (JVM Compiler Interface) Java Hotspot VM Java's AST Interpreter Framework Interface for Writing JIT Compiler in Java Truffle Framework JIT compiler written in Java JavaScript Interpreter Ruby Interpreter Python Interpreter LLVM Interpreter R Interpreter
  5. 5. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | 5
  6. 6. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | GraalVM: The Graal Compiler • Written in Java – Modular, maintainable, and extensible • Comes in two flavors – Graal CE – Graal EE • Advanced optimizations – Partial Escape Analysis (Stadler et al. CGO ‘14) – Path Duplication (Leopoldseder et al. CGO ‘18) – Priority Inlining (Prokopec et al. CGO ‘19) 6 long count = 0, sum = 0; for (int i = 0; i < persons.length; i++) { Person person = persons[i]; int age = person.age; if (person.employment == Employment.EMPLOYED && person.salary > 100_000 && age >= 40) { sum += age; count += 1; } } return (double) sum / count; Arrays.stream(persons) .filter(p -> p.getEmployment() == Employment.EMPLOYED) .filter(p -> p.getSalary() > 100_000) .mapToInt(Person::getAge) .filter(age -> age >= 40).average() .getAsDouble();
  7. 7. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | 7 GraalVM Enterprise Performance – the benchmarks • There’s a new benchmark called “Renaissance” we developed with 4 universities (Charles University, Università della Svizzera Italiana, Johannes Kepler Institute, & Universidad Privada Boliviana) that is described in a new PLDI paper GraalVM EE has 62 optimization phases vs 35 in CE – better inling, profiling, & vectorization among them Renaissance Benchmark: http://Renaissance.dev SpeedupvsJDK8 0.0 1.0 2.0 3.0 4.0 akka-uct chi-square dec-tree finagle-chirper fj-kmeans gauss-mix movie-lens neo4j-analytics philosophers scrabble streams-mnemonics 1.05681.0871.0417 1.1628 0.9804 1.1236 0.98041.0204 2.7778 0.9709 0.8333 0.9434 1.0871.0309 0.9346 0.7937 1.2048 1.087 0.99970.99640.98520.9709 1.3202 1.587 1.1667 1.6628 1.12751.12361.098 1.3469 3.0833 1.02911.0333 1.9623 1.14131.1443 1.486 1.1667 1.37351.3587 1.0997 1.5275 1.2709 1.0194 GraalVM EE 19.0 GraalVM CE 19.0
  8. 8. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | 8 GraalVM Enterprise Performance: Scalabench SpeedupvsJDK8 0.00 0.70 1.40 2.10 2.80 apparat factorie kiama scalac scaladoc scalap scalariform scalatest scalaxb specs tmt geomean 1.0713 1.6393 0.9709 0.6579 0.8621 1.1494 1.0204 1.25 1.06381.0638 1.5385 0.9259 1.3805 2 1.0194 1.1908 0.8362 1.3448 1.1939 1.4375 1.2447 1.4043 2.6154 1.6204 GraalVM EE 19.0 GraalVM CE 19.0 GraalVM shows best results on Scala – which is why Twitter uses GraalVM CE
  9. 9. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | 9 Twitter Team uses Graal compiler for Scala • Twitter VM team uses Graal compiler for their Scala-based microservices; • Resulting in reduced object allocations and improves overall execution speed.
  10. 10. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | - libraal: Run Scala Faster from the get-go • GraalVM Compiler – written in Java – used as JIT Compiler in HotSpot • Packaged as Native Image – Isolates for each compilation • First run will be on par with C2 10 $ scala Streams On your mark, get set, go! 0 (1065 ms) 1 (329 ms) 2 (149 ms) 3 (107 ms) 4 (106 ms) 5 (81 ms) 6 (125 ms) 7 (51 ms) 8 (34 ms) 9 (33 ms) total: 29999997 (2114 ms) $ scala Streams On your mark, get set, go! 0 (191 ms) 1 (107 ms) 2 (69 ms) 3 (120 ms) 4 (27 ms) 5 (26 ms) 6 (27 ms) 7 (28 ms) 8 (27 ms) 9 (27 ms) total: 29999997 (678 ms) With libgraal With graal.jar
  11. 11. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | 11 GraalVM for Faster Compilation Scala Compilation NormalizedTime 0 0.25 0.5 0.75 1 Akka Lichess Guardian Front end 0.52 0.65 0.34 0.56 0.8 0.38 0.80.8 0.8 Java GraalVM Bloop Bloop + GraalVM Comparing to Java 8, GraalVM EE, Bloop, Bloop + GraalVM EE
  12. 12. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | - Graal with HotSpot: Conclusions • Using C2 instead of Graal CE--same as wasting ~7% of the performance – JDK 11+: -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI -XX: +UseJVMCICompiler – JDK 8: Download GraalVM CE and put on PATH and as JAVA_HOME • Using GraalVM EE in production ~35% speedup • Using GraalVM EE for project compilation – Gives a productivity boost 12
  13. 13. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | - Host VM 13 Native Image: Fast Startup and Low Memory Footprint Application Libraries Java class library Substrate VM Input: Classes of application, libraries, and VM Iterative analysis until fixed point is reached Machine Code Image Heap Output: Native executable or library AOT-compile code and take object heap snapshot Points-to Analysis Run Initializations
  14. 14. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | - 14 Native Image: Fast Startup and Low Memory Footprint $ scalac HelloWorld.scala $ time scala HelloWorld Hello, World! user 0.070s $ native-image -cp $SCALA_HOME/lib/scala-library.jar:./ HelloWorld $ time ./helloworld Hello, World! user 0.005s
  15. 15. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | Scala Native via Native Image • Scala Native provides an idiomatic interface • Scala Native can be implemented via GraalVM – Translate Scala Native intrinsics into GraalVM intrinsics – Can be implemented as a simple compiler plugin • All JVM libraries would become available with GraalVM – No need to re-write parts of the JVM ecosystem 15
  16. 16. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | Performance of Scala Native vs. Native Image • Scala Native Benchmarks running with the immix GC 16 Speedup 0 0.55 1.1 1.65 2.2 DeltaBlue Tracer GCBench Json Bounce Richards 1.31 1.19 1.37 1.13 1.34 2.02 0.96 0.81 1.09 0.97 1.21 0.96 111111 Scala Native 0.3.7 Native Image Native Image PGO
  17. 17. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. "17 git clone https://github.com/graalvm/graalvm-demos cd graalvm-demos/scala-days-2018/scalac-native/scala-substitutions sbt package cd ../ $GRAALVM_HOME/bin/native-image -cp $SCALA_HOME/lib/scala-compiler.jar:$SCALA_HOME/lib/ scala-library.jar:$SCALA_HOME/lib/scala-reflect.jar:$PWD/scalac-substitutions/target/scala-2.12/ scalac-substitutions_2.12-0.1.0-SNAPSHOT.jar -H:SubstitutionResources=substitutions.json,substitutions-2.12.json -H:ReflectionConfigurationFiles=scalac-substitutions/reflection-config.json -H:Class=scala.tools.nsc.Main -H:Name=scalac Native scalac
  18. 18. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. "18 https://medium.com/graalvm/compiling-scala-faster-with-graalvm-86c5c0857fa3
  19. 19. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | - Microservice Frameworks: Startup Time Helidon Micronaut Quarkus 0 ms 750 ms 1500 ms 2250 ms 3000 ms 940 ms 2101 ms 988 ms 952 ms 2087 ms 1030 ms 16 ms 37 ms 35 ms Native Image JDK 12 JDK 8 19
  20. 20. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | - Microservice Frameworks: Memory Footprint Helidon Micronaut Quarkus Maximum Memory Size 0 MB 45 MB 90 MB 135 MB 180 MB 121 MB 180 MB 106 MB 125 MB 172 MB 116 MB 17 MB 41 MB 31 MB Native Image JDK 12 JDK 8 20
  21. 21. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | - Fast Startup and Low Footprint for the Scala Ecosystem • GraalVM Native Image is ready for wide adoption – Sorted out most of the unsupported features • Try out your framework (e.g., Akka and Play) – Report issues if any – Test it in your CI: GraalVM Native Image is easy to install and test • The GraalVM team actively works with the community 21
  22. 22. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | Native Image: Profile-Guided Optimizations (PGO) • The Graal compiler is built ground-up with profiles in mind – Collecting profiles is essential for performance of native images • PGO requires running relevant workloads before building an image 22 native-image --pgo- instrument Instrumented Binary native-image --pgo Optimized Binary Profiles (.iprof) Relevant Workloads
  23. 23. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | What about Scala Macros? • Scala macros use dynamic class loading – Dynamically loaded by scalac – Not always known at image build time • Current solution – Build a custom native executable for a project with all macros included 23
  24. 24. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | - Native Image is cool, but what’s more in GraalVM Enterprise? Quarkus Hibernate Benchmark 24 RequestsPerSecond 0 4,500 9,000 13,500 18,000 Cumulative number of requests sent by ApacheBench 1000 10000 100000 1e+06 1e+07 Native Image (EE with PGO) Native Image (CE) JDK 8, Java HotSpot VM Quarkus Hibernate Benchmark After this many requests, improve GC by turning it off & on again We’re working on a better GC too
  25. 25. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | Native Image: Scala Performance 25 • Benchmarks of Scala command line tools Command Line Tools SpeedupvsJDK8 0 0.35 0.7 1.05 1.4 scalac scalariform scalafmt 1.37 1.24 1.03 0.88 0.68 0.6 Native CE/C2 Native EE/C2
  26. 26. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | 26
  27. 27. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | - GraalVM Native Image: Support any Platform via LLVM 27 • Implemented an LLVM backend for GraalVM Native Image – In case Apple starts requiring LLVM for their apps – Easy to port Scala to niche architectures • Using LLVM comes with a price – Moving GC in LLVM is still experimental – Slow at compilation – Slower run-time generated code
  28. 28. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | Cross-Platform Scala: GraalVM and JavaFX IDE Plugin GluonTools JavaFX mobile Graal VM Native ImageGluon Mobile • JavaFX extensions for mobile • Integration with mobile functionality (e.g. GPS/ camera) • Mobile-specific connectivity Java static libs
  29. 29. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | - Cross-Platform Scala: GraalVM and JavaFX • Apple does not allow dynamic code on iOS devices – Running HotSpot is not allowed – Java interpreter is allowed, but slow • Graal Native Image compiles Java Bytecode into native iOS code for AMD64 (simulator) and AArch64 (iPhone/iPad) • JavaFX contains Java code and native code – Java code is compiled by Graal Native Image – Native code is available for iOS and Android platforms 29
  30. 30. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | - Cross-Platform Scala: GraalVM and JavaFX • Maven/Gradle plugins are available for doing – Scala compilation – Native image generation – Linking with required OS-specific tools, libraries, frameworks – Signing apps – Deploying on iOS devices and simulators • Java 11/12/13 apps created for desktop work equally on iOS devices (Android coming soon) 30
  31. 31. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | Demo: Development Tools 31
  32. 32. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | React Native vs JavaFX on GraalVM + More components + Bigger community + Hot Code Updates 32 + Running on the JVM + Better Performance + Faster Startup
  33. 33. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | Spawning a Vibrant Community • Scala is an awesome language to start new projects – Examples: Spark, Akka and Play • React and Flutter native components are open-source – No reason not to use them on the JVM • Time to spawn a mobile ecosystem on the JVM – Scala would be a good place to start 33
  34. 34. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | Cross-Platform Scala: Next Steps • Completeness – Support for GC on iOS – Running on Android • Deploying to mobile faster – Use a Java bytecode interpreter for development • Support for Android 34
  35. 35. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | Conclusions • Don’t waste energy, time, and money – -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI -XX:+UseJVMCICompiler • Time to start JVM-based cross-platform development – Start here: https://github.com/gluonhq/client-samples 35
  36. 36. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | GraalVM: Run Programs Faster Anywhere • Scala compilation with GraalVM 1.3x-1.5x faster • Native images of Scala programs – Fast startup and low footprint – Faster than HotSpot JIT compiled code • Try it today – https://www.graalvm.org • Demos available on GitHub – https://github.com/graalvm/graalvm-demos/scala-days-2018 • Blog article explaining the demo and the results – https://medium.com/graalvm 36
  37. 37. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | - Questions? 37 www.graalvm.org @graalvm

×