Any difference if you don't use the local C? 'ffi.C.unit_set_local... Sent from my Windows Phone ________________________________ From: Niklas Frykholm<mailto:niklas.frykholm@xxxxxxxxx> Sent: 2/13/2015 9:19 AM To: luajit@xxxxxxxxxxxxx<mailto:luajit@xxxxxxxxxxxxx> Subject: FFI Performance We have a code base with tons of Lua <-> Native transitions, so I thought I should investigate what kind of performance improvements we could gain from switching our bindings to FFI instead of standard Lua (before committing and doing the work). So I did a simple test that just calls the same function in a loop using the two different bindings: function noffi_test(u, pos, count) local f = Unit.set_local_position for i=1,count do f(u, 0, pos) end end function ffi_test(u, pos, count) local C = ffi.C for i=1,count do C.unit_set_local_position(u, 0, pos) end end However, I found that in this test, the "noffi" path consistently outperforms the "ffi" path by a wide margin. (averaging 0.9 ms for the noffi path vs 2.3 ms for the ffi path with count at 10000). What could be the reason for this, and how can I investigate further what is going on? Both u and pos are lightuserdata in this code, and the standard Lua wrapper calls the FFI bound function to do the work, so it is definitely in the FFI binding that the performance is lost. // Niklas