Performance Hints

1. Access to local variables are faster than to external variables or class names.

Local variables are assigned to JVM registers, while external variables are accessed by a method call. The following code illustrates a use of local variables to improve the performance.

function test(){
  Object = Object   // this line makes it faster
  for(i = 0; i < 1000; i++){
    Object()
  }
}

2. Don't call eval() in a loop.

eval() is a relatively heavy operation. So eval() call in a loop should be expanded if it might be a perfomance bottle neck.

for(i = 0; i < 1000; i++){
  eval("x" + i + " = " + i)
}
This can be rewritten as follows.
ca = openCharArray()
for(i = 0; i < 1000; i++){
  ca.write("x" + i + " = " + i + "\n")
}
eval(ca.toString())

3. Choose an appropriate PnutsImpl

pnuts.lang.PnutsImpl class defines an interface of script interpreter's implementation. The following classes are implementation classes of the interface provided in the Pnuts distribution.

pnuts.lang.PnutsImpl
Pure interpreter
pnuts.compiler.CompilerPnutsImpl
On-the-fly Compiler
pnuts.ext.CachedPnutsImpl
Mixed mode, which caches compiled (or parsed) scripts and reuse them

On-the-fly compiler usually runs faster than pure interpreter. It is appropriate for interactive use, since the compilation overhead does not matter.

If Pnuts is embedded in an application as a scripting engine and same scripts are executed over and over, mixed mode is the right choice. Since on-the-fly compilation takes a few milliseconds to a few hundreds milliseconds depending on the size of scripts, caching the compiled code would improve the performance.

If the application uses a limited set of scripts, the combination of pure interpreter and precompiled scripts is efficient because there is no compilation overhead and the bytecode compiler package in Pnuts can be removed if needed.

4. When a method call or an instance creation is a bottle neck:

You might consider to use makeProxy() function.


Back