Fastest Scripting Engine

Hi, I’ve read some posts about fast scripting in java, but I’m still confused about which one I should use.
Right now I’m using javax.script, and it is really slow.

My game heavily relies on scripting. Each player, object, map has atleast 1 script if not more executing each frame.
When I put in 200 players, (I know it’s a lot, but it is what I’m aiming for), switching between scripting and not makes my game go from 200fps to 10fps. (nothing else is changed)

I want to be able to have scripts that pretty much run as fast as java does.
Is this possible, and which one should I use?

I also heard about janino. Could I use this?

Thanks very much,
roland

javax.script is a generic interface to any number of scripting languages. Are you using javascript? The JS that java comes with is Rhino, which isn’t stunningly fast, but it shouldn’t be THAT slow unless either your scripts are overly complex or you’re invoking it wrong.

If you want code to run as fast as java, it’s hard to beat java itself. Janino can compile .class files that can be loaded normally and JIT’d like any other java, but it’s not as fast as current versions of javac, and not even close to eclipse’s ejc.

Another alternative is Groovy++, which is supposed to be extremely fast, and has a javax.script interface.

Thanks for the reply, sproingie.

I am using javascript, and my scripts aren’t that complex. I could be invoking it wrong, I’m pretty new to javax.script.
I followed this tutorial: http://java.sun.com/developer/technicalArticles/J2SE/Desktop/scripting/
and made a simple script class:


package util;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
//import javax.swing.JOptionPane;

import file.FileManager;

public class Script
{
	ScriptEngineManager mgr;
	ScriptEngine jsEngine;
	Invocable invocableEngine;
	String m_script;
	
	public Script(String _strFile)
	{
		mgr = new ScriptEngineManager();
		jsEngine = mgr.getEngineByName("JavaScript");
		m_script = "";
		
		invocableEngine = (Invocable)jsEngine;
		

		m_script = FileManager.LoadFileToString(_strFile);
	}
	
	public void ExecuteFunction(String _strName, Object... args)
	{
		try
		{
			invocableEngine.invokeFunction(_strName,args);
		} 
		catch(Exception e)
		{
			e.printStackTrace();
			//JOptionPane.showMessageDialog(null, "Error: " + e.getMessage());
		}
	}
	
	public void ExecuteScript()
	{
		try 
		{
			jsEngine.eval(m_script);
		} 
		catch (ScriptException ex) 
		{
			ex.printStackTrace();
		}  
	}

	public void AddObject(String _strName, Object object)
	{
		jsEngine.put(_strName, object);
	}
}


Every single player has a Script object (Is this bad? I have to load the same file 200 times which is not good, although I want the variables inside the script to be separate for each player)
Also I have ScriptEngineManagers for each script. Can I just have one? and one engine?

I could have a look at Groovy++. I don’t especially want to learn a whole new language, JS was nice.
Thanks again

Janino is easy to use if you want to use Java as your scripting language. It’s hard to give advice on scripting language questions without knowing how you want to use it.

I tried out Janino and it seems to be a lot faster. And I would like to use java as my scripting language. :slight_smile:
Is this the best solution?

How I want to use it? It’s hard to explain. I need the script so that I can change how my player moves and responds to things, and scripts for AI calculation, etc.

Just a test I did:

javax.script “JavaScript”:

function process()
{
	for (var i = 0; i < 1000000; i++) 
		Math.sin(i);
}

754 ms average

Janino:

for (int i = 0; i < 1000000; i++) 
      Math.sin(i);

0.75 ms average (1000 times faster)

Is this accurate?

No, the JVM probably just deleted the entire code as it doesn’t do anything. Try summing the results and outputting them at the end, which will give you a more accurate view*

Cas :slight_smile:

  • of a microbenchmark, so largely meaningless

I didn’t word my question very clearly. By “how” I really meant, does java’s programming model fit what you want to do?

WRT: The micro-benchmark. This is poorly formed even if you take princec’s suggestion and make it sum and use the result. The cost of a trig function vastly outweight the relatively cheap induction variable updating/summation operations. Janino will result in code that will run as fast as if not scripted, once the set-up cost (converting to bytecode) has completed.

That Script class looks about right. I take it you’re only calling ExecuteScript once, then invoking a known entry point every frame through ExecuteFunction? If so, I’m not sure about the slowdown – you might try a profiler and see if it’s really taking all the time in ExecuteFunction or not.

No matter how fast you get it, executing scripts every frame is going to be a potential drain. You might want to only execute scripts if they’re triggered by some event, so you only have to check the trigger every frame.

I know you don’t want to switch languages, but if you don’t find some obvious bottleneck, it may very well be Rhino that’s the problem. Groovy (and especially groovy++) has made really impressive strides in performance lately, meanwhile the Rhino engine in java has quite literally not changed at all.

Thanks for the replies everyone.

That’s true, I only call executeScript once, then use executeFunction each frame.
the executeFunction takes about 0.3ms (which is not much), but does add up when there are 200 players each executing their scripts each frame. Maybe I should have 1 script that controls all the players, instead of one for each. And Janino is quite fast so I am going to see how it goes, even though groovy++ looks really good too.


function process()
{
	var a = 0.0;
	for (var i = 0; i < 1000000; i++) 
		a += Math.sin(i);
	print(a + "\n");
}

i outputted a sum of the sin(i)'s getting a result of 0.23288397807310357 for each.

janino took on average 83ms and javascript took 835ms. (so only 10 times faster this time)

although you are right, its just a sin function so doesn’t really mean much.

I’m still not sure what you mean. Do you mean why Java would be a good scripting language for my game?
I don’t really care what language it is as long as it’s fast enough, and I would rather use a language I knew well than having to learn a new one :slight_smile:

One script engine with one entry point that runs all the players’ contributed scripts is likely to be immensely faster than invoking 200 separate script calls, yes. Even then, when you have 200 possible iterations, making the scripts triggered if possible (obviously not the case if they’re controlling animation) will make it all that much faster. After all, the fastest code is the code you don’t run.

ok :slight_smile: Thanks sproingie

Pnuts compiles to byte code. It still isn’t as fast as Java, since it needs to support additional language features, but it is fast. You might also just use Java for everything.