Performance Tips: the cost of allocation

Looking at the performance results of algorithms using arrays I noticed something very drastic. LuaJIT was terribly slow compared to the other languages.

All these examples use arrays. The main difference is that the Ladder examples use small local arrays to functions, while the other examples use large arrays as mem variables. Here’s a snippet of the code:

The Lua code uses the LuaJIT FFI to create C arrays. The motivation of using it was because, as show here, you can get better performance with C arrays compares to Lua arrays. This is because Lua does not have native arrays, they are tables indexed with integers.

For the line val dpt[4]; the generated Lua code looks as follows:

dpt = ffi.new("double[?]",4)

It looks like every time a local array is used, even for the simplest thing, a new object is allocated. When the object is not used, the memory needs to be collected. In comparison to C++, the code will look as follows:

double dpt[4];

The first thing I tried was changing the Lua code to use regular Lua arrays instead of the FFI.

Even though for large arrays the performance we pay a little bit in performance, the overall gain is bigger.

Conclusion

When using LuaJIT, allocating small and short lived objects using FFI has very large impact in the performance of the code.