I have (not for everyone interesting) some results: I wanted to create an entity-manager with lua metatales. But before that I made some tests about performance.
First some code:
Implemenation with metatable:
Code: Select all
t={} -- basetable
mt={} -- metatable
mt.mytab={}
mt.__newindex=function(t,k,v)
rawset(mt.mytab, k, {val = v, valid = math.random()})
end
mt.__index=function(t,k)
local tab = rawget(mt.mytab, k)
if tab and tab.valid < 0.05 then
return tab.val
end
return false
end
setmetatable(t,mt)
for i=1, 1000000 do
t[math.random(1, 20000000)] = i
end
count = 0
for i=1, 20000000 do
if t[i] then
count = count + 1
end
end
print(count)
Code: Select all
bla = {}
bla.t={} -- basetable
for i=1, 1000000 do
bla.t[math.random(1, 20000000)] = {
val = i,
valid = math.random()
}
end
count = 0
for i=1, 20000000 do
if bla.t[i] and bla.t[i].valid < 0.05 then
count = count + 1
end
end
print(count)
With metatable: 6.161s
Without metatable: 3.557s
Conclusion: Metatables makes Lua slower by 100%.
Consequnce: No way to use metatables for a game as Factorio is.
Well, I think most of you did know that, but maybe for others it's new. So I wanted that to share with you