Node.js: Just as fast, higher, stronger with GraalVM

A presentation at HolyJS 2019 Piter in May 2019 in St Petersburg, Russia by Oleg Šelajev

Slide 1

Slide 1

Node.js Just as fast, higher, stronger with Oleg Šelajev Oracle Labs @shelajev Copyright © 2019, Oracle and/or its affiliates. All rights reserved.

Slide 2

Slide 2

Safe Harbor Statement The following is intended to provide some insight into a line of research in Oracle Labs. 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 in connection with any Oracle product or service remains at the sole discretion of Oracle. Any views expressed in this presentation are my own and do not necessarily reflect the views of Oracle. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 2

Slide 3

Slide 3

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !3

Slide 4

Slide 4

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !4

Slide 5

Slide 5

Launched earlier this month: GraalVM Enterprise 19.0 • More performance • Smaller footprint • Managed runtime for better isolation when running native code • Oracle Enterprise Support 7x24x365 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Confidential – Oracle Internal/Restricted/Highly Restricted 5

Slide 6

Slide 6

Why GraalVM? Fast Java, Scala, Kotlin, Groovy, Clojure… Instant startup, low footprint Polyglot & embeddable VM Interoperability between languages: LLVM, Python, Ruby, R Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !6

Slide 7

Slide 7

Why GraalVM? version Let’s find out Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !7

Slide 8

Slide 8

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !8

Slide 9

Slide 9

Sulong (LLVM) Truffle Framework GraalVM JIT Compiler Java HotSpot VM Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !9

Slide 10

Slide 10

Sulong (LLVM) Truffle Framework GraalVM JIT Compiler Without the JVM Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !10

Slide 11

Slide 11

Sulong (LLVM) Truffle Framework GraalVM JIT Compiler Java HotSpot VM Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !11

Slide 12

Slide 12

Sulong (LLVM) Truffle Framework GraalVM JIT Compiler Without the JVM Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !12

Slide 13

Slide 13

JavaScriptCore

Slide 14

Slide 14

DEMO Copyright © 2019, Oracle and/or its affiliates. All rights reserved.

Slide 15

Slide 15

V8 Dev Blog — https://v8.dev/blog/ignition-interpreter Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !15

Slide 16

Slide 16

V8 Dev Blog — https://v8.dev/blog/ignition-interpreter Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !16

Slide 17

Slide 17

function TreeNode(execute, children) { this.execute = execute; this.children = children; } Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 17

Slide 18

Slide 18

Interpreter worklist.push(rootMethod.start) do { node = worklist.peek(); if (node.hasNotEvaluatedSuccessors()) { worklist.pushAll(node.successors) } else { worklist.pop(); node.evaluate(); } } while (worklist.notEmpty) U U U U U Reality: more lines of code Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 18

Slide 19

Slide 19

Execution U U U U U Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 19

Slide 20

Slide 20

Systems.Computers.Controls, Volume 2, Number 5, 1971 Copyright © 2019, Oracle and/or its affiliates. All rights reserved.

Slide 21

Slide 21

function pow (x, y) { return x ** y; } function pow (x, 2) { return x * x; } Copyright © 2019, Oracle and/or its affiliates. All rights reserved.

Slide 22

Slide 22

Executable U U U U U Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 22

Slide 23

Slide 23

Speculation and Deoptimization Optimize using partial evaluation assuming stable profiling feedback U U S U S Collect profiling feedback I I S I U U I I IS S I Deoptimize if profiling feedback is invalid and reprofile Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 23

Slide 24

Slide 24

Stability Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 24

Slide 25

Slide 25

Warmed-up ClojureScript unit tests https://github.com/graalvm/graaljs/issues/29 Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !25

Slide 26

Slide 26

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !26

Slide 27

Slide 27

ECMAScript compatibility https://kangax.github.io/compat-table/es2016plus/ Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !27

Slide 28

Slide 28

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !28

Slide 29

Slide 29

DEMO Copyright © 2019, Oracle and/or its affiliates. All rights reserved.

Slide 30

Slide 30

DEMO Copyright © 2019, Oracle and/or its affiliates. All rights reserved.

Slide 31

Slide 31

Interoperability Example a= obj.prop „obj“ is not a JavaScript object Message Resolution Not JS? Not JS? Python ? Uses an inline cache entry for every language. 31 Copyright © 2019, Oracle and/or its affiliates. All rights reserved.

Slide 32

Slide 32

Interoperability Protocol boolean isNull(Object receiver); boolean isBoolean(Object receiver); boolean asBoolean(Object receiver); boolean isExecutable(Object receiver); Object execute(Object receiver, Object… arguments); boolean isInstantiable(Object receiver); Object instantiate(Object receiver, Object… arguments); boolean isString(Object receiver); String asString(Object receiver); boolean isNumber(Object receiver); boolean fitsInByte(Object receiver); boolean fitsInShort(Object receiver); boolean fitsInInt(Object receiver); boolean fitsInLong(Object receiver); boolean fitsInFloat(Object receiver); boolean fitsInDouble(Object receiver); byte asByte(Object receiver); short asShort(Object receiver); int asInt(Object receiver); long asLong(Object receiver); float asFloat(Object receiver); boolean isObject(Object receiver); Object getMembers(Object receiver, boolean includeInternal); Object getMembers(Object receiver); boolean isMemberReadable(Object receiver, String member); Object readMember(Object receiver, String member); boolean isMemberModifiable(Object receiver, String member); boolean isMemberInsertable(Object receiver, String member); void writeMember(Object receiver, String member, Object value); boolean isMemberRemovable(Object receiver, String member); void removeMember(Object receiver, String member); boolean isMemberInvokable(Object receiver, String member); Object invokeMember(Object receiver, String member, Object… arguments); boolean isMemberInternal(Object receiver, String member); boolean isMemberWritable(Object receiver, String member); boolean isMemberExisting(Object receiver, String member); boolean isArray(Object receiver); Object readElement(Object receiver, long index); long getArraySize(Object receiver); boolean isElementReadable(Object receiver, long index); void writeElement(Object receiver, long index, Object value); void removeElement(Object receiver, long index); boolean isElementModifiable(Object receiver, long index); boolean isElementInsertable(Object receiver, long index); boolean isElementRemovable(Object receiver, long index); boolean isElementWritable(Object receiver, long index); boolean isElementExisting(Object receiver, long index); boolean isPointer(Object receiver); long asPointer(Object receiver); Object toNative(Object receiver); 32 Copyright © 2019, Oracle and/or its affiliates. All rights reserved.

Slide 33

Slide 33

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

Slide 34

Slide 34

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

Slide 35

Slide 35

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

Slide 36

Slide 36

•An arbitrary number of JS runtimes can be used by one thread at a time. •Concurrent access to Java objects is allowed •Concurrent access to JavaScript objects is not allowed https://medium.com/graalvm/multi-threaded-java-javascript-language-interoperability-in-graalvm-2f19c1f9c37b Copyright © 2019, Oracle and/or its affiliates. All rights reserved.

Slide 37

Slide 37

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

Slide 38

Slide 38

Composing Tools x Debugger Profiler Coverage IDE integration … Copyright © 2019, Oracle and/or its affiliates. All rights reserved. Confidential – Oracle Internal/Restricted/Highly Restricted 38

Slide 39

Slide 39

AST Node (vertex) Tagging Example function foo(a, b) { a = b + 42 } ROOT interface InstrumentableNode { boolean isInstrumentable(); boolean hasTag(Class<Tag> tag); STATEMENT, EXPRESSION WrapperNode createWrapper(ProbeNode probe … EXPRESSION } Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 39

Slide 40

Slide 40

Conditional Breakpoint Example Asynchronous breakpoint Installation Next Execution On Enter installs wrapper and triggers external invalidation of the AST lazily installs breakpoint and inserts condition AST Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 40

Slide 41

Slide 41

DEMO Copyright © 2019, Oracle and/or its affiliates. All rights reserved.

Slide 42

Slide 42

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

Slide 43

Slide 43

Why GraalVM? version Polyglot applications: Java, Python, Ruby, R, LLVM JVM infrastructure: threads, large heaps Incremental rewrite of Java applications to Node.js Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !43

Slide 44

Slide 44

Building a Universal VM is a Community Effort • Test your applications with GraalVM – Documentation and downloads at http://www.graalvm.org • Connect your technology with GraalVM – Integrate GraalVM into your application – Run your own programming language or DSL – Build language-agnostic tools • Join the conversation – Report issues or pull requests on GitHub – graalvm-users@oss.oracle.com – Follow @graalvm Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Confidential – Oracle Internal/Restricted/Highly Restricted !44