Testing new optimization features
This commit is contained in:
parent
7353fde799
commit
e5ac4d3d9e
164
multi/init.lua
164
multi/init.lua
@ -29,6 +29,7 @@ local clock = os.clock
|
|||||||
local thread = {}
|
local thread = {}
|
||||||
local in_proc = false
|
local in_proc = false
|
||||||
local processes = {}
|
local processes = {}
|
||||||
|
local find_optimization = false
|
||||||
|
|
||||||
if not _G["$multi"] then
|
if not _G["$multi"] then
|
||||||
_G["$multi"] = {multi=multi,thread=thread}
|
_G["$multi"] = {multi=multi,thread=thread}
|
||||||
@ -113,7 +114,11 @@ end
|
|||||||
function multi.ForEach(tab,func)
|
function multi.ForEach(tab,func)
|
||||||
for i=1,#tab do func(tab[i]) end
|
for i=1,#tab do func(tab[i]) end
|
||||||
end
|
end
|
||||||
|
local CRef = {
|
||||||
|
Fire = function() end
|
||||||
|
}
|
||||||
|
|
||||||
|
local optimization_stats = {}
|
||||||
local ignoreconn = true
|
local ignoreconn = true
|
||||||
function multi:newConnection(protect,func,kill)
|
function multi:newConnection(protect,func,kill)
|
||||||
local c={}
|
local c={}
|
||||||
@ -128,8 +133,11 @@ function multi:newConnection(protect,func,kill)
|
|||||||
for i,v in pairs(t) do
|
for i,v in pairs(t) do
|
||||||
if v==self then
|
if v==self then
|
||||||
local ref = self:Connect(select(2,...))
|
local ref = self:Connect(select(2,...))
|
||||||
ref.root_link = select(1,...)
|
if ref then
|
||||||
return ref
|
ref.root_link = select(1,...)
|
||||||
|
return ref
|
||||||
|
end
|
||||||
|
return self
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return self:Connect(...)
|
return self:Connect(...)
|
||||||
@ -207,7 +215,10 @@ function multi:newConnection(protect,func,kill)
|
|||||||
if lock then return end
|
if lock then return end
|
||||||
for i=#call_funcs,1,-1 do
|
for i=#call_funcs,1,-1 do
|
||||||
if not call_funcs[i] then return end
|
if not call_funcs[i] then return end
|
||||||
pcall(call_funcs[i],...)
|
local suc, err = pcall(call_funcs[i],...)
|
||||||
|
if not suc then
|
||||||
|
print(err)
|
||||||
|
end
|
||||||
if kill then
|
if kill then
|
||||||
table.remove(call_funcs,i)
|
table.remove(call_funcs,i)
|
||||||
end
|
end
|
||||||
@ -231,6 +242,7 @@ function multi:newConnection(protect,func,kill)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function c:fastMode()
|
function c:fastMode()
|
||||||
|
if find_optimization then return self end
|
||||||
function self:Fire(...)
|
function self:Fire(...)
|
||||||
for i=1,#fast do
|
for i=1,#fast do
|
||||||
fast[i](...)
|
fast[i](...)
|
||||||
@ -341,16 +353,39 @@ function multi:newConnection(protect,func,kill)
|
|||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if find_optimization then
|
||||||
|
--
|
||||||
|
end
|
||||||
|
|
||||||
c.connect=c.Connect
|
c.connect=c.Connect
|
||||||
c.GetConnection=c.getConnection
|
c.GetConnection=c.getConnection
|
||||||
c.HasConnections = c.hasConnections
|
c.HasConnections = c.hasConnections
|
||||||
c.GetConnection = c.getConnection
|
c.GetConnection = c.getConnection
|
||||||
|
|
||||||
if not(ignoreconn) then
|
if not(ignoreconn) then
|
||||||
multi:create(c)
|
multi:create(c)
|
||||||
end
|
end
|
||||||
return c
|
return c
|
||||||
end
|
end
|
||||||
|
|
||||||
|
multi.enableOptimization = multi:newConnection()
|
||||||
|
multi.optConn = multi:newConnection(true)
|
||||||
|
multi.optConn(function(msg)
|
||||||
|
table.insert(optimization_stats, msg)
|
||||||
|
end)
|
||||||
|
|
||||||
|
function multi:getOptimizationConnection()
|
||||||
|
return multi.optConn
|
||||||
|
end
|
||||||
|
|
||||||
|
function multi:getOptimizationStats()
|
||||||
|
return optimization_stats
|
||||||
|
end
|
||||||
|
|
||||||
|
function multi:isFindingOptimizing()
|
||||||
|
return find_optimization
|
||||||
|
end
|
||||||
|
|
||||||
-- Used with ISO Threads
|
-- Used with ISO Threads
|
||||||
local function isolateFunction(func,env)
|
local function isolateFunction(func,env)
|
||||||
local dmp = string.dump(func)
|
local dmp = string.dump(func)
|
||||||
@ -531,10 +566,6 @@ function multi:newConnector()
|
|||||||
return c
|
return c
|
||||||
end
|
end
|
||||||
|
|
||||||
local CRef = {
|
|
||||||
Fire = function() end
|
|
||||||
}
|
|
||||||
|
|
||||||
multi.OnObjectCreated=multi:newConnection()
|
multi.OnObjectCreated=multi:newConnection()
|
||||||
multi.OnObjectDestroyed=multi:newConnection()
|
multi.OnObjectDestroyed=multi:newConnection()
|
||||||
multi.OnLoad = multi:newConnection(nil,nil,true)
|
multi.OnLoad = multi:newConnection(nil,nil,true)
|
||||||
@ -1093,6 +1124,62 @@ function thread.hold(n,opt)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function thread.hold(n,opt)
|
||||||
|
thread._Requests()
|
||||||
|
local opt = opt or {}
|
||||||
|
if type(opt)=="table" then
|
||||||
|
interval = opt.interval
|
||||||
|
if opt.cycles then
|
||||||
|
return yield(CMD, t_holdW, opt.cycles or 1, n or dFunc, interval)
|
||||||
|
elseif opt.sleep then
|
||||||
|
return yield(CMD, t_holdF, opt.sleep, n or dFunc, interval)
|
||||||
|
elseif opt.skip then
|
||||||
|
return yield(CMD, t_skip, opt.skip or 1, nil, interval)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if type(n) == "number" then
|
||||||
|
thread.getRunningThread().lastSleep = clock()
|
||||||
|
return yield(CMD, t_sleep, n or 0, nil, interval)
|
||||||
|
elseif type(n) == "table" and n.Type == "connector" then
|
||||||
|
local rdy = function()
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
n(function(a1,a2,a3,a4,a5,a6)
|
||||||
|
rdy = function()
|
||||||
|
if a1==nil then
|
||||||
|
return NIL,a2,a3,a4,a5,a6
|
||||||
|
end
|
||||||
|
return a1,a2,a3,a4,a5,a6
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
return yield(CMD, t_hold, function()
|
||||||
|
return rdy()
|
||||||
|
end, nil, interval)
|
||||||
|
elseif type(n) == "function" then
|
||||||
|
if find_optimization then
|
||||||
|
local cache = string.dump(n)
|
||||||
|
local f_str = tostring(n)
|
||||||
|
local good = true
|
||||||
|
for i=1,#func_cache do
|
||||||
|
if func_cache[i][1] == cache and func_cache[i][2] ~= f_str then
|
||||||
|
if not func_cache[i][3] then
|
||||||
|
multi.optConn:Fire("It's better to store a function to a variable than to use an anonymous function within the hold method!\n" .. debug.traceback())
|
||||||
|
func_cache[i][3] = true
|
||||||
|
end
|
||||||
|
good = false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if good then
|
||||||
|
table.insert(func_cache, {cache, f_str})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return yield(CMD, t_hold, n or dFunc, nil, interval)
|
||||||
|
else
|
||||||
|
error("Invalid argument passed to thread.hold(...)!")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function thread.holdFor(sec,n)
|
function thread.holdFor(sec,n)
|
||||||
thread._Requests()
|
thread._Requests()
|
||||||
return yield(CMD, t_holdF, sec, n or dFunc)
|
return yield(CMD, t_holdF, sec, n or dFunc)
|
||||||
@ -1744,6 +1831,10 @@ function multi.init(settings, realsettings)
|
|||||||
else
|
else
|
||||||
multi.mainloop = mainloop
|
multi.mainloop = mainloop
|
||||||
end
|
end
|
||||||
|
if settings.findopt then
|
||||||
|
find_optimization = true
|
||||||
|
multi.enableOptimization:Fire(multi, thread)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
return _G["$multi"].multi,_G["$multi"].thread
|
return _G["$multi"].multi,_G["$multi"].thread
|
||||||
end
|
end
|
||||||
@ -2093,5 +2184,64 @@ else
|
|||||||
multi.m.sentinel = newproxy(true)
|
multi.m.sentinel = newproxy(true)
|
||||||
getmetatable(multi.m.sentinel).__gc = multi.m.onexit
|
getmetatable(multi.m.sentinel).__gc = multi.m.onexit
|
||||||
end
|
end
|
||||||
|
local func_cache = {}
|
||||||
|
multi:newThread(function()
|
||||||
|
thread.skip()
|
||||||
|
if find_optimization then
|
||||||
|
|
||||||
|
function thread.hold(n,opt)
|
||||||
|
thread._Requests()
|
||||||
|
local opt = opt or {}
|
||||||
|
if type(opt)=="table" then
|
||||||
|
interval = opt.interval
|
||||||
|
if opt.cycles then
|
||||||
|
return yield(CMD, t_holdW, opt.cycles or 1, n or dFunc, interval)
|
||||||
|
elseif opt.sleep then
|
||||||
|
return yield(CMD, t_holdF, opt.sleep, n or dFunc, interval)
|
||||||
|
elseif opt.skip then
|
||||||
|
return yield(CMD, t_skip, opt.skip or 1, nil, interval)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if type(n) == "number" then
|
||||||
|
thread.getRunningThread().lastSleep = clock()
|
||||||
|
return yield(CMD, t_sleep, n or 0, nil, interval)
|
||||||
|
elseif type(n) == "table" and n.Type == "connector" then
|
||||||
|
local rdy = function()
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
n(function(a1,a2,a3,a4,a5,a6)
|
||||||
|
rdy = function()
|
||||||
|
if a1==nil then
|
||||||
|
return NIL,a2,a3,a4,a5,a6
|
||||||
|
end
|
||||||
|
return a1,a2,a3,a4,a5,a6
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
return yield(CMD, t_hold, function()
|
||||||
|
return rdy()
|
||||||
|
end, nil, interval)
|
||||||
|
elseif type(n) == "function" then
|
||||||
|
local cache = string.dump(n)
|
||||||
|
local f_str = tostring(n)
|
||||||
|
local good = true
|
||||||
|
for i=1,#func_cache do
|
||||||
|
if func_cache[i][1] == cache and func_cache[i][2] ~= f_str and not func_cache[i][3] then
|
||||||
|
multi:getOptimizationConnection():Fire("It's better to store a function to a variable than to use an anonymous function within the hold method!\n" .. debug.traceback())
|
||||||
|
func_cache[i][3] = true
|
||||||
|
good = false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if good then
|
||||||
|
table.insert(func_cache, {cache, f_str})
|
||||||
|
end
|
||||||
|
return yield(CMD, t_hold, n or dFunc, nil, interval)
|
||||||
|
else
|
||||||
|
error("Invalid argument passed to thread.hold(...)!")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
-- Add more Overrides
|
||||||
|
end
|
||||||
|
end).OnError(print)
|
||||||
|
|
||||||
return multi
|
return multi
|
||||||
@ -239,13 +239,14 @@ function multi:newSystemThreadedConnection(name)
|
|||||||
|
|
||||||
thread:newThread("STC_SUB_MAN"..name,function()
|
thread:newThread("STC_SUB_MAN"..name,function()
|
||||||
local item
|
local item
|
||||||
|
local sub_func = function() -- This will keep things held up until there is something to process
|
||||||
|
return c.subscribe:pop()
|
||||||
|
end
|
||||||
while true do
|
while true do
|
||||||
thread.yield()
|
thread.yield()
|
||||||
-- We need to check on broken connections
|
-- We need to check on broken connections
|
||||||
ping(c) -- Should return instantlly and process this in another thread
|
ping(c) -- Should return instantlly and process this in another thread
|
||||||
item = thread.hold(function() -- This will keep things held up until there is something to process
|
item = thread.hold(sub_func)
|
||||||
return c.subscribe:pop()
|
|
||||||
end)
|
|
||||||
if item[1] == c.CONN then
|
if item[1] == c.CONN then
|
||||||
multi.ForEach(c.links, function(link) -- Sync new connections
|
multi.ForEach(c.links, function(link) -- Sync new connections
|
||||||
item[2]:push{c.CONN, link}
|
item[2]:push{c.CONN, link}
|
||||||
|
|||||||
@ -77,6 +77,7 @@ function multi:newSystemThread(name, func, ...)
|
|||||||
c.creationTime = os.clock()
|
c.creationTime = os.clock()
|
||||||
c.alive = true
|
c.alive = true
|
||||||
c.priority = THREAD.Priority_Normal
|
c.priority = THREAD.Priority_Normal
|
||||||
|
local multi_settings = multi.defaultSettings
|
||||||
c.thread = lanes.gen("*",
|
c.thread = lanes.gen("*",
|
||||||
{
|
{
|
||||||
globals={ -- Set up some globals
|
globals={ -- Set up some globals
|
||||||
@ -88,6 +89,7 @@ function multi:newSystemThread(name, func, ...)
|
|||||||
},
|
},
|
||||||
priority=c.priority
|
priority=c.priority
|
||||||
},function(...)
|
},function(...)
|
||||||
|
require("multi"):init(multi_settings)
|
||||||
require("multi.integration.lanesManager.extensions")
|
require("multi.integration.lanesManager.extensions")
|
||||||
local has_error = true
|
local has_error = true
|
||||||
return_linda:set("returns",{func(...)})
|
return_linda:set("returns",{func(...)})
|
||||||
|
|||||||
135
test.lua
135
test.lua
@ -1,8 +1,10 @@
|
|||||||
package.path = "./?/init.lua;?.lua;lua5.4/share/lua/5.4/?/init.lua;lua5.4/share/lua/5.4/?.lua;"--..package.path
|
package.path = "./?/init.lua;?.lua;lua5.4/share/lua/5.4/?/init.lua;lua5.4/share/lua/5.4/?.lua;"--..package.path
|
||||||
package.cpath = "lua5.4/lib/lua/5.4/?/core.dll;"--..package.cpath
|
package.cpath = "lua5.4/lib/lua/5.4/?/core.dll;"--..package.cpath
|
||||||
multi, thread = require("multi"):init{print=true}
|
multi, thread = require("multi"):init{print=true}
|
||||||
-- GLOBAL, THREAD = require("multi.integration.lanesManager"):init()
|
GLOBAL, THREAD = require("multi.integration.lanesManager"):init()
|
||||||
|
multi:getOptimizationConnection()(function(msg)
|
||||||
|
print(msg)
|
||||||
|
end)
|
||||||
-- local conn1, conn2, conn3 = multi:newConnection(), multi:newConnection(), multi:newConnection()
|
-- local conn1, conn2, conn3 = multi:newConnection(), multi:newConnection(), multi:newConnection()
|
||||||
-- thread:newThread(function()
|
-- thread:newThread(function()
|
||||||
-- print("Awaiting status")
|
-- print("Awaiting status")
|
||||||
@ -23,77 +25,74 @@ multi, thread = require("multi"):init{print=true}
|
|||||||
-- conn3:Fire()
|
-- conn3:Fire()
|
||||||
-- end)
|
-- end)
|
||||||
|
|
||||||
|
local conn = multi:newSystemThreadedConnection("conn"):init()
|
||||||
|
|
||||||
|
multi:newSystemThread("Thread_Test_1", function()
|
||||||
|
local multi, thread = require("multi"):init()
|
||||||
|
local conn = GLOBAL["conn"]:init()
|
||||||
|
local console = THREAD.getConsole()
|
||||||
|
conn(function(a,b,c)
|
||||||
|
console.print(THREAD:getName().." was triggered!",a,b,c)
|
||||||
|
end)
|
||||||
|
multi:mainloop()
|
||||||
|
end)
|
||||||
|
|
||||||
|
multi:newSystemThread("Thread_Test_2", function()
|
||||||
|
local multi, thread = require("multi"):init()
|
||||||
|
local conn = GLOBAL["conn"]:init()
|
||||||
|
local console = THREAD.getConsole()
|
||||||
|
conn(function(a,b,c)
|
||||||
|
console.print(THREAD:getName().." was triggered!",a,b,c)
|
||||||
|
end)
|
||||||
|
multi:newAlarm(2):OnRing(function()
|
||||||
|
console.print("Fire 2!!!")
|
||||||
|
conn:Fire(4,5,6)
|
||||||
|
THREAD.kill()
|
||||||
|
end)
|
||||||
|
|
||||||
-- local conn = multi:newSystemThreadedConnection("conn"):init()
|
multi:mainloop()
|
||||||
|
end)
|
||||||
|
local console = THREAD.getConsole()
|
||||||
|
conn(function(a,b,c)
|
||||||
|
console.print("Mainloop conn got triggered!",a,b,c)
|
||||||
|
end)
|
||||||
|
|
||||||
-- multi:newSystemThread("Thread_Test_1",function()
|
alarm = multi:newAlarm(1)
|
||||||
-- local multi, thread = require("multi"):init()
|
alarm:OnRing(function()
|
||||||
-- local conn = GLOBAL["conn"]:init()
|
console.print("Fire 1!!!")
|
||||||
-- local console = THREAD.getConsole()
|
conn:Fire(1,2,3)
|
||||||
-- conn(function(a,b,c)
|
end)
|
||||||
-- console.print(THREAD:getName().." was triggered!",a,b,c)
|
|
||||||
-- end)
|
|
||||||
-- multi:mainloop()
|
|
||||||
-- end)
|
|
||||||
|
|
||||||
-- multi:newSystemThread("Thread_Test_2",function()
|
alarm = multi:newAlarm(3):OnRing(function()
|
||||||
-- local multi, thread = require("multi"):init()
|
multi:newSystemThread("Thread_Test_3",function()
|
||||||
-- local conn = GLOBAL["conn"]:init()
|
local multi, thread = require("multi"):init()
|
||||||
-- local console = THREAD.getConsole()
|
local conn = GLOBAL["conn"]:init()
|
||||||
-- conn(function(a,b,c)
|
local console = THREAD.getConsole()
|
||||||
-- console.print(THREAD:getName().." was triggered!",a,b,c)
|
conn(function(a,b,c)
|
||||||
-- end)
|
console.print(THREAD:getName().." was triggered!",a,b,c)
|
||||||
-- multi:newAlarm(2):OnRing(function()
|
end)
|
||||||
-- console.print("Fire 2!!!")
|
multi:newAlarm(4):OnRing(function()
|
||||||
-- conn:Fire(4,5,6)
|
console.print("Fire 3!!!")
|
||||||
-- THREAD.kill()
|
conn:Fire(7,8,9)
|
||||||
-- end)
|
end)
|
||||||
|
multi:mainloop()
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
-- multi:mainloop()
|
multi:newSystemThread("Thread_Test_4",function()
|
||||||
-- end)
|
local multi, thread = require("multi"):init()
|
||||||
-- local console = THREAD.getConsole()
|
local conn = GLOBAL["conn"]:init()
|
||||||
-- conn(function(a,b,c)
|
local conn2 = multi:newConnection()
|
||||||
-- console.print("Mainloop conn got triggered!",a,b,c)
|
local console = THREAD.getConsole()
|
||||||
-- end)
|
multi:newAlarm(2):OnRing(function()
|
||||||
|
conn2:Fire()
|
||||||
-- alarm = multi:newAlarm(1)
|
end)
|
||||||
-- alarm:OnRing(function()
|
multi:newThread(function()
|
||||||
-- console.print("Fire 1!!!")
|
console.print("Conn Test!")
|
||||||
-- conn:Fire(1,2,3)
|
thread.hold(conn + conn2)
|
||||||
-- end)
|
console.print("It held!")
|
||||||
|
end)
|
||||||
-- alarm = multi:newAlarm(3):OnRing(function()
|
multi:mainloop()
|
||||||
-- multi:newSystemThread("Thread_Test_3",function()
|
end)
|
||||||
-- local multi, thread = require("multi"):init()
|
|
||||||
-- local conn = GLOBAL["conn"]:init()
|
|
||||||
-- local console = THREAD.getConsole()
|
|
||||||
-- conn(function(a,b,c)
|
|
||||||
-- console.print(THREAD:getName().." was triggered!",a,b,c)
|
|
||||||
-- end)
|
|
||||||
-- multi:newAlarm(4):OnRing(function()
|
|
||||||
-- console.print("Fire 3!!!")
|
|
||||||
-- conn:Fire(7,8,9)
|
|
||||||
-- end)
|
|
||||||
-- multi:mainloop()
|
|
||||||
-- end)
|
|
||||||
-- end)
|
|
||||||
|
|
||||||
-- multi:newSystemThread("Thread_Test_4",function()
|
|
||||||
-- local multi, thread = require("multi"):init()
|
|
||||||
-- local conn = GLOBAL["conn"]:init()
|
|
||||||
-- local conn2 = multi:newConnection()
|
|
||||||
-- local console = THREAD.getConsole()
|
|
||||||
-- multi:newAlarm(2):OnRing(function()
|
|
||||||
-- conn2:Fire()
|
|
||||||
-- end)
|
|
||||||
-- multi:newThread(function()
|
|
||||||
-- console.print("Conn Test!")
|
|
||||||
-- thread.hold(conn + conn2)
|
|
||||||
-- console.print("It held!")
|
|
||||||
-- end)
|
|
||||||
-- multi:mainloop()
|
|
||||||
-- end)
|
|
||||||
|
|
||||||
multi:mainloop()
|
multi:mainloop()
|
||||||
Loading…
x
Reference in New Issue
Block a user