Why does Farseer 2.x store temporaries as members and not on the stack? (.NET)
- by Andrew Russell
UPDATE: This question refers to Farseer 2.x. The newer 3.x doesn't seem to do this.
I'm using Farseer Physics Engine quite extensively at the moment, and I've noticed that it seems to store a lot of temporary value types as members of the class, and not on the stack as one might expect.
Here is an example from the Body class:
private Vector2 _worldPositionTemp = Vector2.Zero;
private Matrix _bodyMatrixTemp = Matrix.Identity;
private Matrix _rotationMatrixTemp = Matrix.Identity;
private Matrix _translationMatrixTemp = Matrix.Identity;
public void GetBodyMatrix(out Matrix bodyMatrix)
{
Matrix.CreateTranslation(position.X, position.Y, 0, out _translationMatrixTemp);
Matrix.CreateRotationZ(rotation, out _rotationMatrixTemp);
Matrix.Multiply(ref _rotationMatrixTemp, ref _translationMatrixTemp, out bodyMatrix);
}
public Vector2 GetWorldPosition(Vector2 localPosition)
{
GetBodyMatrix(out _bodyMatrixTemp);
Vector2.Transform(ref localPosition, ref _bodyMatrixTemp, out _worldPositionTemp);
return _worldPositionTemp;
}
It looks like its a by-hand performance optimisation. But I don't see how this could possibly help performance? (If anything I think it would hurt by making objects much larger).