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”
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
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.
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)