Simple and efficient way of taking advantage of Roblox's Parallel Lua.
- Download the module or get it from the toolbox.
- Place the module in
ReplicatedStorage
(make sure there exists anActor
and aClientActor
as children of the module). - Make sure your hierachy looks like this:
Pointer
should be an ObjectValue
, Function
a BindableFunction
, and Script
a normal Script
(in the ClientActor
, it should be a LocalScript
) with the following content:
local functions = require(script.Parent.Pointer.Value)
script.Parent.Function.OnInvoke = function(func, ...)
task.desynchronize()
return functions[func](...)
end
- Create a module script including all the executable functions. It can look like this:
return {
Test = function(a, b)
for i = 1, 1e6 do
a, b = b, a
end
return a, b
end,
}
- Create a script, and type out the following:
local ParallelModule = require(path.to.module)(path.to.functions.module)
-- Create an array of parameters (Can be an array of tables as well)
local parameters = table.create(100, true) -- Just an example, you could put pretty anything here (as long as it's an array of parameters)
-- Returns an array of the returned results
local returnValues = ParallelModule("FunctionName", parameters)
print(returnValues)
- Upon calling
ParallelModule
, the function will yield until all of the parameters have been processed. Internally, we're simply iterating through the array and executing the function usingtask.spawn()
.
The output of an example run using the code below :
Here's the code that was used for the benchmark above:
local ParallelLua = require(ReplicatedStorage.ParallelLua)(ReplicatedStorage.Functions)
while true do
-- SEQUENTIAL LUAU
local start = os.clock()
for i = 1, 100 do
local a, b = 0, 1
for i = 1, 1e6 do
a, b = b, a
end
end
print("SEQ : ", os.clock() - start)
-- PARALLEL LUAU
local start = os.clock()
local parameters = table.create(100, { 0, 1 })
ParallelLua("Test", parameters)
print("PARA : ", os.clock() - start)
task.wait(1)
end