From 988834b4795137c9b4b51356a2a933bfd03d5abf Mon Sep 17 00:00:00 2001 From: ccuser44 <68124053+ccuser44@users.noreply.github.com> Date: Sun, 2 Jun 2024 12:34:06 +0300 Subject: [PATCH 01/17] Add multithreading handler --- MainModule/Server/Shared/FiOne.lua | 47 +++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/MainModule/Server/Shared/FiOne.lua b/MainModule/Server/Shared/FiOne.lua index a1266cccaa..9e642897f5 100644 --- a/MainModule/Server/Shared/FiOne.lua +++ b/MainModule/Server/Shared/FiOne.lua @@ -460,6 +460,51 @@ local function on_lua_error(failed, err) error(string.format("%s:%i: %s", src, line, err), 0) end +local function new_threaded_closure(proto, env, upval) + local actor = Instance.new("Actor") + local clone = script:Clone() + local runner = script.Runner:Clone() + local event = Instance.new("BindableEvent") + local tag = 0 + + for _, v in clone:GetChildren() do + if v:IsA("Actor") then + v:Destroy() + end + end + + clone.Parent = actor + runner.Parent = clone + event.Name = "ReturnPass" + event.Parent = clone + actor.Parent = script + + actor:SendMessage("lua_wrap_state", proto, env, upval) + + return function(...) + tag = tag + 1 + local currentTag = tag + local routine = coroutine.create(function() return coroutine.yield() end) + local connection = event.Event:Connect(function(eventTag, ...) + if eventTag == currentTag then + coroutine.resume(routine, ...) + end + end) + + actor:SendMessage("run_callback", currentTag, ...) + + local args = table.pack(coroutine.resume()) + + connection:Disconnect() + + if not args[1] then + error(args[2], 2) + end + + return table.unpack(args, 2) + end +end + local function run_lua_func(state, env, upvals) local code = state.code local subs = state.subs @@ -854,7 +899,7 @@ local function run_lua_func(state, env, upvals) pc = pc + nups end - memory[inst.A] = lua_wrap_state(sub, env, uvlist) + memory[inst.A] = (sub.source and string.sub(sub.source, 1, 39) == "SUPER_SECRET_ADONIS_MULTITHREAD_PREFIX_") and new_threaded_closure(sub, env, uvlist) or lua_wrap_state(sub, env, uvlist) else --[[TESTSET]] local A = inst.A From c68f89a4135036ad93695b56b754576d4e14099d Mon Sep 17 00:00:00 2001 From: ccuser44 <68124053+ccuser44@users.noreply.github.com> Date: Sun, 2 Jun 2024 12:35:20 +0300 Subject: [PATCH 02/17] Rename MainModule/Server/Shared/FiOne.lua to MainModule/Server/Shared/FiOne/init.lua --- MainModule/Server/Shared/{FiOne.lua => FiOne/init.lua} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename MainModule/Server/Shared/{FiOne.lua => FiOne/init.lua} (100%) diff --git a/MainModule/Server/Shared/FiOne.lua b/MainModule/Server/Shared/FiOne/init.lua similarity index 100% rename from MainModule/Server/Shared/FiOne.lua rename to MainModule/Server/Shared/FiOne/init.lua From e0a5d950499a5313ea2337c515ccf69d75759330 Mon Sep 17 00:00:00 2001 From: ccuser44 <68124053+ccuser44@users.noreply.github.com> Date: Sun, 2 Jun 2024 12:37:38 +0300 Subject: [PATCH 03/17] Create Runner.lua --- MainModule/Server/Shared/FiOne/Runner.lua | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 MainModule/Server/Shared/FiOne/Runner.lua diff --git a/MainModule/Server/Shared/FiOne/Runner.lua b/MainModule/Server/Shared/FiOne/Runner.lua new file mode 100644 index 0000000000..f1e18177ef --- /dev/null +++ b/MainModule/Server/Shared/FiOne/Runner.lua @@ -0,0 +1,14 @@ +--!native +-- Multithreader - ccuser44 +local fiOne = require(script.FiOne) +local actor = script.Parent +local event = script.ReturnPass +local callback = function(...) return ... end + +actor:BindToMessage("lua_wrap_state", function(proto, env, upval) + callback = fiOne.lua_wrap_state(proto, env, upval) +end) + +actor:BindToMessageParallel("run_callback", function(tag, ...) + event:Fire(tag, pcall(callback, ...)) +end) From 920667a377d19c72968b78200354c7989f04c74d Mon Sep 17 00:00:00 2001 From: ccuser44 <68124053+ccuser44@users.noreply.github.com> Date: Sun, 2 Jun 2024 12:47:52 +0300 Subject: [PATCH 04/17] Fix typo --- MainModule/Server/Shared/FiOne/init.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/MainModule/Server/Shared/FiOne/init.lua b/MainModule/Server/Shared/FiOne/init.lua index 9e642897f5..fe16985ea9 100644 --- a/MainModule/Server/Shared/FiOne/init.lua +++ b/MainModule/Server/Shared/FiOne/init.lua @@ -460,6 +460,7 @@ local function on_lua_error(failed, err) error(string.format("%s:%i: %s", src, line, err), 0) end +-- ccuser44 added multithreading support local function new_threaded_closure(proto, env, upval) local actor = Instance.new("Actor") local clone = script:Clone() @@ -493,7 +494,7 @@ local function new_threaded_closure(proto, env, upval) actor:SendMessage("run_callback", currentTag, ...) - local args = table.pack(coroutine.resume()) + local args = table.pack(coroutine.resume(routine)) connection:Disconnect() From 8a65b352bc574763da2e7b282b26f675b7f89a55 Mon Sep 17 00:00:00 2001 From: ccuser44 <68124053+ccuser44@users.noreply.github.com> Date: Fri, 18 Oct 2024 12:57:44 +0300 Subject: [PATCH 05/17] Add todo --- MainModule/Server/Shared/FiOne/init.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/MainModule/Server/Shared/FiOne/init.lua b/MainModule/Server/Shared/FiOne/init.lua index fe16985ea9..319717c914 100644 --- a/MainModule/Server/Shared/FiOne/init.lua +++ b/MainModule/Server/Shared/FiOne/init.lua @@ -461,6 +461,7 @@ local function on_lua_error(failed, err) end -- ccuser44 added multithreading support +-- TODO: Create multiple threads for closure and use a module and/or round robin load balancer to distribute usage local function new_threaded_closure(proto, env, upval) local actor = Instance.new("Actor") local clone = script:Clone() From a50866defa8854a74af19ccac40d5d57cc7eb55b Mon Sep 17 00:00:00 2001 From: ccuser44 <68124053+ccuser44@users.noreply.github.com> Date: Fri, 18 Oct 2024 13:09:45 +0300 Subject: [PATCH 06/17] Update and rename Runner.lua to Runner.server.lua --- .../Server/Shared/FiOne/{Runner.lua => Runner.server.lua} | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) rename MainModule/Server/Shared/FiOne/{Runner.lua => Runner.server.lua} (77%) diff --git a/MainModule/Server/Shared/FiOne/Runner.lua b/MainModule/Server/Shared/FiOne/Runner.server.lua similarity index 77% rename from MainModule/Server/Shared/FiOne/Runner.lua rename to MainModule/Server/Shared/FiOne/Runner.server.lua index f1e18177ef..d4d5125867 100644 --- a/MainModule/Server/Shared/FiOne/Runner.lua +++ b/MainModule/Server/Shared/FiOne/Runner.server.lua @@ -1,5 +1,11 @@ --!native --- Multithreader - ccuser44 +--[[ + Description: Wrapper for FiOne multithread closures + Author: github@ccuser44 + Date: 2024 + License: CC0 +]] + local fiOne = require(script.FiOne) local actor = script.Parent local event = script.ReturnPass From f0373d66f6138a1fc8e054e76fd5342f64917cf2 Mon Sep 17 00:00:00 2001 From: ccuser44 <68124053+ccuser44@users.noreply.github.com> Date: Fri, 18 Oct 2024 13:10:52 +0300 Subject: [PATCH 07/17] Create Runner.meta.json --- MainModule/Server/Shared/FiOne/Runner.meta.json | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 MainModule/Server/Shared/FiOne/Runner.meta.json diff --git a/MainModule/Server/Shared/FiOne/Runner.meta.json b/MainModule/Server/Shared/FiOne/Runner.meta.json new file mode 100644 index 0000000000..5077db135e --- /dev/null +++ b/MainModule/Server/Shared/FiOne/Runner.meta.json @@ -0,0 +1,5 @@ +{ + "properties": { + "Disabled": true + } +} From a7d2921f3e451164bf9fe0ec39e876ff1875db4f Mon Sep 17 00:00:00 2001 From: ccuser44 <68124053+ccuser44@users.noreply.github.com> Date: Fri, 18 Oct 2024 13:25:13 +0300 Subject: [PATCH 08/17] Fix disabling --- MainModule/Server/Shared/FiOne/init.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/MainModule/Server/Shared/FiOne/init.lua b/MainModule/Server/Shared/FiOne/init.lua index 319717c914..db952dde2c 100644 --- a/MainModule/Server/Shared/FiOne/init.lua +++ b/MainModule/Server/Shared/FiOne/init.lua @@ -480,6 +480,7 @@ local function new_threaded_closure(proto, env, upval) event.Name = "ReturnPass" event.Parent = clone actor.Parent = script + runner.Disabled = false actor:SendMessage("lua_wrap_state", proto, env, upval) From e65ca524f20e90b84a36f64c8c514a716f4f8dcb Mon Sep 17 00:00:00 2001 From: ccuser44 <68124053+ccuser44@users.noreply.github.com> Date: Fri, 18 Oct 2024 13:33:30 +0300 Subject: [PATCH 09/17] Fix runner naming --- MainModule/Server/Shared/FiOne/init.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/MainModule/Server/Shared/FiOne/init.lua b/MainModule/Server/Shared/FiOne/init.lua index db952dde2c..91daadd388 100644 --- a/MainModule/Server/Shared/FiOne/init.lua +++ b/MainModule/Server/Shared/FiOne/init.lua @@ -477,6 +477,7 @@ local function new_threaded_closure(proto, env, upval) clone.Parent = actor runner.Parent = clone + runner.Name = "MultithreadRunner_"..tostring(math.random()) event.Name = "ReturnPass" event.Parent = clone actor.Parent = script From 20fbe045e807435b8e1c8c00372872308b7b010e Mon Sep 17 00:00:00 2001 From: ccuser44 <68124053+ccuser44@users.noreply.github.com> Date: Fri, 18 Oct 2024 14:22:31 +0300 Subject: [PATCH 10/17] Fix typo --- MainModule/Server/Shared/FiOne/Runner.server.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/MainModule/Server/Shared/FiOne/Runner.server.lua b/MainModule/Server/Shared/FiOne/Runner.server.lua index d4d5125867..b2c31da55c 100644 --- a/MainModule/Server/Shared/FiOne/Runner.server.lua +++ b/MainModule/Server/Shared/FiOne/Runner.server.lua @@ -11,8 +11,8 @@ local actor = script.Parent local event = script.ReturnPass local callback = function(...) return ... end -actor:BindToMessage("lua_wrap_state", function(proto, env, upval) - callback = fiOne.lua_wrap_state(proto, env, upval) +actor:BindToMessage("wrap_state", function(proto, env, upval) + callback = fiOne.wrap_state(proto, env, upval) end) actor:BindToMessageParallel("run_callback", function(tag, ...) From e7dfcceeae321f6049d15f665e4d856de49c06bd Mon Sep 17 00:00:00 2001 From: ccuser44 <68124053+ccuser44@users.noreply.github.com> Date: Mon, 21 Oct 2024 14:49:06 +0300 Subject: [PATCH 11/17] Finally fix multithreading --- MainModule/Server/Shared/FiOne/init.lua | 27 +++++++++++++++---------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/MainModule/Server/Shared/FiOne/init.lua b/MainModule/Server/Shared/FiOne/init.lua index 91daadd388..b01882a2e1 100644 --- a/MainModule/Server/Shared/FiOne/init.lua +++ b/MainModule/Server/Shared/FiOne/init.lua @@ -1,4 +1,5 @@ --!native +--!nolint TableOperations --# selene: allow(divide_by_zero, multiple_statements, mixed_table) --[[ FiOne @@ -462,10 +463,13 @@ end -- ccuser44 added multithreading support -- TODO: Create multiple threads for closure and use a module and/or round robin load balancer to distribute usage +local isClient, actorContainer local function new_threaded_closure(proto, env, upval) + isClient = isClient or game:GetService("RunService"):IsClient() + actorContainer = actorContainer or script local actor = Instance.new("Actor") local clone = script:Clone() - local runner = script.Runner:Clone() + local runner = script[isClient and "LocalRunner" or "Runner"]:Clone() local event = Instance.new("BindableEvent") local tag = 0 @@ -475,30 +479,31 @@ local function new_threaded_closure(proto, env, upval) end end - clone.Parent = actor - runner.Parent = clone - runner.Name = "MultithreadRunner_"..tostring(math.random()) event.Name = "ReturnPass" - event.Parent = clone - actor.Parent = script + event.Parent = runner + clone.Parent = runner runner.Disabled = false + runner.Parent = actor + actor.Name = "MultithreadRunner_"..tostring(math.random()) + actor.Parent = actorContainer - actor:SendMessage("lua_wrap_state", proto, env, upval) + task.defer(actor.SendMessage, actor, "wrap_state", proto, env, upval) + + task.wait() return function(...) tag = tag + 1 local currentTag = tag - local routine = coroutine.create(function() return coroutine.yield() end) + local routine = coroutine.running() local connection = event.Event:Connect(function(eventTag, ...) if eventTag == currentTag then coroutine.resume(routine, ...) end end) - actor:SendMessage("run_callback", currentTag, ...) - - local args = table.pack(coroutine.resume(routine)) + task.defer(actor.SendMessage, actor, "run_callback", currentTag, ...) + local args = table.pack(coroutine.yield()) connection:Disconnect() if not args[1] then From b44242912517ee8f337a7044f193163f64163cdf Mon Sep 17 00:00:00 2001 From: ccuser44 <68124053+ccuser44@users.noreply.github.com> Date: Mon, 21 Oct 2024 15:13:38 +0300 Subject: [PATCH 12/17] Create LocalRunner.meta.json --- MainModule/Server/Shared/FiOne/LocalRunner.meta.json | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 MainModule/Server/Shared/FiOne/LocalRunner.meta.json diff --git a/MainModule/Server/Shared/FiOne/LocalRunner.meta.json b/MainModule/Server/Shared/FiOne/LocalRunner.meta.json new file mode 100644 index 0000000000..6233c02da7 --- /dev/null +++ b/MainModule/Server/Shared/FiOne/LocalRunner.meta.json @@ -0,0 +1,6 @@ +{ + "properties": { + "RunContext": "Client", + "Disabled": true + } +} From 0ee1f92c38d8e2413b3ff10812329a551afdcca3 Mon Sep 17 00:00:00 2001 From: ccuser44 <68124053+ccuser44@users.noreply.github.com> Date: Mon, 21 Oct 2024 15:14:41 +0300 Subject: [PATCH 13/17] Create LocalRunner.server.lua --- .../Shared/FiOne/LocalRunner.server.lua | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 MainModule/Server/Shared/FiOne/LocalRunner.server.lua diff --git a/MainModule/Server/Shared/FiOne/LocalRunner.server.lua b/MainModule/Server/Shared/FiOne/LocalRunner.server.lua new file mode 100644 index 0000000000..b2c31da55c --- /dev/null +++ b/MainModule/Server/Shared/FiOne/LocalRunner.server.lua @@ -0,0 +1,20 @@ +--!native +--[[ + Description: Wrapper for FiOne multithread closures + Author: github@ccuser44 + Date: 2024 + License: CC0 +]] + +local fiOne = require(script.FiOne) +local actor = script.Parent +local event = script.ReturnPass +local callback = function(...) return ... end + +actor:BindToMessage("wrap_state", function(proto, env, upval) + callback = fiOne.wrap_state(proto, env, upval) +end) + +actor:BindToMessageParallel("run_callback", function(tag, ...) + event:Fire(tag, pcall(callback, ...)) +end) From 030f44ef7053096b4d82f391f520ebdc4f5a52e1 Mon Sep 17 00:00:00 2001 From: ccuser44 <68124053+ccuser44@users.noreply.github.com> Date: Mon, 21 Oct 2024 15:18:35 +0300 Subject: [PATCH 14/17] Use more AST optimisations --- MainModule/Server/Shared/FiOne/Runner.server.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/MainModule/Server/Shared/FiOne/Runner.server.lua b/MainModule/Server/Shared/FiOne/Runner.server.lua index b2c31da55c..e099a07b9b 100644 --- a/MainModule/Server/Shared/FiOne/Runner.server.lua +++ b/MainModule/Server/Shared/FiOne/Runner.server.lua @@ -1,4 +1,5 @@ --!native +--!optimise 2 --[[ Description: Wrapper for FiOne multithread closures Author: github@ccuser44 From 92eb6df6ec28aa507b18e8f339947ec24d71a8d9 Mon Sep 17 00:00:00 2001 From: ccuser44 <68124053+ccuser44@users.noreply.github.com> Date: Mon, 21 Oct 2024 15:19:03 +0300 Subject: [PATCH 15/17] Use more AST optimisations --- MainModule/Server/Shared/FiOne/LocalRunner.server.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/MainModule/Server/Shared/FiOne/LocalRunner.server.lua b/MainModule/Server/Shared/FiOne/LocalRunner.server.lua index b2c31da55c..e099a07b9b 100644 --- a/MainModule/Server/Shared/FiOne/LocalRunner.server.lua +++ b/MainModule/Server/Shared/FiOne/LocalRunner.server.lua @@ -1,4 +1,5 @@ --!native +--!optimise 2 --[[ Description: Wrapper for FiOne multithread closures Author: github@ccuser44 From 6e8e5ff4553162386269c2fd3805fb70b37d98bb Mon Sep 17 00:00:00 2001 From: ccuser44 <68124053+ccuser44@users.noreply.github.com> Date: Sun, 27 Oct 2024 14:12:29 +0200 Subject: [PATCH 16/17] Fix typo --- MainModule/Server/Shared/FiOne/LocalRunner.server.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MainModule/Server/Shared/FiOne/LocalRunner.server.lua b/MainModule/Server/Shared/FiOne/LocalRunner.server.lua index e099a07b9b..cef01f0fc7 100644 --- a/MainModule/Server/Shared/FiOne/LocalRunner.server.lua +++ b/MainModule/Server/Shared/FiOne/LocalRunner.server.lua @@ -1,5 +1,5 @@ --!native ---!optimise 2 +--!optimize 2 --[[ Description: Wrapper for FiOne multithread closures Author: github@ccuser44 From a6cbe0b4bedb9a68c88161649a9af1f5c7d9b0a9 Mon Sep 17 00:00:00 2001 From: ccuser44 <68124053+ccuser44@users.noreply.github.com> Date: Sun, 27 Oct 2024 14:12:47 +0200 Subject: [PATCH 17/17] Fix typo --- MainModule/Server/Shared/FiOne/Runner.server.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MainModule/Server/Shared/FiOne/Runner.server.lua b/MainModule/Server/Shared/FiOne/Runner.server.lua index e099a07b9b..cef01f0fc7 100644 --- a/MainModule/Server/Shared/FiOne/Runner.server.lua +++ b/MainModule/Server/Shared/FiOne/Runner.server.lua @@ -1,5 +1,5 @@ --!native ---!optimise 2 +--!optimize 2 --[[ Description: Wrapper for FiOne multithread closures Author: github@ccuser44