POLYGLOT WITH GRAALVM O S C O N 2 0 1 9 / M I C H A E L H U N G E R / @ M E S I R I I
MICHAEL HUNGER Caretaker General, Neo4j Head of Neo4j Labs Disturber of the Peace Java Champion (graphs)-[:ARE]->(everywhere) Twitter & Medium: @mesirii
WRITING ABOUT GRAAL SINCE 2014
WARNING: WHIRLWIND TOUR DONT‘T READ THE CODE
POLYGLOT? NOT EVERYONE IS A JAVA DEVELOPER!
POLYGLOT? We have: • Isn‘t the JVM already polyglot? – Scala, Groovy, Kotlin, Clojure, Frege … – JRuby, Jython, … L We want: • More languages, better performance
WHY SHOULD I CARE?
WHATS IN • Better JVM performance FOR ME? • Maintainable JIT compiler Lego box • Faster evolution of Java • With Truffle Language Runtime – Run JavaScript, Ruby, R, Python, LLVM code efficiently on the JVM • With Substrate VM – Binaries for Language Runtimes – AOT compiled native images of your applications
GETTING STARTED WHO READS THE INSTRUCTIONS
HOW CAN • Dedicated GraalVM Download I USE IT? or using sdkman gu (graal-updater) Utility js/node, ruby, python, R runtimes native-image tool • Java 11 with command line flags -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI -XX:+UseJVMCICompiler https://www.graalvm.org/downloads/
JAVA 11 sdk use java 11.0.1-open java -Diterations=3 CountUppercase \ I‘m happy to be back in Portland, OR for OSCON 2019 -XX:+UnlockExperimentalVMOptions -XX:+UseJVMCICompiler -Dgraal.PrintCompilation=true
GRAALVM sdk install java 19.1.0-grl gu install R python ruby native-image gu list Lego Instructions built ComponentId Version Component name ----------------------------------- graalvm 19.1.0 GraalVM Core R 19.1.0 FastR native-image 19.1.0 Native Image python 19.1.0 Graal.Python ruby 19.1.0 TruffleRuby java –version OpenJDK 64-Bit GraalVM CE 19.1.0 (build 25.212-b03-jvmci-20-b04, mixed mode)
PYTHON # graalpython fac.py 2500 import sys def fac(n): if n==1: return 1 else: return fac(n-1)*n x = int(sys.argv[1]) print("Factorial for {} is {} " .format(x,fac(x)))
LLVM #include <stdio.h> BITCODE int main() { printf("Hello from GraalVM!\n"); return 0; } clang -c -O1 -emit-llvm hello.c lli hello.bc
R PLOTTING R --version:graalvm data <- "https://raw.githubusercontent.com/selva86/datasets/master/proglanguages.csv" library(ggplot2) library(treemapify) proglangs <- read.csv(data) ggplot(proglangs, aes(area = value, fill = parent, label = id, subgroup = parent)) + geom_treemap() + geom_treemap_subgroup_border() + geom_treemap_subgroup_text() + geom_treemap_text()
JS POLYGLOT node --version:graalvm node --jvm const BigInteger = Java.type("java.math.BigInteger") let a = new BigInteger("10") .add(new BigInteger("8") .multiply(new BigInteger("4"))) console.log(a.toString()) > 42
GRAAL COMPILER OPTIMIZING COMPILER IN JAVA
VISION STATEMENT Create an extensible , modular , dynamic , and aggressive compiler using object- oriented and reflective Java programming, a graph- based and visualizable intermediate representation, and Java snippets . —Thomas Würthinger
G R A A L ! ? • JIT -Compiler implemented in Java !?! • Aggressively optimizing –e.g. inlining POJOS/DTOS –Inlining streams –Speeds up many typical Java/Scala programs • Uses compute graph for optimization • New compiler interface (JVMCI)
GRAAL COMPILER OPTIMIZATIONS
GRAALVM BOX OF JOY GRAAL ❤ TRUFFLE ❤ SUBSTRATE
GraalVM is a high-performance, BIGGER ON embeddable, polyglotVirtual Machine for THE INSIDE running applications written in JavaScript, Python, Ruby, R, JVM-based languages like Java, Scala, Kotlin, and LLVM-based languages such as C and C++. Additionally, GraalVM allows efficient interoperability between programming languages and compiling Java applications ahead-of-time into native executables for faster startup time and lower memory overhead. https://github.com/oracle/graal/releases
HISTORY • Collection of Research Projects – TruffleRuby / FastR • Maxine (Research) VM in Java • „A Joke?“ • „Java-on-Java“ John Rose – Project Metropolis • Power Combo: – Substrate-VM – Truffle – Graal Compiler – AOT Compilation
• Oracle Labs Project GRAALVM • Versions – 19.1.1 (quarterly release) • Integrated – JVM 1.8.x – Node.js 10.x / ECMAScript 2019 – LLVM bitcode runtime • Supports – Truffle Runtime – Language Packs (via gu) – Native Image AOT • Editions – Community (GPL v2 w/ CP-Exception – Enterprise (faster, sandboxing, commercial support) – Oracle Database Engine
NATIVE IMAGE MACHINE CODE BABY
• Aggressive Ahead of time compilation (AOT) NATIVE • Extremely fast startup time IMAGE • Small binary executables for current OS • Class initialization during build • For FaaS, Commandline • Microservices: Micronaut, Helidon, Quarkus, Spring (soon) • No classloading / class metadata • Limitations: – no reflection, no later classloading, no initializer dynamics – Slow build https://medium.com/graalvm/lightweight-cloud-native-java-applications- 35d56bc45673
TRUFFLE L A N G U A G E R U N T I M E
TRUFFLE • Language Runtime • API & Type system • Implement language constructs • Annotated Java Methods – T ooling, T esting • Generic or specialized operations
TRUFFLE + GRAAL
TRUFFLE • Integrates with Graal Compiler GRAAL • Partial Evaluation • Optimize special cases based on steady state assumption • Deoptimize (trap) on failed assumptions
SAMPLE LANGUAGE T R U F F L E E X A M P L E L A N G U A G E
TRUFFLE: ADDITION-NODE (SL) @NodeInfo (shortName = "+") public abstract class SLAdditionNode extends SLBinaryNode { @Fallback protected Object typeError(Object left, Object right) { throw SLException.typeError(this, left, right); } @Specialization (rewriteOn = ArithmeticException.class) protected long add(long left, long right) { return Math.addExact(left, right) ; } @Specialization @TruffleBoundary protected SLBigNumber add(SLBigNumber left, SLBigNumber right) { return new SLBigNumber(left.getValue().add(right.getValue())) ; } @Specialization (guards = "isString(left, right)") @TruffleBoundary protected String add(Object left, Object right) { return left.toString() + right.toString() ; } protected boolean isString(Object a, Object b) {…}
LANGUAGES BOX OF COLORS
JAVASCRIP T • Main target language via graaljs • Replacement for Rhino/Nashorn • EcmaScript 2019 & Node.js (10.15.2) compat • 90% of 95k npm packages • Graaljs can run slowly w/o Graal • ScriptEngine support • org.graalvm.js:js/js-scriptengine https://www.graalvm.org/docs/reference-manual/languages/js/
GRAAL PYTHON • Early stage support Python 3.7 • Goal: „SciPy“ support • No python debugger, but GraalVMs https://www.graalvm.org/docs/reference-manual/languages/python/
FAST-R • Compatible with GNU R (3.5.1) • Much faster than other R implementations • R/Rscript • Install packages (e.g. ggplot2, Shiny) • Minimal: graphics package • Compatibility checker • Tools (debugger, profiler) • Java based Graphics https://www.graalvm.org/docs/reference-manual/languages/r/
TRUFFLE RUBY • Initial research project • Quite complete coverage (2.6.2) incl. c-extensions • Parallel threads • Faster than MRI/JRuby (up to 31x) • Recent: fibers • Missing: suspend, continuation, fork https://www.graalvm.org/docs/reference-manual/languages/ruby/
LLVM • LLVM 6.0.0 Bitcode • Via Sulong a LLVM implementation in Java via Truffle • Can use native libraries • lli to execute LLVM Bitcode • Sandbox in GraalVM Enterprise – sandbox libraries – virtualize syscalls – memory on managed heap https://medium.com/graalvm/safe-and-sandboxed-execution-of- native-code-f6096b35c360
POLYGLOT DO WHAT YOU WANT THURSDAY
TRUFFLE • Based on Truffle Implementations of dynamic languages • Joint underlying API / Typesystem • Context – eval – bind – invoke • Source • Value.* docs.oracle.com/en/graalvm/enterprise/19/sdk/org/graalvm/polyglot/Context.html
VALUE • The „Any“ type across languages – Scalar – List/Array – Host/Proxy Object w/ Members – Function/Invoke/Executable • Provides some semantics and conversions • Removes need for (de)serialization • Thread safety depends on language support docs.oracle.com/en/graalvm/enterprise/19/sdk/org/graalvm/polyglot/Value.html
Polyglot support: --polyglot CAPABILITIES Eval • Polyglot.eval("python","21*2") • polyglot.eval(language="ruby", file="./my_ruby_file.rb") Export / Import • polyglot.import_value(„name“) • Polyglot.export_value(„name“,value) • ctx.getBindings.put/getMember(name,value) Object Access • Object.size() / call / invoke • Object[name] / • Via Value.*
JAVA INTEROP JVM Support: --jvm Flag Import Java.import “java.util.UUID“ java.type("java.math.BigInteger") from java.util import ArrayList Helpers • isFunction / isObject / isSymbol / isNull / instance_of Access • allow<Host/Native/Polyglot/*>Access
POLYGLOT EXAMPLES
Recommend
More recommend