8u20 with multi-threaded class cache

Hannes Wallnoefer hannes.wallnoefer at oracle.com
Tue Mar 17 17:38:51 UTC 2015

Hi Chris, Kunal,

Answers are inlined below.

Am 2015-03-16 um 23:03 schrieb Chris Pettitt:
> Hi folks,
> We're looking into the possibility of using the class cache in multi-threaded code, as introduced in [1]. We have a few questions related to this feature:
> 1. The article implies that ScriptEngine can be treated as thread-safe - provided we're not using the default context - though the code doesn't state this explicitly. Is this a safe assumption? Are there any other caveats?

This is correct as far as Nashorn is concerned. In the original JDK8 
release, using a ScriptEngine with multiple bindings/globals will 
compile each script from scratch. We introduced the class caching in 
8u20 as our best effort to fit code reuse on the existing ScriptEngine 
API. The only caveat I can think of is that code may run slower with 
multiple bindings because of callsite polymorphism. Also bear in mind 
that this is a relatively new feature, but there seemed to be no problem 
using it with your dust scripts (see example below).

> 2. As we need to set the Context for each eval, does this lock us out of using Invocable?

You can actually use this with Invocable.invokeMethod(), passing the 
binding as first argument ("thiz" parameter). I've rewritten the 
threaded class cache example from the blog post to do this with your 
dust benchmark and it seems to work fine:


I just edited the bench.js file to remove the benchmark loop at the 
bottom, otherwise this will run with the script files you sent me. Feel 
free to use this as a starting point for your own testing.

It could also work with the other Invocable method taking a "thiz" 
parameter, getInterface(Object, Class), but I haven't tested this.

> 3. The code for determining if two Sources are the same ultimately falls back to a comparison of the url / name / content of the scripts. Is there a way to eval with a Source to avoid this fallback? It looks like it is not exposed in a public way.

I'm not sure I understand this question. What I use in my example is 
jdk.nashorn.api.scripting.URLReader, which is handy to preserve the 
source URL in error messages.  I believe Nashorn will still load the 
content of the URL to make sure scripts are actually identical.

I hope this helps. Let me know if you have further questions.


> [1]: https://blogs.oracle.com/nashorn/entry/improving_nashorn_startup_time_using
> Thanks,
> Chris

More information about the nashorn-dev mailing list