The technique and strategy of using the Rhino framework for high-performance scripts in the Java library (TIPS and Strategies for High-Performance Script Parsing in Java Class Libraries using the Rhino Framework)
The techniques and strategies of using the Rhino framework for high -performance scripts in the Java library
Overview:
Rhino is an open source framework running the JavaScript script on the Java virtual machine.It provides a way to embed JavaScript into the Java application. Using Rhino can obtain flexibility and script ability.However, in order to ensure high -performance script analysis, we need to understand some skills and strategies.
1. Write the efficient JavaScript code:
Writing efficient JavaScript code is the key to ensuring high performance to ensure script.Here are some techniques and suggestions:
-So avoid using too much JavaScript function calls in the cycle to minimize the number of functions calls as much as possible.
-Sto use of local variables to store the duplicate value to avoid multiple access to global variables.
-So avoid too much closure because they increase the memory consumption of scripts.
-A using native JavaScript objects and data types instead of Java objects, because the Java object conversion in Rhino will bring performance expenses.
-For the frequent string stitching operations and try to use array and StringBuilder.
2. Optimize Rhino configuration:
By reasonable configuration Rhino can improve the performance of script analysis.Here are some optimization strategies:
-Cap to the size of the heap: By adjusting the number of memory on appropriately, the performance of script resolution can be improved.You can use the Context class provided by Rhino to set the maximum pile of size.
Context cx = Context.enter();
cx.setOptimizationLevel (-1); // Turn off optimization
cx.setmaximuminterpreterstackdepth (1000); // Set the heap size
-The cache and reuse of the Context object: Create and destroy the CONTEXT objects with a large overhead, so you can try to use the connection pool or singular mode to reuse the Context object.
-Basion multi -threaded support: In some cases, multi -threaded support for Rhino's disability may be more conducive to performance.Multi-threaded support can be disabled by setting `rhino.opt.Level` to" -1 ".
System.setProperty("rhino.opt.level", "-1");
-Ast-in-time compiler (JIT): Rhino provides an optional JIT compiler that can improve the performance of script analysis.You can use the following code to enable the JIT compiler:
cx.setOptimizationLevel(9);
3. Reasonable use of the cache:
The compiled script that has been compiled can avoid repeated analysis and compilation process, and improve the performance of script execution.You can use the MAP data structure to cache the compiled script.
Script compiledScript = scriptCache.get(script);
if (compiledScript == null) {
// There is no existence in the cache, then compiles
compiledScript = cx.compileString(script, "", 1, null);
scriptCache.put(script, compiledScript);
}
4. Pre -compiled resources:
If there is a static script resources in the application, these resources can be pre -compiled when the application starts to avoid dynamically parsing and compiling during runtime.This can be embedded in the Java file by embedding the script resources during the construction process, and the script is realized directly when the application starts.
Summarize:
By writing high -efficiency JavaScript code, optimizing Rhino configuration, and rational use of cache and pre -compilation resources, it can improve the efficiency of using the Rhino framework in the Java library for high -performance script analysis.These techniques and strategies can help developers get better performance when using Rhino.