Hey developers,
As we continue our work on Luau VM performance, one of the items we wanted to address was performance of Vector3 operations.
Vector3s used to be heap-allocated, meaning that memory allocation took place for every vector, and only de-allocated by garbage collection - which was having a negative impact on performance. This particularly impacts code that creates a lot of short-lived temporary vectors, which could trigger a lot of garbage collection steps. Another important area is binary operations between vectors that were implemented using metatable function and even a simple addition would have to go through a function call.
Internally, our VM has supported a built-in âvectorâ value type for some time now with built-in vector operations, but the Roblox Vector3 userdata class didnât use that machinery.
After additional development, we are now ready to switch Vector3 userdata to use our vector value type! You should expect performance improvements for property accesses and all existing operations. You can even use Vector3 to vectorize your scalar code for an improvement in performance.
The Native Vector3 Type can be enabled via the Roblox Studio Beta Features Menu:
But while we tried to make this change backwards-compatible and our Vector3 userdata implementation tried to emulate an immutable value type like the built-in vector, underlying type change does bring behavior changes that can be seen from outside:
- type(Vector3.new()) will now return âvectorâ instead of âuserdataâ. Note that the separate function, typeof(Vector3.new()) will continue to return Vector3 as it did before.
- Vector3 is now a value type, which changes the result of rawequal for Vector3 values that were created separately, but hold the same value. This also means that rawequal(v, v) will return false if one of the vector components is NaN.
- If youâve used Vector3 as a table key, instances holding the same value will now be compared equally
- If youâve used Vector3 as a weak table key, these table entries will no longer be collected. If you use such tables for caching, you should switch to an explicit caching scheme.
We are launching this as Beta so that you will be able to test your games and report problems that arise from the underlying type change. We would also like to hear how your Vector3-heavy code performance is improved!
If we donât discover any significant issues we plan to enable this change for game clients and servers by default in a few weeks. Please let us know if your game may be negatively impacted by this change.
Thanks,
The Roblox Team
edit: clarified the behavior of typeof