Using parallel lua to speed up computations

I’m trying to use parallel lua for this test case I have where I spawn 100 parts, then calculate how far every part is to each other, and whether it has direct line of sight of each other.

I thought this would be something that would be faster if I used parallel lua, so I sectioned out the objects among the actors (e.g. if there are 10 actors, then each actor is responsible for doing these computations for 10 of the parts), then when its finishing computing it fires a bindable event which tells the main script to merge all the data.

The issue is that im finding parallel lua to be at least 4x slower in most cases, this doesn’t seem right, any idea what im doing wrong?

I’ve checked on the microprofiler and it is definitely running on different threads, is there just too much overhead from connecting in parallel?

It might have to do with how you’re using a BindableEvent to merge the data instead of a SharedTable, and note that you can also safely use Raycast inside parallel threads (jic telling you if you were only using parallel to calculate the distances)

But I personally think you should try optimizing this with something that’s called a “swarm module”

A staff member has made a guide on this if you’d like to read that: Optimizing Unit Targeting in Roblox with a Swarm Module but I’ll try to explain it myself to my best.

Basically, you have to put all of your parts and parts’ positions into a cell inside of a grid using tables, when you have to do the computations, you can do both the distance check and raycast on ONLY the nearby cells as we know that the other cells are likely too far away, and because the cells are just stored in a table, we can just get the nearby cells with a simple for loop

This’ll likely optimize your system better because you don’t have to run unnecessary computations on EVERY single part that exists

1 Like

Sorry I should have specified, even if I don’t include the time it takes to merge the data back together, its still at least 4x slower than not using it at all.

I will check out swarm modules (:

That’s honestly weird, could you try measuring the time it takes to do each computation? Parallel Luau is very bad with long computations (or computations that just vary a lot in the time they take to finish, such as for example making a raycast of 3 studs and making a raycast of 1000 studs)

1 Like

Okay so,

for it to do the calculations for 100 npcs (meaning 10,000 comparisons)

With 20 actors

serial: 0.0499 seconds
parallel: 0.1901 seconds (almost 4x slower than serial)

parallel average for 1 actor: 0.0347 seconds
parallel max: 0.0857 seconds
parallel min: 0.00614

So there does seem to be significant difference between the actors, since the longest one took about 12x longers than the fastest.

Though its kinda odd still that its taking so long when the average is 0.0347 seconds

Okay so it turns out I made a mistake when partitioning my data to the various actors. I fixed that and now it is running faster than serial!

1 Like