lua-users home
lua-l archive

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


actually the first time I call lua I open a state and load/compile the script then
every time I call lua I do it this way (in your "not to do list"):



   ...push args

   lua_pcall(L,...)  --that is what I mean by ''call''

I have also try to measure the time of this from beginning to the end,
I mostly obtain several million calls per second as you mentionned but sometimes it takes 0.01 sec of execution (this loop). I have try to see if it corresponds to a garbage collection cycle by stopping it but didn't change anything. Having 0.01 sec sometimes for one lua vm is not a problem but when running an hundred of lua vm it's a little bit too much.


Mike Pall wrote:


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:

Even more ideas can be found by reading through fully optimized
Lua code. Shameless plug (I wrote most of these):

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.


fn:David HERVIOU
org;quoted-printable:Ecole Nationale d'Ing=C3=A9nieurs de Brest
adr;quoted-printable;dom:BP 38;;25, rue Claude Chappe;Plouzan=C3=A9;France;29280
title;quoted-printable:Centre Europ=C3=A9en de R=C3=A9alit=C3=A9 Virtuelle
tel;work:+33 (0)2 98 05 89 45
tel;fax:+33 (0)2 98 05 89 79
note;quoted-printable:Doctorant En Informatique au laboratoire LISyC (Laboratoire d'Informatiqu=
	e des Syst=C3=A8mes Complexes) dans le cadre du projet AR=C3=A9Vi.