lua-users home
lua-l archive

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

"Nick Trout" <> 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". 
> have a very good example.. I buildt a robot that used Lua for the AI... 
> 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 
> 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 
doing vector math with tables like:

  newtable = table1:add_(table2)

Instead of creating an returning a new table, have the operation apply to 
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, 
speeds up the program quite a bit since allocating memory for the new tables 

can also be expensive.