Polyglot with Graalvm

Polyglot with Graalvm

POLYGLOT WITH GRAALVM OSCON 2019 / MICHAEL HUNGER / @MESIRII 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 • Faster evolution of Java Lego box • 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 sdk install java 19.1.0-grl GRAALVM 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 --version:graalvm R PLOTTING 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 GRAAL !? • 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 • Versions GRAALVM – 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 LANGUAGE RUNTIME TRUFFLE • Language Runtime • API & Type system • Implement language constructs • Annotated Java Methods – Tooling, Testing • 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 TRUFFLE EXAMPLE LANGUAGE 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 • To o l s (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 Eval CAPABILITIES • 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 POLYGLOT EXAMPLE (1) // gu install ruby python R // groovy PolyTest1.groovy @Grab("org.graalvm.sdk:graal-sdk:19.1.0") import org.graalvm.polyglot.* ctx = Context.newBuilder().allowAllAccess(true).build() ctx.eval("js", "print('Hello JavaScript!');") ctx.eval("R", "print('Hello R!');"); ctx.eval("ruby", "puts 'Hello Ruby!'"); ctx.eval("python", "print('Hello Python!')"); POLYGLOT EXAMPLE (PYTHON-JAVA) import java generic = java.type('org.testcontainers.containers.GenericContainer') container = generic('nginx') container.setExposedPorts([80]) container.start(); print('%s:%s' % (container.getContainerIpAddress(), container.getMappedPort(80))); https://medium.com/graalvm/using-testcontainers-from-a-node-js-application-3aa2273bf3bb POLYGLOT EXAMPLE (C) #include <stdio.h> #include <curl/curl.h> long request() { CURL *curl = curl_easy_init(); long response_code = -1; if(curl) { CURLcode res; curl_easy_setopt(curl, CURLOPT_URL, "http://example.com"); res = curl_easy_perform(curl); if(res == CURLE_OK) { curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    97 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us