I have a few types that I store millions of instances of in a file on the HD. At the start of the program subsets are loaded into a variety of hashmaps, depending on their attributes. The class that handles the hashmaps also handles the updating of those instances. During runtime the instances are mostly evaluated with linq to pass specific subsets along. That is about 99% of processing in the program.
I only want to allow the removal and addition of instances. Changing of already existing ones should not be allowed.
To better illustrate this I changed the type of those instances from class to struct.
But when I do that, the runtime performance tanks(99% only evaluation).
So what am I understanding wrong about structs? Are they not ment to be stored in huge amounts over a longer time and evaluated? Is GetHashCode and Equals way more expensive on them than on classes?
Here my GetHashCode Method:
private int _worldSpaceID;
public override int GetHashCode()
{
if (_worldSpaceID == 0)
{ //worldspace bit layout: yyyyyzzzzzzzzzxxxxxxxxx zzzyyyxxxx ((5ypos 9zpos 9xpos 3zrot3yrot3xrot))
//set location part of worldspace id
Int3 posFl = new Int3(Position);
int mask = 0x01FF;
_worldSpaceID += (int)(((posFl.x & mask) << 9) + ((posFl.z & mask) << 18) + ((posFl.y & 0x001F) << 27));
//set rotation part of worldspace id
Int3 rotFl = new Int3(Rotation);
mask = 0x0007;
_worldSpaceID += (int)((rotFl.x & mask) + ((rotFl.y & mask) << 3) + ((rotFl.z & mask) << 6));
//Avoid recalculating in case the calculation sets _worldSpaceID to 0;
_worldSpaceID = _worldSpaceID == 0 ? -1 : _worldSpaceID;
}
return _worldSpaceID;
}