So I have a math class which performs calculations on the getters of a number of properties. Now I converted these properties so that after their first evaluation return only that result from then on.
Now concerning performance I was wondering that given that there will most likely be hundreds if not thousands of instances of these stored in collections, which approach is best on average?
Things that concern me is that the latter approach:
increases the GC load and it also affects locality of reference as nullables will allocate memory away from parent object.
public class SomeMathType
{
// these are structs
public Vector A { get; }
public Vector B { get; }
...
public double Length => A + B;
...
}
vs.
public class SomeMathType
{
...
private double? _length;
private Vector? _vector;
// these are structs
public Vector A { get; }
public Vector B { get; }
public Vector Vector
{
get
{
_vector = _vector ?? B - A;
return _vector.Value;
}
}
public double Length
{
get
{
_length = _length ?? Vector.Length;
return _length.Value;
}
}
...
}