polyglot
play

POLYGLOT WITH GRAALVM O S C O N 2 0 1 9 / M I C H A E L H U N - PowerPoint PPT Presentation

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 &


  1. 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

  2. MICHAEL HUNGER Caretaker General, Neo4j Head of Neo4j Labs Disturber of the Peace Java Champion (graphs)-[:ARE]->(everywhere) Twitter & Medium: @mesirii

  3. WRITING ABOUT GRAAL SINCE 2014

  4. WARNING: WHIRLWIND TOUR DONT‘T READ THE CODE

  5. POLYGLOT? NOT EVERYONE IS A JAVA DEVELOPER!

  6. POLYGLOT? We have: • Isn‘t the JVM already polyglot? – Scala, Groovy, Kotlin, Clojure, Frege … – JRuby, Jython, … L We want: • More languages, better performance

  7. WHY SHOULD I CARE?

  8. 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

  9. GETTING STARTED WHO READS THE INSTRUCTIONS

  10. 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/

  11. 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

  12. 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)

  13. 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)))

  14. LLVM #include <stdio.h> BITCODE int main() { printf("Hello from GraalVM!\n"); return 0; } clang -c -O1 -emit-llvm hello.c lli hello.bc

  15. 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()

  16. 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

  17. GRAAL COMPILER OPTIMIZING COMPILER IN JAVA

  18. 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

  19. 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)

  20. GRAAL COMPILER OPTIMIZATIONS

  21. GRAALVM BOX OF JOY GRAAL ❤ TRUFFLE ❤ SUBSTRATE

  22. 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

  23. 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

  24. • 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

  25. NATIVE IMAGE MACHINE CODE BABY

  26. • 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

  27. TRUFFLE L A N G U A G E R U N T I M E

  28. TRUFFLE • Language Runtime • API & Type system • Implement language constructs • Annotated Java Methods – T ooling, T esting • Generic or specialized operations

  29. TRUFFLE + GRAAL

  30. TRUFFLE • Integrates with Graal Compiler GRAAL • Partial Evaluation • Optimize special cases based on steady state assumption • Deoptimize (trap) on failed assumptions

  31. SAMPLE LANGUAGE T R U F F L E E X A M P L E L A N G U A G E

  32. 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) {…}

  33. LANGUAGES BOX OF COLORS

  34. 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/

  35. 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/

  36. 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/

  37. 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/

  38. 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

  39. POLYGLOT DO WHAT YOU WANT THURSDAY

  40. 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

  41. 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

  42. 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.*

  43. 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

  44. POLYGLOT EXAMPLES

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend