First fully functional JIT compiler for AngelScript available

Started by
13 comments, last by Arthur Valitov 11 years, 8 months ago
The developers at Blind Mind Studios have implemented the first fully functional JIT compiler for AngelScript.

It is now available to the public as an open source project, with the master repository hosted at github.com.

https://github.com/BlindMindStudios/AngelScript-JIT-Compiler


I haven't had a chance to test it myself, but what Andrew (Thy Reaper) tells me is quite impressive. With the JIT compiler the performance typically improves between 1.5 to 3 times, and in some cases even reaches levels of native C++ code.

This is especially impressive because the JIT compiler itself is implemented in relatively few lines of code, which makes it perfect for embedding.


Currently the JIT compiler only supports x86, but I'm sure it can be quite easily extended to other CPUs as well.



Post your impressions here.

Also, if there are suggestions for further improving AngelScript to make the JIT compiled code even better I'd love to hear them.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement
This. is. cool.

One of these days I plan on using AngelScript... I just need to find a need for scripting first... and for that to happen I need to stop doing homework and work on hobby projects, but the homework never ends...

Anyway, I'm excited to use this in the future! Thanks Andreas (for AngleScript) and Blind Mind Studios (for the JIT compiler)!
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

This. is. cool.

QFE.
clb: At the end of 2012, the positions of jupiter, saturn, mercury, and deimos are aligned so as to cause a denormalized flush-to-zero bug when computing earth's gravitational force, slinging it to the sun.
wow, that's impressive! the only gripe I have about it is that it depends on C++0x but that's not a critical thing, probably
Those tests are not rigorous, but still telling:
Large loop with raw arithmetic: 4 times faster.
Constructing and traversing a large "random" binary tree: 2 times faster (expected more here, no native calls are involved).
Bubblesort of ints array (with stock array addon): around 2.5 times faster.

I haven't had the chance to test it in production yet - first I have to eliminate the reliance on std::function as StlPort does not support it - but it appears very promising.
Hello everyone, I'm the owner of Blind Mind Studios, and responsible for the JIT. If you have any questions or suggestions, let me know.


Those tests are not rigorous, but still telling:
Large loop with raw arithmetic: 4 times faster.
Constructing and traversing a large "random" binary tree: 2 times faster (expected more here, no native calls are involved).
Bubblesort of ints array (with stock array addon): around 2.5 times faster


The binary tree is going slower than you might expect because the JIT doesn't natively handle context switches, for a variety of reasons. Any time a script function is called, the JIT has to return to the AngelScript context for it to allocate stack memory, locate pointers, etc. In the case of native calls, on the other hand, many are completely handled by the JIT.


wow, that's impressive! the only gripe I have about it is that it depends on C++0x but that's not a critical thing, probably


The primary use of C++0x is for lambdas due to so much use of local variables for the psuedo-assembly (each pseudo-register being a stack local variable).


Anyway, I'm excited to use this in the future! Thanks Andreas (for AngleScript) and Blind Mind Studios (for the JIT compiler)!


You're welcome!
You guys are awesome! Thank you so much for this contribution.
Rantings, ravings, and occasional insight from your typical code-monkey: http://angryprogrammingprimate.blogspot.com/
I think I´ve found a potential problem in the way the JIT is implemented in Angelscript. Blind Mind Studios´ web site shows a sample code for use with their JIT:

[source lang="java"]int main() {
asIScriptEngine* engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);

//Create the JIT Compiler. The build flags are explained below,
//as well as in as_jit.h
asCJitCompiler* jit = new asCJITCompiler(0);

//Enable JIT helper instructions; without these,
//the JIT will not be invoked
engine->SetEngineProperty(asEP_INCLUDE_JIT_INSTRUCTIONS, 1);

//Bind the JIT compiler to the engine
engine->SetJITCompiler(jit);

//.............Setup some other stuff, compile and execute your scripts.....

//Clean up your engine. Code pages will automatically be cleared
//by the JIT when the engine is released.
DiscardModules();
engine->Release();
delete jit;

return 0;
}[/source]

The problem is in the last lines: when we call engine->Release(), we´re not destroying the engine, we´re just telling it that we won´t hold a reference to it anymore. The engine might continue to exist for some undefined time. Then we destroy (delete) jit, which actually destroys the JIT compiler (which is kind of the correct thing to do, otherwise we might be leaking memory). What if after that the engine (which still might be alive) tries to access the now defunct JIT? Crash! I think the JIT should also be a reference counted object.

I think I´ve found a potential problem in the way the JIT is implemented in Angelscript. Blind Mind Studios´ web site shows a sample code for use with their JIT:

[source lang="java"]int main() {
asIScriptEngine* engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);

//Create the JIT Compiler. The build flags are explained below,
//as well as in as_jit.h
asCJitCompiler* jit = new asCJITCompiler(0);

//Enable JIT helper instructions; without these,
//the JIT will not be invoked
engine->SetEngineProperty(asEP_INCLUDE_JIT_INSTRUCTIONS, 1);

//Bind the JIT compiler to the engine
engine->SetJITCompiler(jit);

//.............Setup some other stuff, compile and execute your scripts.....

//Clean up your engine. Code pages will automatically be cleared
//by the JIT when the engine is released.
DiscardModules();
engine->Release();
delete jit;

return 0;
}[/source]

The problem is in the last lines: when we call engine->Release(), we´re not destroying the engine, we´re just telling it that we won´t hold a reference to it anymore. The engine might continue to exist for some undefined time. Then we destroy (delete) jit, which actually destroys the JIT compiler (which is kind of the correct thing to do, otherwise we might be leaking memory). What if after that the engine (which still might be alive) tries to access the now defunct JIT? Crash! I think the JIT should also be a reference counted object.


IMO you should just call SetJITCompiler(NULL);

/f
Alternatively you could store the jitcompiler as the script engine's user data, and register a cleanup function with the engine to delete the jitcompiler when the engine is deleted.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement