As the title says, is this a good or bad implementation?
local Rs = game:GetService("RunService")
Rs.Heartbeat:ConnectParallel(function()
local queue = {}
for i = 1, 200 do
table.insert(queue, function() end) --pretend these are all different functions
end
task.synchronize()
for _, v in ipairs(queue) do
v()
end
end)
No, that still only utilizes one thread in a serial execution phase.
You need multiple actors to utilize multiple threads, and they only actually multithread in parallel execution phases (thread safety and other complicated stuff).
Please note:
Parallel Luau is complicated and not fit for every situation, make sure your task will actually benefit from it, such as heavy computations that don’t perform datamodel operations.
Multiple scripts in one Actor run serially in that parallel thread.
You can’t access information from other Lua VMs normally, including variables and functions. To share data, use Actor:SendMessage() and SharedTables.
You should minimize the amount of data that needs to be transferred between Actors, as they are pretty slow compared to accessing information within the same VM, and you can run into issues with thread safety where data hasn’t been updated as you expect.
Currently, what you are doing is 1 parallel thread, in which you insert functions inside of a table, during the deserialized phases. Problem is that since this is being done from the same parallel thread, there aren’t any other parallel thread that can run simultaniously
In your example you also then run each function sequentially from the queue, wouldn’t the idea be to run the functions in parallel?
Using parallel lua is also a lot more complicated. Restrictions in place prevent us from sending functions to scripts within different actors (acting as parallel threads), meaning we either have to clone an actor and script, and each of those can work in parallel, or if parallel lua is used from 1 script to handle some task, then a scheduler needs to be used, and they get around the function limitation by using module scripts that contain a function, and each actor loads that module script
I have made my own scheduler, and you can find many others on the forum, some probably better than mine. I will say however that parallel lua is really hard to find good uses for, as the overhead is quite significant, and roblox, for some reason, limited it to only 3 cpu threads…