I’ve seen a script that requires creating lots of temporary vectors each frame, and the author, to avoid using “new” all the time, instead had a large array of Vector3s and Vector2s. Every frame, he reset the pointer to the array back to 0 and incremented it each time he needed a new vector, before changing the vector’s components.
My question: does this really provide any significant benefit?
It’s not a good idea to do that, at least when it comes to structs like Vector3. Here’s the result of a test involving 1000 instantiated objects. It uses a class that’s essentially a Vector3, except it’s a class and not a struct:
class Test {
var x : float;
var y : float;
var z : float;
function Test (x : float, y : float, z : float) {
this.x = x;
this.y = y;
this.z = z;
}
}
The script on each of the 1000 objects allocates a new Test class every frame in Update:
function Update () {
var foo = new Test(0.0, 0.0, 0.0);
}

As you can see, there are GC spikes, and it allocates 19.5 KB per frame. So, here’s the result of a test that allocates a new Vector3 in Update:
function Update () {
var foo = new Vector3(0.0, 0.0, 0.0);
}

As you can see, there are no GC collection spikes, and no bytes being allocated per frame. It’s perfectly OK to allocate new structs like Vector3 in Update. It is, in fact, preferable to the alternative, namely convoluted messy code that doesn’t actually accomplish anything. Simply keep variables local as usual, and don’t try any fancy schemes involving re-using array entries (unless you’re dealing with classes and not structs, I guess).