How can I optimize a client-sided RunService loop that loops through and runs calculations on up to hundreds of parts every frame? I thought of using parallel lua, but it means that I would have to change my system drastically so that each part is under an actor (not sure if that’s how it works)? I’m a little confused on how that works.
In the loop, it’s mostly doing magnitude checks and vector3 stuff for those wondering.
Basically, what the system does is it iterates over a folder of parts every frame, checks if that part’s velocity magnitude is greater than some value, then changes a BodyMover’s velocity so that those parts slow down.
inlining math is what you can do, but its only like micro optimization, you won’t truly optimize this by optimizing math, you need to optimize the iterations, if you said hundreds of parts, then parallel lua is the only path
Operations that change the DataModel cannot be performed in a parallel execution phase. https://create.roblox.com/docs/scripting/multithreading#thread-safety
Additionally, with the added overheads of Parallel Luau, you likely would not see a measurable improvement on a few hundred calculations, and you may end up with worse performance than before.
The biggest optimization that could be made here is caching the parts that are needed, which minimizes DataModel indexing.
You might be asking, “What is the idea of doing this?”
The idea is :
You have pre-made, ready to clone Script/LocalScript, they’re just listening for a message from Connection BindToMessage and BindToMessageParallel to do something
actors Tasks are sorted, meaning, when you Dispatch(message, ...), basically it just finds the actor with the smallest tasks, then sends a message to it
When actors are doing something, they +1 Attribute of tasks, after finishing, will -1