How to use Parallel Luau

First step is to enable parallel luau in your beta features. Then after that you gotta go create an actor instance, type this in your command prompt

local p = Instance.new("Actor") p.Parent = game.StarterGui

These actor instances basically act as parents for scripts which will have the parallel capabilties in them. All Parrallel luau does is run code multi threaded on your cpu. For example say I wanted to for loop some table right

local data = {}
for i=1,1000 do
   data[i] = i*i
end

If we weren’t using parrallel luau this would run serial. That basically means it works how you think it works, it will iterate 1000 times in order, without skipping anything. What Parrallel luau does is it makes it so that this for loop is ran multi threaded, thus each core on your cpu does less iterations. So to actually do this we need to create some sort of function for it. Then we need to bind it to be ran parallel. The Connect function for events basically means run this function serial and ConnectParallel means run it muti threaded. So a example of what this code could look like multi threaded might be

local bindable = Instance.new("BindableEvent")
bindable.Parent = script.Parent
bindable.Event:ConnectParralel(function()
local data = {}
for i=1,1000 do
   data[i] = i*i
end
end)

bindable:Fire()

Now why do I have this data array? Why not just directly output my data to say changing a parts color in game? Well the problem is we can’t write to anything outside of this actor. We can however read off of some objects in the explorer (except for some gui properties). However in-general if we wanted to change something, say the name to be the value of this index in which we have a thousand parts in the workspace named the index value, we would use the

task.synchronize()

function. This basically stops the parrallel-ness happening and goes back to serial, one core. Now we can write and read to anything within our game that we had access to before.

local bindable = Instance.new("BindableEvent")
bindable.Parent = script.Parent
bindable.Event:ConnectParralel(function()
local data = {}
for i=1,1000 do
   data[i] = i*i
end
task.synchronize()
for i=1,1000 do
  workspace[i].Color = 255-i*(255 - Color3.new(0,0,0))
end
end)

bindable:Fire()

You might be saying, well whats hte point in doing that if we gotta for loop everything again? Let me give a more practical example. Say I got this gui-based rendering engine

I need to do this complex math equation which takes a lot of resources. If I were to distribute the stuff on parrallel I can then go back to my serial task scheduler thingy and then update the color of the frame from there. Remember that its not the loop which makes the update slow (we are totally ignoring wait here) it’s the code in it. Our math function will take significantly longer than changing the color of the gui. Roblox has optimized the heck out of that. If we wanted to go back to multi-threaded we call

task.desynchronize()

For anybody who uses OpenCL task.desynchronize() is basically changing the kernel code.

Have fun!

22 Likes