mirror of
https://github.com/MightyPirates/OpenComputers.git
synced 2025-09-19 04:06:43 -04:00
Improved performance of component callbacks.
Enforcing tighter time limit on user __gc methods.
This commit is contained in:
parent
e03e63323c
commit
c124a650df
@ -80,12 +80,19 @@ sandbox = {
|
|||||||
setmetatable = function(t, mt)
|
setmetatable = function(t, mt)
|
||||||
local gc = rawget(mt, "__gc")
|
local gc = rawget(mt, "__gc")
|
||||||
if type(gc) == "function" then
|
if type(gc) == "function" then
|
||||||
|
-- For all user __gc functions we enforce a much tighter deadline.
|
||||||
|
-- This is because these functions may be called from the main
|
||||||
|
-- thread under certain circumstanced (such as when saving the world),
|
||||||
|
-- which can lead to noticeable lag if the __gc function behaves badly.
|
||||||
rawset(mt, "__gc", function(self)
|
rawset(mt, "__gc", function(self)
|
||||||
|
local oldDeadline, oldHitDeadline = deadline, hitDeadline
|
||||||
local co = coroutine.create(gc)
|
local co = coroutine.create(gc)
|
||||||
debug.sethook(co, checkDeadline, "", hookInterval)
|
debug.sethook(co, checkDeadline, "", hookInterval)
|
||||||
|
deadline, hitDeadline = math.min(oldDeadline, computer.realTime() + 0.5), true
|
||||||
local result, reason = coroutine.resume(co, self)
|
local result, reason = coroutine.resume(co, self)
|
||||||
debug.sethook(co)
|
debug.sethook(co)
|
||||||
checkDeadline()
|
checkDeadline()
|
||||||
|
deadline, hitDeadline = oldDeadline, oldHitDeadline
|
||||||
if not result then
|
if not result then
|
||||||
error(reason, 0)
|
error(reason, 0)
|
||||||
end
|
end
|
||||||
@ -440,17 +447,18 @@ end
|
|||||||
|
|
||||||
local libcomponent
|
local libcomponent
|
||||||
|
|
||||||
|
local proxyCache = setmetatable({}, {__mode="v"})
|
||||||
|
local proxyDirectCache = setmetatable({}, {__mode="v"})
|
||||||
|
|
||||||
local componentCallback = {
|
local componentCallback = {
|
||||||
__call = function(self, ...)
|
__call = function(self, ...)
|
||||||
return libcomponent.invoke(self.address, self.name, ...)
|
return invoke(component, not not proxyDirectCache[self], self.address, self.name, ...)
|
||||||
end,
|
end,
|
||||||
__tostring = function(self)
|
__tostring = function(self)
|
||||||
return libcomponent.doc(self.address, self.name) or "function"
|
return libcomponent.doc(self.address, self.name) or "function"
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
local proxyCache = setmetatable({}, {__mode="v"})
|
|
||||||
|
|
||||||
libcomponent = {
|
libcomponent = {
|
||||||
doc = function(address, method)
|
doc = function(address, method)
|
||||||
checkArg(1, address, "string")
|
checkArg(1, address, "string")
|
||||||
@ -500,8 +508,9 @@ libcomponent = {
|
|||||||
if not methods then
|
if not methods then
|
||||||
return nil, reason
|
return nil, reason
|
||||||
end
|
end
|
||||||
for method in pairs(methods) do
|
for method, direct in pairs(methods) do
|
||||||
proxy[method] = setmetatable({address=address,name=method}, componentCallback)
|
proxy[method] = setmetatable({address=address,name=method}, componentCallback)
|
||||||
|
proxyDirectCache[proxy[method]] = direct
|
||||||
end
|
end
|
||||||
proxyCache[address] = proxy
|
proxyCache[address] = proxy
|
||||||
return proxy
|
return proxy
|
||||||
|
Loading…
x
Reference in New Issue
Block a user