Lua Optimisation Coding Tips

Post Reply
User avatar
Vadi
Posts: 5035
Joined: Sat Mar 14, 2009 3:13 pm

Lua Optimisation Coding Tips

Post by Vadi »

Lua 5.1 Notes

Easy stuff Advanced stuff
  • Memory allocation from the heap--e.g. repeatedly creating tables or closures--can slow things down.
  • Short inline expressions can be faster than function calls. t[#t+1] = 0 is faster than table.insert(t, 0).
  • Constant folding: x * (1/3) is just as fast as x * 0.33333333333333333 and is generally faster than x/3 on most CPUs (see multiplication note below). 1 + 2 + x, which is the same as (1+2) + x should be just as fast as 3 + x or x + (1 + 2) but faster than x + 1 + 2, which is the same as (x + 1) + 2 as is not necessary equivalent to the former. Note that addition of numbers on computers is generally not associative when overflow occurs, and the compiler doesn't even know whether x is a number or some other type with a non-associative __add metamethod. - LuaList:2006-03/msg00363.html
  • Factoring expressions: x*y+x*z+y*z --> x*(y+z) + y*z. Lua will not do this for you, particularly since it can't assume distributive and other common algebraic properties hold during numerical overflow.
Source: OptimisationCodingTips

Post Reply