Subj : Re: Performance? To : Rickard From : Igor Bukanov Date : Fri Jun 20 2003 03:46 pm Rickard wrote: > Igor Bukanov wrote: > >>> I'm using Rhino as a way to accomplish dynamic extensions (aka >>> portlets) in our CMS, and also as a shell-type tool to access the >>> system. >>> >>> Functionality wise everything is the way I want it to be, but the >>> performance is a bit troubling. Most of the time we do 5-10 lines of >>> code to get some data by calling services, then transforming it to be >>> suitable for HTML output. What surprised me is that these small >>> snippets can take about 100ms (and sometimes up to 500ms) to execute. >>> >>> I've tried all the usual suspects: doing a shared scope, calling >>> setOptimizationLevel(9) (which made no difference at all), etc. >> >> >> You should call setOptimizationLevel(-1) to force pure interpreted >> mode. In your case this is exactly what you need since this will avoid >> creation and loading of Java classes which can be extremly slow. > > > But, these scripts will run over and over again. Isn't it better to have > them compiled in that case? How do you execute the script now? Do you use Context.compileScript() and call Script.exec() later or call Context.evaluateString directly which just a convenience method to call the above sequence? If you evaluate the same script more them once, you should compile it once and then call Script.exec on it no matter which optimization you use. Note that Context.compileScript() always compile a script to internal presentation and then that internal presentation is executed. The difference between setOptimizationLevel(level >= 0) and setOptimizationLevel(-1) is that the first one is JVM bytecode which is loaded via a custom classloader and to execute the script Rhino simply calls it. In the second case the internal presentation is a special bytecode that is interpreted during execution. > Is the access of Java objects different > depending on whether the script is compiled or not? Why would it be > faster in the interpreted mode? Context.compileScript() is significantly faster in the interpreted mode because it does not need to load Java classes which can be very slow. In addition the internal Rhino bytecode for JavaScript is simpler and faster to produce from the script parse tree then to generate JVM bytecode. Regards, Igor .