Lua Optimization Tips
Remember, every second you spend in Lua is a second that the Server thread, Connection thread, and possibly one or more Emerge threads stay at a complete standstill! For this reason, intensive mods should designed with speed in mind.
N.B.
Many of these tips are specific to neither Lua nor Minetest.
Using Script API
Profiling
Very simple; no reason not to do so:
local t1 = os.clock()
--... work here ...
print(string.format("elapsed time: %.2fms", (os.clock() - t1) * 1000))
- Compare results often to identify bottlenecks before they become harder to find
Loop ordering
Always use z, y, x ordering unless there's a good reason not to.
- Keeps cache coherency
- Opens opportunity to use simple arithmetic to calculate indicies
Prefer local variables
- Should usually recompute variable contents instead of computing once and storing in a global variable
- For most minor computations, the benefit of local access outweighs the cost of re-computing their contents.
- Of course, the code in concern should be profiled instead of blindly following this general rule of thumb.
Use separate variables when possible instead of tables
- Even though putting associated variables inside of tables may keep things more orderly, the cost associated with performing a key lookup on access is high compared to a local variable reference.
- Some benchmarks have shown the opposite to be true when using LuaJIT, this must be verified.
Avoid having to re-enter the C++ side by calling API when not needed
- Although Lua is quite slow compared to native code, the amount of time spent in switching could be much greater.
- Again, the developer's discretion and profiling is needed to determine what the best course of action is.
Array index calculation
"Convenience" functions such as VoxelArea:index() are very attractive for simplicity's sake, but should not be used if maximizing speed is desired.
Try to use simple arithmetic like addition and subtraction for calculating array indices.
To deal with gaps in between multi-dimensional arrays:
- Keep a 'base' index variable that has the 0th sub-index of the current outer loop's index
- Add the stride length added to it per iteration of the outer loop
- Increment a copy of that variable within the inner loop
- Repeat for each dimension
This way, costly multiplications in the innermost loop can be eliminated.
Sample of a very fast loop done in this manner:
local idx_z_base = initial_index_offset
for z = z0, z1 do
local idx_y_base = idx_z_base
for y = y0, y1 do
local i = idx_y_base
for x = x0, x1 do
-- ... work here ...
i = i + 1
end
idx_y_base = idx_y_base + y_stride
end
idx_z_base = idx_z_base + z_stride
end
Use iteration instead of recursion
e.g. this:
-- depends on vector_extras
local set = vector.set_data_to_pos
local get = vector.get_data_from_pos
local remove = vector.remove_data_from_pos
local function get_tab2d(pos, func, max)
local z,y,x = vector.unpack(pos)
local tab = {{z,x}}
local tab_avoid = {}
set(tab_avoid, z,x,0, true)
local tab_done,num = {pos},2
local n = 1
while n do
local z,x = unpack(tab[n])
for i = -1,1,2 do
for _,p2 in pairs({
{z+i,x},
{z,x+i},
}) do
local z,x = unpack(p2)
if not get(tab_avoid, z,x,0)
and func({x=x, y=y, z=z}) then
set(tab_avoid, z,x,0, true)
tab_done[num] = p2
num = num+1
if max
and num > max then
return false
end
table.insert(tab, {z,x})
end
end
end
tab[n] = nil
n = next(tab)
end
return tab_done
end
Writing Script API
When working with arrays, use lua_rawgeti()/lua_rawseti()
- These work on plain integer indexes rather than field names and perform faster.
Prefer lua_newtable() to lua_createtable()
It has been observed that lua_newtable() is generally faster than lua_createtable() by a rather wide margin.
- This might seem counterintuitive, as lua_createtable() preallocates a known amount of memory in bulk to save steps, but it has a very negative effect on the cache for situations where the performance improvement would have otherwise made a difference.