Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

Polyglot on the JVM with Graal

Polyglot on the JVM with Graal

Overview of Graal and GraalVM, which was delivered at JJUG CCC 2017 spring.

Akihiro Nishikawa

May 20, 2017
Tweet

More Decks by Akihiro Nishikawa

Other Decks in Technology

Transcript

  1. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | Polyglot on the JVM with Graal Akihiro Nishikawa May 20, 2017 JJUG CCC 2017 Spring
  2. Copyright © 2017, 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, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle. 2
  3. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | Program Agenda 3 What’s Graal? What’s Graal VM? Polyglot in Graal VM How to use Appendix 1 2 3 4
  4. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | Graal and Graal VM • A new compiler for HotSpot written in Java and with a focus on speculative optimizations. – Based HotSpot – Aggressive high-level optimizations – JVMCI and Graal included in JDK9 (modified version of JDK8 available via OTN) 5 Graal
  5. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | Graal and Graal VM • JVM packaged with Graal, Truffle, and other languages such as JavaScript, R, Ruby, and so on. 6 Graal VM
  6. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | 7 Graal and Graal VM C++ Java HotSpot Compiler Interface Client Server HotSpot JVMCI Graal HotSpot VM Graal VM Compiler Interface Client
  7. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | The One VM Concept High performance polyglot virtual machine 9
  8. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | 10 The Ruby Logo is Copyright (c) 2006, Yukihiro Matsumoto. It is licensed under the terms of the Creative Commons Attribution-ShareAlike 2.5 agreement JS Logo Copyright (c) 2011 Christopher Williams <[email protected]>, MIT licence You can distribute the R logo under the terms of the Creative Commons Attribution-ShareAlike 4.0 International license (CC-BY-SA 4.0) or (at your option) the GNU General Public License version 2 (GPL-2).
  9. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | • JavaScript – Better ECMAScript2016 score than V8 – Almost same performance as V8 – Fully support for node.js • R – Statistical language 13 • Ruby – Fork of JRuby – 5〜10x better performance • C、C++、Fortran – Native language support via LLVM Graal VM Polyglot
  10. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | 14 Graal VM Polyglot C HotSpot JVMCI Graal Truffle LLVM (Sulong) R Ruby JavaScript Java
  11. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | Truffle • Together with the Graal compiler, Truffle interpreters are automatically just-in-time compiled and programs running on top of them can reach performance of normal Java. • Provides the basic foundation for building abstract-syntax-tree (AST) interpreters that perform self-optimizations at runtime – The included TruffleDSL provides a convenient way to express such optimizations. 15 Framework for implementing languages as simple interpreters
  12. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | 16 Fully compatible including native module support! Graal.js Architecture C++ JavaScript Java node modules with only JavaScript node standard library V8 API Thread pool (libeio) Event loop (libev) DNS (c-ares) crypto (OpenSSL) Graal.js JavaScript Engine Adapter V8 API to Graal.js (via JNI) Native extensions node bindings (socket, http, …) node modules with native extensions
  13. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | FastR • Goal: realize the advantages of the Truffle stack for R – Superior performance without resorting to C/C++/Fortran/… – Designed for data-heavy and parallel applications – CRAN / Bioconductor repository support • Not an ”incremental improvement” on GNU R – New execution engine written from scratch, based on Truffle – Designed as a drop-in replacement for GNU R • Speedup over latest GNU R interpreter – Somewhere between 2 and 10x 17 https://github.com/graalvm/fastr
  14. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | Completeness 19 ECMAScript 2015 (ES6) (16298 tests/16417 tests) Failing tests are to a large part Unicode Regular Expressions 93.4% ECMAScript 2016 (ES7) Supports exponentiation operator, Array.prototype.includes Fails due to new block-level function declaration and corner- case tests of the spread operator 99.3%
  15. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | Oracle Confidential – Internal Classic research benchmarks (Octane) 0 0.2 0.4 0.6 0.8 1 1.2 1.4 V8 (5.4.500.6) Graal.js 20 roughly level with V8
  16. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | Completeness – language and core library 22 Ruby Language JRuby: 94% 96% Ruby core libraries JRuby: 95% 99%
  17. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | Classic research benchmarks – 10-20x faster 0 5 10 15 20 25 30 35 40 45 GraalVM JRuby+invokedynamic Ruby 23 Bigger score is better (based on Ruby score) Benchmarks bound by allocation or BigInteger performance
  18. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | “But, it’s easy to optimize that kind of code!” 24
  19. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | 25 Simple local variables Vectorisation opportunities Simple floating point arithmetic Basic loops No method calls (except operators) Only types are numerical or boolean
  20. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | 27 Smalltalk-style blocks instead of loops Loop bounds are objects instead of simple values Instance variables Logic hidden in methods Arrays
  21. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | 28 Arithmetic hidden in core library methods Intermediate objects Hash mapped to an array of arrays, via another array, converted back to a hash Inner loop pixels represented as a hash of r, g, b No local variables, only method calls
  22. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | 29 Metaprogramming send Dynamically created symbol Actual logic method dynamic method calls
  23. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | Chunky PNG kernels 0.01 0.1 1 10 100 1000 GraalVM JRuby+invokedynamic Ruby 30 Many of these benchmarks are optimised away entirely by GraalVM
  24. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | PSD.rb kernels 0.1 1 10 100 1000 GraalVM JRuby+invokedynamic Ruby 31
  25. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | REST public internet REST private intranet 33
  26. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | Ruby Truffle::Interop.eval('application/language', source) value = Truffle::Interop.import(name) Truffle::Interop.export(name) 37
  27. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | JavaScript Interop.eval('application/language', source) value = Interop.import(name) Interop.export(name) // Returns 20 Interop.eval('application/javascript', '14 + 6') 38
  28. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | JavaScript in Ruby puts Truffle::Interop.eval('application/javascript', '14 + 6') 39 This returns 20 JavaScript Ruby
  29. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | JavaScript in Ruby Truffle::Interop.eval('application/javascript', "function add(a,b) { return a + b; } Interop.export('add', add.bind(this));") add = Truffle::Interop.import('add') puts add.call(14,2) 40 Uses function defined in JavaScript from Ruby JavaScript Ruby
  30. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | 44 Clamp Warms up and then reports iterations per second Random inputs stop the whole thing being totally optimized away
  31. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | Ruby 0 1000000 2000000 3000000 4000000 5000000 6000000 7000000 8000000 9000000 10000000 GraalVM JRuby+invokedynamic Ruby Operations Per Second 45
  32. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | 46 JavaScript (V8) Rewritten in JavaScript and call it on V8
  33. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | Ruby + JavaScript (V8) 0 50000 100000 150000 200000 250000 300000 350000 Ruby (just Ruby) Ruby (Ruby + JS with V8) Operations Per Second 47
  34. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | JRuby + JavaScript (Rhino) 0 100000 200000 300000 400000 500000 600000 JRuby+indy (just Ruby) JRuby+indy (Ruby + JS with Rhino) Operations Per Second 49
  35. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | JRuby + JavaScript (Nashorn) 0 100000 200000 300000 400000 500000 600000 JRuby+indy (just Ruby) JRuby+indy (Ruby + JS with Rhino) JRuby+indy (Ruby + JS with Nashorn) Operations Per Second 51
  36. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | Ruby + JavaScript (Graal VM) 0 1000000 2000000 3000000 4000000 5000000 6000000 7000000 8000000 9000000 10000000 GraalVM (just Ruby) GraalVM (Ruby + JS) JRuby+invokedynamic Ruby Operations Per Second 53
  37. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | 0 1000000 2000000 3000000 4000000 5000000 6000000 7000000 8000000 9000000 10000000 GraalVM (just Ruby) GraalVM (Ruby + JS) JRuby+indy (just Ruby) JRuby+indy (Ruby + JS with Rhino) JRuby+indy (Ruby + JS with Nashorn) Ruby (just Ruby) Ruby (Ruby + JS with V8) Operations Per Second 54
  38. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | 1 10 100 1000 10000 100000 1000000 10000000 GraalVM (just Ruby) GraalVM (Ruby + JS) JRuby+indy (just Ruby) JRuby+indy (Ruby + JS with Rhino) JRuby+indy (Ruby + JS with Nashorn) Ruby (just Ruby) Ruby (Ruby + JS with V8) Operations Per Second 55
  39. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | Basic Concepts • Common representation of programs • Keep it rich enough to not have to throw away meaning • Common optimization of the representation 60
  40. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | x + y * z + x * y z load_local x load_local y load_local z call * call + pushq %rbp movq %rsp, %rbp movq %rdi, -8(%rbp) movq %rsi, -16(%rbp) movq %rdx, -24(%rbp) movq -16(%rbp), %rax movl %eax, %edx movq -24(%rbp), %rax imull %edx, %eax movq -8(%rbp), %rdx addl %edx, %eax popq %rbp ret 61
  41. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | U U U U U Node Rewriting for Profiling Feedback AST Interpreter Uninitialized Nodes Node Transitions S U I D G Uninitialized Integer Generic Double String T. Würthinger, C. Wimmer, A. Wöß, L. Stadler, G. Duboscq, C. Humer, G. Richards, D. Simon, and M. Wolczko. One VM to rule them all. In Proceedings of Onward!, 2013. 62
  42. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | U U U U U I I I G G Node Rewriting for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized Nodes Node Transitions S U I D G Uninitialized Integer Generic Double String T. Würthinger, C. Wimmer, A. Wöß, L. Stadler, G. Duboscq, C. Humer, G. Richards, D. Simon, and M. Wolczko. One VM to rule them all. In Proceedings of Onward!, 2013. 63
  43. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | I I I G G I I I G G Rewriting ng Feedback AST Interpreter Rewritten Nodes Compilation using Partial Evaluation Compiled Code I D Uninitialized Integer Generic Double T. Würthinger, C. Wimmer, A. Wöß, L. Stadler, G. Duboscq, C. Humer, G. Richards, D. Simon, and M. Wolczko. One VM to rule them all. In Proceedings of Onward!, 2013. 64
  44. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | T. Würthinger, C. Wimmer, A. Wöß, L. Stadler, G. Duboscq, C. Humer, G. Richards, D. Simon, and M. Wolczko. One VM to rule them all. In Proceedings of Onward!, 2013. I I I G G I I I G G Deoptimization to AST Interpreter D I Node Rewriting to Update Profiling Feedback 65
  45. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | T. Würthinger, C. Wimmer, A. Wöß, L. Stadler, G. Duboscq, C. Humer, G. Richards, D. Simon, and M. Wolczko. One VM to rule them all. In Proceedings of Onward!, 2013. I I G G D I D G G D I D G G Node Rewriting to Update Profiling Feedback Recompilation using Partial Evaluation 66
  46. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | 70 Even if another language… Frequently executed call
  47. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | • Includes: – JVM (RE or DK) – Java – JavaScript – Ruby – R – More in the future • Binary tarball release • Mac or Linux 82 Graal VM – everything in one package today
  48. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | 83 Java 9 – runs on an unmodified JVM Hotspot Graal Truffle JS others… Ruby Java C++ JVMCI (JVM Compiler Interface)
  49. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | 84 Java 9 – runs on an unmodified JVM Hotspot Graal Truffle JS others… Ruby via Maven etc Java 9
  50. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | 86 http://www.oracle.com/technetwork/oracle-labs/program-languages Search for “Graal OTN”
  51. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | 87 https://github.com/graalvm Search for “GitHub GraalVM”
  52. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | Acknowledgements Oracle Danilo Ansaloni Stefan Anzinger Cosmin Basca Daniele Bonetta Matthias Brantner Petr Chalupa Jürgen Christ Laurent Daynès Gilles Duboscq Martin Entlicher Brandon Fish Bastian Hossbach Christian Humer Mick Jordan Vojin Jovanovic Peter Kessler David Leopoldseder Kevin Menard Jakub Podlešák Aleksandar Prokopec Tom Rodriguez Oracle (continued) Roland Schatz Chris Seaton Doug Simon Štěpán Šindelář Zbyněk Šlajchrt Lukas Stadler Codrut Stancu Jan Štola Jaroslav Tulach Michael Van De Vanter Adam Welc Christian Wimmer Christian Wirth Paul Wögerer Mario Wolczko Andreas Wöß Thomas Würthinger JKU Linz Prof. Hanspeter Mössenböck Benoit Daloze Josef Eisl Thomas Feichtinger Matthias Grimmer Christian Häubl Josef Haider Christian Huber Stefan Marr Manuel Rigger Stefan Rumzucker Bernhard Urban University of Edinburgh Christophe Dubach Juan José Fumero Alfonso Ranjeet Singh Toomas Remmelg LaBRI Floréal Morandat University of California, Irvine Prof. Michael Franz Gulfem Savrun Yeniceri Wei Zhang Purdue University Prof. Jan Vitek Tomas Kalibera Petr Maj Lei Zhao T. U. Dortmund Prof. Peter Marwedel Helena Kotthaus Ingo Korb University of California, Davis Prof. Duncan Temple Lang Nicholas Ulle University of Lugano, Switzerland Prof. Walter Binder Sun Haiyang Yudi Zheng Oracle Interns Brian Belleville Miguel Garcia Shams Imam Alexey Karyakin Stephen Kell Andreas Kunft Volker Lanting Gero Leinemann Julian Lettner Joe Nash David Piorkowski Gregor Richards Robert Seilbeck Rifat Shariyar Alumni Erik Eckstein Michael Haupt Christos Kotselidis Hyunjin Lee David Leibs Chris Thalinger Till Westmann
  53. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

    | Safe Harbor Statement The preceding 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, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle. 89