lua-users home
lua-l archive

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


"Nick Trout" <nick@videosystem.co.uk> wrote:
> The downside to this is ofcause performance and the problem with
> dertermining the cpu time for a function... Lua will never be "real-time". 
I
> have a very good example.. I buildt a robot that used Lua for the AI... 
And
> I had to stop the robot at some specific times when Lua did
> garbage-collection. If I kept it running while the garbage collection took
> place the robot might drive off-course in the 1/3 of a second it took to 
do
> it.

One trick to avoiding this problem is create as few new tables as possible 
in your
program - since it's the tables that are garbage collected. For instance, if 
you're 
doing vector math with tables like:

  newtable = table1:add_(table2)

Instead of creating an returning a new table, have the operation apply to 
the
first table:

  table1 = table1:add_(table2)

And if you really need a new table for the result do something like this:

  newtable = table1:clone()
  newtable = newtable:add_(table2)

I've found that this technique not only avoids collecting excess garbage, 
but
speeds up the program quite a bit since allocating memory for the new tables 

can also be expensive.

Steve