Faster distance checking

As a Roblox developer, it is currently too hard to check object distance performantly.

If Roblox is able to address this issue, it would improve my development experience because it will allow for faster code where millions of distance checks are required, for example, in space simulations which are checking the distance between objects all of the time.

In this post I wrote about how luau has improved the performance of .Magnitude in comparison to the previously faster rootless distance checks. In the post I described how the reason rootless distance checks are likely so much slower now is due to both Indexing and the cost of datatype creation.

I propose that, based on my use case, CFrame, Vector3, and Vector2 all get an :InDistanceTo(otherVector, distance) function for checking linear distances (This is similar to . This eliminates the need for creating new Vectors/CFrames by subtraction (which I estimate based on my benchmarks in the linked post takes about 50% of the execution time), and allows for a rootless magnitude check internally

Previous argument against a feature like this has been that square rooting is not really that slow, and while I agree in that area, there are additional costs due to indexing and datatype creation, and on top of that, the fact that rootless checks are still a good bit faster faster I think really make this sort of thing worth it.

Based on around 20 benchmarks each, I estimate that one million distance checks would take roughly 30-60 ms on my better hardware, and closer to 40-65 ms on a real Roblox server with this change, vs about 160-180 on my hardware and 210-230 on Roblox servers. I got this estimation by taking the maximum and minimum times of .Magnitude, the maximum and minimum times of existingTable.ExistingValue and the maximum and minimum times of vector1 - vector2, and subtracting the latter two values.

11 Likes