float xPos, yPos, zPos;
This may be a dumb question, but is there anything bad about using:
public Vector3 getPos { get { return Vector3.right * xPos + Vector3.up * yPos + Vector3.forward * zPos; } }
Instead of using:
public Vector3 getPos { get { return new Vector3(xPos, yPos, zPos); } }
The reason I ask this is because I recently read something that said that using ‘new Vector3’ creates some garbage. I need to call this thousands of times or more and I don’t want this to impact performance. The second one is obviously easier to read, but I need performance since this is going to be running on the base class of the entity system which may have thousands of active instances which call it multiple times per frame.
I recently read something that said that using ‘new Vector3’ creates some garbage.
Please show us where you read that, because it’s completely wrong. Vector3 is a struct and therefore a value type. new Vector3 does not allocate any memory.
This code:
public Vector3 getPos { get { return Vector3.right * xPos + Vector3.up * yPos + Vector3.forward * zPos; } }
Is actually much slower as you would invoke the Vector3*float operator 3 times and the Vector3+Vector3 operator 2 times. Those would do 3 float multiplications (in case of the first operator) and 3 float additions (in case of the second).
Unity for the longest time had those static properties actually be a shorthand like this:
public static Vector3 right {get{return new Vector3(1,0,0);}}
Apart from that, when you scroll down just a little bit you will find the +operator and the *operator. As you may see, it also uses the Vector3 constructor to actually form the result of the operation. So you’re essentially replaced a single call to a method with 3 float arguments with 3 method calls which take a Vector3 and a float argument (essentially passing 4 floats) which in turn call the constructor 3 times and does unnecessary multiplications, as well as calling another method 2 times that takes 2 vector3 values (so passing 6 float values) which does the summation and then also calls the constructor once more.
So your “optimised” code essentially replaced the one constructor call with 5 constructor calls and a lot of additional overhead.
So if you have the source of that claim anywhere, please share it, this needs to be fixed / called out
I’m slightly reluctant to enter this discussion because - it depends. My mantra is always to try it and profile the results. public Vector3 = new(x, y, z) gives the compiler the best chance of optimising because you’re not obscuring what you want. However, I don’t think anybody can say whether the code, the memory overhead or the cost of garbage collection is going to be worse than the others.
Have you considered lightweight object pooling? You could potentially avoid GC altogether. Also, recall that if you have appropriate places in your code, you can always callSystem.GC.Collect(); yourself.
Anyway, prove there’s a problem before trying to solve it. In my experience second guessing performance problems leads to dragons and not unicorns. Dangerous…