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!

90 Likes

Epiccccc, gonna use this :+1: Thanks for making it, hope more people discover this.

4 Likes

Very informative! Thank you very much!

You meda a spellyng erorr on “ConnectParallel”

3 Likes

And you 100% also made a big spelling mistake, this is just irony at work lmao.

But hey, thanks for the tutorial it’s been very helpful to try to see if I can improve some of my work that requires a lot of work to be done.

3 Likes

Very helpful and informative. Thank you.

Very Helpful tutorial! Your tutorials always go straight into the details, good work!

It’s surprising that this still is the only tutorial for such an incredibly useful feature.
The docs are pretty hard to understand so thank you for explaining it in a simple way.

5 Likes