Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to store and load precompiled js to org.graalvm.polyglot.Context

It there any way to convert javascript source into some pre-compiled stated that can be stored and loaded somehow to org.graalvm.polyglot.Context instead of eval-ing it as a raw String? Something like undocumented --persistent-code-cache in nashorn.

like image 620
shabunc Avatar asked Apr 28 '19 19:04

shabunc


People also ask

Why GraalVM is faster?

Run Java FasterThe compiler of GraalVM provides performance advantages for highly abstracted programs due to its ability to remove costly object allocations in many scenarios.

What is polyglot API?

The GraalVM Polyglot API enables your applications running in GraalVM to execute code in multiple other languages from within your application. For instance, from a Java application you can execute JavaScript code. If you use GraalVM as your Java SDK - then you already have the Polyglot API available on the classpath.

Who is using GraalVM?

Twitter are one company using GraalVM in production today, and they say that for them it is paying off in terms of real money saved. Twitter are using GraalVM to run Scala applications — GraalVM works at the level of JVM bytecode so it works for any JVM language.


1 Answers

As of May'19, you can share code within the same process to avoid reparsing (similar to the Nashorn code persistence) by reusing the same Engine object between different Contexts like this:

try (Engine engine = Engine.create()) {
    Source source = Source.create("js", "21 + 21");
    try (Context context = Context.newBuilder().engine(engine).build()) {
        int v = context.eval(source).asInt();
        assert v == 42;
    }
    try (Context context = Context.newBuilder().engine(engine).build()) {
        int v = context.eval(source).asInt();
        assert v == 42;
    }
}

More details can be found here: https://www.graalvm.org/docs/graalvm-as-a-platform/embed/#enable-source-caching

We have plans to support persistent code cache across processes in combination with the GraalVM native-image tool in the future. We already support creating native-images that contain the JavaScript interpreter and the GraalVM compiler. We want to add support for allowing to include pre-warmed up scripts, hopefully with pre-compiled JavaScript native-code as well. So you might be able to start your JS application with close to zero startup time. No ETA though.

like image 173
Christian Humer Avatar answered Oct 23 '22 04:10

Christian Humer