Can a custom Int3 struct be more efficient than Vector3? (floats)

I’m studying the source code of the most popular A* pathfinding asset on the Unity Asset Store. It uses a custom Int3 struct for coordinates. One reason for it may be that usually in A* we work with grids, where integer coordinates make sense, but can the other reason be that integers are more efficient than floats? or is the performance gain generally too small to notice?

Do not do this. Use the data type that’s appropriate for the context. Floats/doubles if you need fractions, whereas integers if you won’t. A* PP uses integers for the grid because that’s what makes senes there.

People only substitute integers for floats for performance in crazy situations, which you’ll likely never encounter. In the non-specific general case, you may even harm performance!

Unity has Vector3Int.

The other library using a different struct for the same thing could simply be that the library is not just for Unity, so it can’t use Unity types internally.

A lot of time and energy has gone into floating-point math arithmetic hardware to the point where it’s probably more advanced than integer stuff. So the performance thing is more-or-less a thing of the past. These days it’s usually better to use ints instead of floats because you need exact precision. NOT because you need performance.

It could be that they wanted something that was guaranteed to be deterministic across networks and on different systems. As for why they used their own instead of Unity’s - probably due to legacy. If it’s an asset that’s been around for a long time (I’m assuming you’re talking about A*PP) then it would pre-date the inclusion of the Vector3Int in Unity. It could also have something to do with the aforementioned determinism. If you really want to know the answer though, why don’t you contact the asset provider and ask them directly?

1 Like

I just had to check … Vector3Int first appeared in the 2017.2 manual.

1 Like