- Home /
Code Performance: When to use "new" on Vector3
I am calculating a vector normal and storing the results into another vector and it seems to work regardless of whether or not my destination vector is an instance of vector3 or just a variable of type vector3.
Can someone explain what's going on when I need to use "new Vector3()" and when not to?
var vectorNormalized : Vector3;
//normalization below seems to work with or without this line.
vectorNormalized = new Vector3();
vectorNormalized = Vector3.Normalize(myOriginalVector);
In UnityScript/Javascript, what's the most optimal way to do what I'm trying to do?
Thanks, Manny
Answer by YoungDeveloper · Aug 21, 2015 at 03:34 PM
var vectorNormalized : Vector3; //you create new vector 0,0,0
vectorNormalized = new Vector3(); //you create new vector 0,0,0 again
vectorNormalized = Vector3.Normalize(myOriginalVector); //new vector is created as normalized from same vector
Dont create useless objects, instead you could simply.
function Start(){
var vectorNormalized:Vector3 = new Vector3();
vectorNormalized.Normalize();
}
Thanks for your quick reply. I have a need to keep both by original vector as well as obtain a normalized version. Would you recommend:
var vectorNormalized : Vector3;
function someFunctionThatGetsCalled$$anonymous$$ultipleTimes() {
vectorNormalized = myOriginalVector;
vectorNormalized.Normalize();
....
....
}
Thanks.
If you need to keep both, then creating a temp cache is the only way.
private var myVector : Vector3 = new Vector3(5f,5f,5f);
function Funk(){
var myVectorNormalized:Vector3 = myVector.normalized;
}
Answer by Owen-Reynolds · Aug 21, 2015 at 04:52 PM
It doesn't matter whether you do or don't use new
on Vectors.
Vector3s are structs. This official microsoft C# page explains that using new
for structs isn't a real new
. It's just a way to force it to be initialized:
https://msdn.microsoft.com/en-us/library/0taef578.aspx
The same page also explains how structs aren't really objects, the way classes are. So Vector3 doesn't make an object, since it can't.
If reading that page hurts your head, that's the main idea. The language and the compiler are often doing a lot that you can't see. You generally have to trust that the usual way to do things (like, official Unity examples using new
with Vector3s) gets handled well-enough.
I agree, the "new" call should not do anything on a struct memory wise, but the $$anonymous$$ono compiler used in Unity does not make the optimization of ignoring the constructor call. You can find more information, with IL instructions comparison, at this link: https://forum.unity.com/threads/vector3-and-other-structs-optimization-of-operators.477338/
Answer by Aka_ToolBuddy · Jun 09, 2020 at 08:01 PM
Yes, you should avoid calling new if you want to get performance. Of course I am speaking about situations where you have a LOT of vector operations for this to make a difference. A scenario where this can happen is mesh generation/transformation.
If you don't call the constructor, then you have to assign all the vector's component before being able to use it, otherwise the compiler will not be happy.
You don't have to bother doing this if you use this asset I made that does that automatically for you
The asset: Frame Rate Booster
The forum explaining how things work: https://forum.unity.com/threads/vector3-and-other-structs-optimization-of-operators.477338/
Your answer
Follow this Question
Related Questions
Setting Scroll View Width GUILayout 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers
2d camera help! 0 Answers
On Trigger Stay 1 Answer