Can Nashorn startup slowness be overcome?
Asked Answered
G

1

10

I've used Rhino for a scripting component inside graphics. In the project there are about 200 small scripts running independantly. Immediately when starting the application the scripts should be at full speed. Rhino's performance was sufficient, but since Oracle advices to migrate to Nashorn, i'm facing a dilema.

Below a picture showing the load difference between Rhino and Nashorn at approximayely 15,000 invocations of the scripts. The Startup slowness of Nashorn is my biggest issue.

Note, this was back on JDK 1.8.0. JDK 1.8u5 is similar

Engine performance compared

I hope the picture is clear.

This is an outline of how i use the ScriptEngine:

  • I'm using One scripting Engine instance,
  • i create a CompiledScript object for each script,
  • A Swingworker executes a CompiledScript.eval() once.
  • Every half second the SwingWorkers are started.
  • Each CompiledScript has its own SimpleScriptContext instance which is reused for every execution.

Below i included a runtime profile of how busy the engine is over time; enter image description here

Does anyone know how to overcome the startup slowness of Nashorn?


UPDATE 15 April '15
Ran the same test with 200 seperate scripts on Java8u45.
Performance is much better! Runs similarly fast as Rhino on Java7.

Gytle answered 12/9, 2014 at 14:53 Comment(4)
Rhino under Java 1.8 could be revealing.Perri
Thanks, that would be my last hope. By the way, I read here that i'd need a manually built jar. Any idea why, or would it just be for discouragement?Gytle
I read: "If you want to get pre-built binary instead, you can download from here:" - so no problem. It is compiled in not Java 1.8 - so what.Perri
But that's the 'js.jar', not the 'js-engine.jar' as explained in the next step. "Goed weekend" :)Gytle
K
3

On Java 1.8, you can use Rhino via the javax.script API by using this Maven dependency and requesting the engine rhino:

    <dependency>
        <groupId>de.christophkraemer</groupId>
        <artifactId>rhino-script-engine</artifactId>
        <version>1.1.1</version>
    </dependency>

Home page here: https://github.com/cevou/rhino-script-engine

Binaries: here

If you want the very latest version of Rhino, you can override it by adding something like this:

    <dependency>
        <groupId>org.mozilla</groupId>
        <artifactId>rhino</artifactId>
        <version>${rhinoVersion}</version>
    </dependency>

Binaries: here

Incidentally, if you want to use get the latest Rhino on Java 1.7 via javax.script, you should request the engine name rhino17R5 or you might randomly get an instance of the old Rhino which is part of the JRE. The exact engine name required depends on the version of rhino-script-engine. For 1.1.1, it is rhino17R5.

Kurr answered 5/3, 2015 at 8:9 Comment(5)
Thanks. (i'm not familiar with maven) I'm not sure, but if your rhino is the same version as can be built using this article: wiki.openjdk.java.net/display/Nashorn/… Then that interpreter is actually quite a bit slower than the one built-in to java7. Maybe you know why?Gytle
I think this one is taken directly from OpenJDK (presumably 7), because it is GPL + Classpath exception. I think it would be a much newer implementation than the one you mention.Kurr
The default Rhino optimisation level may have changed between the old ScriptEngine implementation for Rhino and the more recent one.Kurr
Version 1.1.1 now includes Rhino 1.7R5 with engine name rhino17R5.Declaration
Thanks @cevou. I've updated the example. Have you considered coming up with a unique name for your engine, one which won't change so often? For instance, rhino-cevou?Kurr

© 2022 - 2024 — McMap. All rights reserved.