I am using the Lua "C" API function luaL_loadbuffer()
to run Lua code.
I have a small handful of Lua chunks that I am calling many, many times. But every time I call luaL_loadbuffer()
the chunk gets recompiled. This seems hugely inefficient. Most of the code referenced by the chunk is precompiled, but why do I need to recompile the chunk itself every time? How can I avoid this recompilation?
Can I pass a precompiled chunk to luaL_loadbuffer()
?
Can I make a full copy of the returned Lua stack and re-use it?
Is there some other clever approach?
Continue to use luaL_loadbuffer
to load scripts. Load here means precompile. Just save the function left on the stack somewhere in your program (or leave it on the stack if you can). When the time comes to run a script, lua_pushvalue
and lua_pcall
it.