How can I optimize calculations on hundreds of parts at a time?

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.

Can you provide the code for clarity?

What does this system do? What’s the reason for iterating over hundreds of parts? A description of what the system does would help

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.

See my latest reply for more info…

Are you trying to make slow motion parts?

It’s like a custom air resistance system for a soccer game that slows the balls down

What if you instead of using BodyMover, just will change Part.AssemblyLinearVelocity to slowdown parts?
Limit max AssemblyLinearVelocity possible

local LIMIT_SPEED = 50
local Velocity = Part.AssemblyLinearVelocity

Part.AssemblyLinearVelocity = vector.create(
math.min(Velocity.X, LIMIT_SPEED),
math.min(Velocity.Y, LIMIT_SPEED),
math.min(Velocity.Z, LIMIT_SPEED)
)

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.

Could you elaborate more on what you mean by this?

Well is it slow? If the number of parts is in the hundred then I would safely assume that would have zero impact on performance.

You can use Parallel luau for that. I’ve been working on this Module. which I have used in my game
you can try it too!, it’s simple
GitHub - weenachuangkud/VMsDispatcher: Modded Dispatcher borrowed from SecureCast

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