lua-users home
lua-l archive

Re: Lua vm call speed

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi,
herviou wrote:
> This weekend I have 
> pointed out that it can cost a non negligeable time to call the lua vm. 
Umm, this is usually not an issue? Lua can do several million
calls per second on a (slow) Pentium III.
Maybe your definition of "call" is different. Parsing a script
should only be done once (and closures created only once, too).
The resulting functions should be put into a table or an upvalue
of a C closure.
Something like:
 ... loop ... {
 lua_rawgeti(L, table_idx, idx); // Push table outside loop/use upvalue
 ... or ...
 lua_pushvalue(L, lua_upvalueindex(idx)); // For low # of functions
 ...
 lua_call(L, nargs, nresults);
 }
But avoid doing any of these on every iteration:
 lua_open() ... lua_close() // Recreates a Lua universe every time.
 lua_load()/luaL_load*() // Loads/parses a script every time.
 lua_pushcfunction()/lua_pushclosure() // Creates a C closure every time.
 lua_getglobal()/lua_setglobal() // Accesses a global every time.
There are more things to avoid on the Lua side of the code, too.
There's a Wiki page: http://lua-users.org/wiki/OptimisationTips
Even more ideas can be found by reading through fully optimized
Lua code. Shameless plug (I wrote most of these):
http://shootout.alioth.debian.org/gp4/benchmark.php?test=all&lang=lua&lang2=lua
> I'm working with Lua for creating IA in virtual drivers in a
> traffic road simulator.
BTW: this sounds like you should investigate Lua coroutines, too.
Look through the archive about Lua coroutines for AI in games.
Bye,
 Mike

AltStyle によって変換されたページ (->オリジナル) /