by qikInNdOutReply · 4y ago · 0 comments
The lua code by itself did not ran long enough to actually profit much from optimizations much.
So, back then we did discuss possible solutions, and one idea was to basically notice a upcoming C-Call in bytecode ahead of execution, detect the stability of the arguments ahead of time. A background thread, extracts the values, perform the Call-processing of arguments and return pushes the values onto the stack, finally setting a "valid" bit to unblock the c-call (which by then actually is no longer a call). Both sides never have a complete cache eviction and live happily ever after. Unfortunatly i have a game dev addiction, so nothing ever came of it.
But similar minds might have pushed similar ideas.. so asking the hive for this jive. Anyone ever seen something similar in the wild?
Ultimately the script in a game engine either makes a bunch of API calls, so we want the "system call overhead" to be that of an opaque function call (4ns), and sometimes you share memory in order to read out properties and put abstractions on them (probably not relevant to Lua). So, it's important to get going fast, and to be able to make hundreds of engine API calls without unnecessary costs.
Benchmarks: https://gist.github.com/fwsGonzo/2f4518b66b147ee657d64496811...
I thought FFI promised almost seamless Lua->C calls. And can even pass Lua functions into C with native signatures (but this bridge is still claimed expensive).
There is trace stiching meant to help with code that does a lot of c calls. This was disabled in 2015 April 28 due to it being flawed but enabled again August 29 later that year with a working design.
Otherwise the recommendations are to reduce and maybe cache values on the lua side of things if you cannot use ffi.
Otherwise as mentioned in other comments if you can use ffi, use it. The performance benefit is significant.
The downside is that the ffi API is unsafe so in the context of a game development you'd have to be careful about third party scripts from modding or similar.
Another downside is if you cannot use jit (like on iOS) the performance hit can be worse than using the lua c api