Closed jojo59516 closed 2 years ago
Obviously we all agree that a table index metamethod is better. Nevertheless I made a simple benchmark for this (with Lua 5.1.5 and LuaJIT 2.1.0-beta3 in interpreter mode). Before:
middleclass git/6102f67...*
❯ lua performance/test.lua
invoke unoverridden method 0.75
invoke overridden method 0.75
middleclass git/6102f67...*
❯ luajit -joff performance/test.lua
invoke unoverridden method 0.421875
invoke overridden method 0.421875
After:
middleclass git/master*
❯ lua performance/test.lua
invoke unoverridden method 0.359375
invoke overridden method 0.34375
middleclass git/master*
❯ luajit -joff performance/test.lua
invoke unoverridden method 0.234375
invoke overridden method 0.234375
performance/test.lua:
local time = require 'performance/time'
local class = require 'middleclass'
local Base = class('Base')
function Base:f()
return
end
local Derived = Base:subclass('Derived')
local obj = Derived()
time('invoke unoverridden method', function ()
for i = 1, 1000 do
obj:f()
end
end)
function Derived:f()
return
end
time('invoke overridden method', function ()
for i = 1, 1000 do
obj:f()
end
end)
Well, I added two test cases for checking instance's index
metamethod being a table, in order to increase code coverage :)
Hey, many thanks for this. I apologize for the time it took me to review this. I think this contribution is good. I appreciate that you even added some extra tests to increase coverage :)
Wow, thank you for the brilliant middleclass so much! :)
Hello. I found that a instance of a subclass always have a functional
__index
metamethod. But if I didn't override it's__index
metamethod in baseclass, it should be a table. It will be more efficient. I optimized infd25b1a
. And I think conditional branches of a wrapped__index
metamethod (returned by_createIndexWrapper()
) could be decreased. I optimized in63f83ea
. All of specs are passed.