LuaJIT glue logic for software

In electronics glue logic is the informal name for the (often) off the cuff implementation of logic that “glues” different chips together.  LuaJIT is as you might expect a Just In Time compiler for the Lua programming language.  There is a small caveat in that LuaJIT isn’t compatible with the very latest version of Lua (it’s compatible with version 5.1), that said the advantages of LuaJIT are such that frankly that’s really a non issue.

The first advantage is as you might expect speed – which is really quite impressive! such that the author is prepared to claim “It’s widely considered to be one of the fastest dynamic language implementations. ” and I have to agree that we are talking substantial differences in speed here.

What is particularly impressive however is the FFI (foreign function interface) accessing a C library (be it you own or an existing library) is done in a really easy to use way.  Not just functions but C stucts and shock horror pointers are really easy to use.  C data structures that are created for you by LuaJIT are managed by the GC (garbage collector).  Should you actually need to manually allocate memory, its possible to specify a finalizer when you do the allocation, this finalizer will eventually get called once the variable no longer has any references.

Lets be clear here there is no “wrapper” going on here no extra C library using cumbersome Lua stacks to communicate, just load the library, declare the function signature and once that’s done just call it at will…

Now while this is all great, the cost is – (in the authors words) “no hand holding”, which is actually a refreshing change, often there are no end language compromises to protect the programmer from themselves, but equally I’ve not managed to break it (yet!)

While a more typical use for Lua is as a scripting engine, I think that it can be taken just a little step further and use it to actually create a complete game…

This is where the idea of logic glue comes in, by using well established cross platform libraries (for example SDL) to provide the heavy lifting you can “glue” these libraries together using Lua to provide the actual game logic – mainly because accessing C is so straight forward.  There is sufficient power from LuaJIT to even do a fair amount of processing for example to try to see how much it would stress the cpu, each frame (60 FPS) I draw a random pattern of small 16×16 squares anywhere from 4,000 to 8,000 of them without even seeing any activity on my desktop CPU meter widget, that’s Lua code doing the loop and SDL doing just the drawing.  I then decided to throw up a bunch of alpha blended, rotated and scaled “sprites”, at the point the screen was getting over cluttered I decided that for a 2D game there is more than enough power ( and still not bothering the cpu)

Okay far from scientific but its more than enough to see if you’re in the same ball park, lets face it with so called “real world” benchmarks its all too easy to be accidentally measuring something related but not entirely what you want to measure, trying to get exact figures is frequently pointless….

So you have the rapid development advantages of Lua with the power and speed of easy to access mature cross platform C libraries, as you can see a very powerful combination.

For the paranoid it’s even a very easy hack to embed LuaJIT generated bytecode into a simple C runner application…

Leave a Reply

Your email address will not be published. Required fields are marked *