Is :ConnectParallel() more performant?

I tried looking around and it seems like a lot of people know what it is but so little are giving examples of when to use it, how to use it and if it saves on performance.

PlayersService.PlayerAdded:ConnectParallel(RegisterPlayer)
WorkspaceService.DescendantAdded:ConnectParallel(RegisterPart)
local function RegisterPart(BasePart)
	if  BasePart:IsA("BasePart")
		and BasePart.CanCollide == true
		and BasePart.Parent:FindFirstChildWhichIsA("Humanoid") == nil
	then
		table.insert(PartRegistry, BasePart)
	end
end

I’m currently using it on any connection that can support it, am I using it wrong?

5 Likes

Gathered from Parallel Lua Beta - Updates / Announcements

From my knowledge, making it run in parallel will sync related (or exact) signals together.
Let’s say you were listening to Heartbeat in multiple scripts for example, instead of running them individually it will realize that there’s multiple and run Heartbeat once and execute everything using Heartbeat at once, instead of happening multiple times.

Now if I get this right, the Actor has to be different but the Signal which is being read has to be the same. If you’re reading the ChildAdded of Part1 and Part2 and parallel them, it will not execute at the same time (for obvious, performant reasons). However, if you were listening to ChildAdded in two different scripts it’s good practice to use ConnectParallel to lower RBXScriptSignal events firing too much.

In practicality, if it’s a signal for an event that is only connected once, just use Signal::Connect.

10 Likes
-- Good Practice

-- Script 1
local RunService = game:GetService('RunService')
RunService.Heartbeat:ConnectParallel(function()
    -- do something
end

-- Script 2
local RunService = game:GetService('RunService')
RunService.Heartbeat:ConnectParallel(function()
    -- do a different thing
end

These will run at the same time and only fire RunService.Heartbeat once.

-- Bad Practice

-- Script 1
local Part1 = workspace.Part1
Part1.ChildAdded:ConnectParallel(function(Child)
    -- do something
end

-- Script 2
local Part2 = workspace.Part2
Part2.ChildAdded:ConnectParallel(function(Child)
    -- do another thing
end

Even though these are Parallel Signals, these will not run at the same time. This is because it is a signal for a different Instance. If these were both for workspace.Part1 or vice versa however, this would be acceptable.

Since they’re not, it only makes sense to just use Signal::Connect

34 Likes

Edit: Nevermind, I figured it out. Thank you for your clarifications though!

Does anyone know a scenario where this method (ConnectParallel) would come into play?

If you have something running on the same context thread and multiple things are listening to the same event, ConnectParallel may be a good option. Usually in an environment with good practice, there really should never be multiple listeners for one event: but if there was this will make it more effective.

It’s been a while since I’ve actually looked into this, but I don’t know if ConnectParallel relies on the new Deferred event mode. That would be something important to note.

Assuming you’re familiar with task.desynchronize:
image