One of the things I'm looking to change is the use of thin getter/setter functions. I think at the time I was implementing them I was still thinking like a C++ programmer. Basically a few of my class source files are full of functions that look like this:
Code: Select all
function entity.Entity:getAngle()
return self._angle
end
function entity.Entity:setAngle(angle)
self._angle = angle
end
Code A:
Code: Select all
local blah = { x = 0 }
for i = 0, 999999999 do
blah.x = i
end
Code: Select all
[aura typecheckspeedtest]$ time luajit typecheck.lua
real 0m1.018s
user 0m1.017s
sys 0m0.000s
[aura typecheckspeedtest]$ time lua5.1 typecheck.lua
real 0m40.431s
user 0m40.377s
sys 0m0.013s
Code: Select all
local blah = { x = 0 }
function blah:setX(newX)
if type(newX) ~= "number" then
error("Invalid type")
end
self.x = newX
end
for i = 0, 999999999 do
blah:setX(i)
end
Code: Select all
[aura typecheckspeedtest]$ time luajit typecheck.lua
real 0m1.019s
user 0m1.017s
sys 0m0.000s
[aura typecheckspeedtest]$ time lua5.1 typecheck.lua
real 2m36.141s
user 2m36.060s
sys 0m0.003s
Let's just say we don't use getters/setters and set the variable directly, bypassing any sort of sanity check... like, say we set x to "pig". The Lua runtime will eventually catch the error, but it will be in some other part of the program, as opposed to a type check catching the error right away.
When it comes to Lua, which is more important, speed and simplicity, or sanity checks?