<div dir="ltr"><div id="gmail-:sz" class="gmail-Am gmail-aiL gmail-Al editable gmail-LW-avf gmail-tS-tW gmail-tS-tY" aria-label="Message Body" role="textbox" aria-multiline="true" tabindex="1" style="direction:ltr;min-height:626px" aria-controls=":vh" aria-expanded="false"><font face="georgia, serif">Hello, <br><br><font color="#000000"><span style="font-size:14px;font-variant-ligatures:no-common-ligatures">We use Nashorn with Tomcat as a long running service. </span></font>We recently migrated to Java 21. ( from java 11 and the performance is good with Java-11).<br>Since Nashorn is moved out of JDK we have pulled in '<span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0);font-size:14px"><b>nashorn-core-15.4.jar</b>' and its dependency (<b>asm</b>*) and loaded it as a regular jar. Functionality everything looks ok. <br><br>During the performance test we observed very high cpu usage when nashorn engine's 'eval' is called.  The CPU consumption is so high that instances are throttled and performance becomes 10x slower compared to java-11. <br><br>Upon investigation using profiler (jvisualVM) looks like most of the CPU is spent in compile <a href="https://github.com/openjdk/nashorn/blob/main/src/org.openjdk.nashorn/share/classes/org/openjdk/nashorn/internal/runtime/Context.java#L1454">method</a>, more specifically during <a href="https://github.com/openjdk/nashorn/blob/main/src/org.openjdk.nashorn/share/classes/org/openjdk/nashorn/internal/runtime/Context.java#L217">ContextCodeInstaller.initialize</a> and </span><a href="http://NamedContextCodeInstaller.">NamedContextCodeInstaller.</a></font><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0);font-size:14px"><font face="georgia, serif"><a href="https://github.com/openjdk/nashorn/blob/main/src/org.openjdk.nashorn/share/classes/org/openjdk/nashorn/internal/runtime/Context.java#L290">install</a> <br><br>Looks like the compile method is optimized with cache, unfortunately the cache is in context scope, meaning it's not shared between ScriptEngines and Nashorn is not thread-safe (as per the docs) to use a single instance of ScriptEngine across all threads. Also the cache uses 'soft-reference', would it cause double whammy when there is a memory pressure.<br><br>so, how to improve the performance of the Nashorn engine, specifically the `compile` part.<br>Is there any other option we can try? BTW,  </font></span><span class="gmail-s1" style="font-family:georgia,serif;color:rgb(0,0,0);font-size:14px;font-variant-ligatures:no-common-ligatures">persistent-code-cache did not help as </span><span style="font-family:georgia,serif;font-size:9.8pt;color:rgb(8,8,8)">OptimisticTypesPersistence.g</span><span style="font-family:georgia,serif;color:rgb(8,8,8);font-size:9.8pt">etVersionDirName is performing poorly </span><div><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0);font-size:14px"><font face="georgia, serif"><br>Test code that we used to study this high CPU issue is attached. </font></span></div><div><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0);font-size:14px"><font face="georgia, serif"><br></font></span></div><div><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0);font-size:14px"><font face="georgia, serif">Thanks a lot for hemp & any insight </font><br></span></div><div><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0);font-size:14px"><font face="georgia, serif">Sakkanan</font></span></div></div></div>