Optimizing Lua for cyclic execution
lua
Solution
You're correct, if the code is not changing, there is no reason to reprocess the code. Perhaps you could do something like the following:
luaL_loadbuffer(state, buff, len, name); // TODO: check return value
while (true) {
// sleep 10ms
lua_pushvalue(state, -1); // make another reference to the loaded chunk
lua_call(state, 0, 0);
}
You'll note that we simply duplicate the function reference on the top of the stack, since `lua_call` removes the function that it calls from the stack. This way, you do not lose a reference to the loaded chunk.
Problem
I'm exeucting my Lua script once per program cycle of 10 ms. using the same Lua_state (luaL_newstate called once in my app) Calling luaL_loadbuffer complies the script very fast for sure, still it seems unneccessary to do this every time the script is executed since the script does not change. Tried to save binary using lua_dump() and then execute it, but lua_pcall() didn't accept the binary for some reason. Any ideas on how to optimize? (LuaJIT is not an unfortenately an option here) Jan