[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: How can I optimize my metatables?
- From: "Soni \"They/Them\" L." <fakedme@...>
- Date: Thu, 30 Nov 2017 20:38:41 -0200
Hi!
I have some code that looks like this:
debug.setmetatable("", {__index=function(o,k)
local mt = metatables[coroutine.running()].string
if mt then
local __index = rawget(mt, "__index")
if type(__index) == "function" then
return __index(o,k)
else
return __index[k]
end
end
error()
end})
It takes the metatables for the basic Lua types (string, number, nil,
etc) and replaces them with a proxy metatable. This proxy metatable
forwards to a different table based on the currently running coroutine.
This gives me virtualization of those metatables.
It's really slow (3-4x slower[1] than the default string metatable) and
I'd like to make it faster. Is that possible?
[1] - I haven't actually benchmarked it, but default string metatable
gives about 2 table accesses per operation; this thing does at least 8
when using globals, and that doesn't take into account interpreter
overhead and all the function calls!
--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.