Busy Java Developer's Guide to GraalVM

Objectives

Learn about Graal

GraalVM

Overview

GraalVM

What is it?

GraalVM

What is it?

GraalVM

Several distinct "flavors"

GraalVM Drop-in

Replacement for ...

GraalVM Drop-in

A "universal runtime"

GraalVM Drop-in

Drop-in Tools

GraalVM Drop-in

Drop-in JVM ("Espresso")

GraalVM Drop-in

(Sort of) Drop-in JVM AOT compiler

GraalVM Drop-in

Drop-in NodeJS

GraalVM Drop-in

Drop-in LLVM Toolchain

GraalVM Drop-in

Drop-in Python

GraalVM Drop-in

Drop-in Webassembly

GraalVM Drop-in

Drop-in R

GraalVM Native Images

Batteries included; no JVM required

GraalVM Native Images

Wait, what?

GraalVM Native Images

Native binaries

GraalVM Native Images

Building the image

GraalVM Native Images

Example

public class Example {
    public static void main(String[] args) {
        String str = "Native Image is awesome";
        String reversed = reverseString(str);
        System.out.println("The reversed string is: " + reversed);
    }

    public static String reverseString(String str) {
        if (str.isEmpty())
            return str;
        return reverseString(str.substring(1)) + str.charAt(0);
    }
}

build.sh

echo GraalVM Home is "$GRAALVM_HOME"

echo Compiling Java...
javac $1.java

echo Converting to native binary...
$GRAALVM_HOME/bin/native-image $*

GraalVM Native Images

How are we doing this?

GraalVM Native Images

native-image options

GraalVM Native Images

What's going on in here?

GraalVM Native Images

But...

GraalVM Native Images

Is this really better?

GraalVM Native Images

I need more details!

Truffle

A Framework for Languages

Truffle

Truffle Language implementations

Truffle

Two modes of usage

Truffle-as-polyglot

From each language according to its abilities...

Truffle-as-polyglot

Installation/usage

Truffle-as-polyglot

Hosting from Java

Truffle-as-polyglot

Hello, JavaScript

package com.newardassociates.graalpolyglot;

import org.graalvm.polyglot.*;
import org.graalvm.polyglot.proxy.*;

public class App {
    static String JS_CODE = 
        "(function myFun(param){return 'hello '+param;})";

    public String getGreeting() {
        try (Context context = Context.create()) {
            Value value = context.eval("js", JS_CODE);
            return value.execute("World").asString();
        }
    }

    public static void main(String[] args) {
        System.out.println(new App().getGreeting());
    }
}

Truffle-as-polyglot

Hello, Java!

var BigInteger = Java.type('java.math.BigInteger');
console.log(BigInteger.valueOf(2).pow(100).toString(16));

Run with (GraalVM) node

$ node --jvm app.js
10000000000000000000000000

Truffle: Language Implementation Framework

Building your own language using Truffle

Truffle Implementation

Wait, build my own language?

Truffle Implementation

Simple Language

Truffle Implementation

Simple Language

Truffle Implementation

Hello, SimpleLanguage

function main() {  
  println("Hello World!");  
}

Truffle Implementation

Objects in SimpleLanguage

function main() {
    obj3 = new();
    obj3.fn = mkobj;
    println(obj3.fn().z); // prints "zzz"
}

function mkobj() {
  newobj = new();
  newobj.z = "zzz";
  return newobj;
}

Truffle Implementation

Wait, how...?

Truffle: Tool Implementation Framework

Building instrumentation tools like debuggers, profilers, etc

Truffle Tools

GraalVM as a VM/JVM platform

Truffle Tools

SimpleTool

Summary

So... now what?

Summary

Graal is the future

... I can't see how it WON'T be the future for Java

Graal Resources

Where to go to get more

Graal Resources

Official

Graal Resources

Books

Videos

Credentials

Who is this guy?