0

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;
}
user3488765
  • 415
  • 3
  • 12
  • Did you implement a custom GetHashCode and Equals method on the structs? – Scott Chamberlain Nov 20 '16 at 05:40
  • Initially I did, However I posted this question after removing them and still having considerably worse performance – user3488765 Nov 20 '16 at 05:50
  • 2
    Without [MCVE] it is not possible to find reason. (You've obviously read http://stackoverflow.com/questions/521298/when-to-use-struct, so I assume your structs are following the guidelines). – Alexei Levenkov Nov 20 '16 at 07:15
  • The built in GetHashCode and Equals for structs uses reflection and [is considerably slower](http://stackoverflow.com/questions/39391107/why-are-hashsets-of-structs-with-nullable-values-incredibly-slow/39391290#39391290) than a custom implementation. – Scott Chamberlain Nov 20 '16 at 07:58
  • @AlexeiLevenkov I am. Except I am not sure on the boxing.. If the struct uses 3-7 interfaces. Is that also boxing? Or is it just object conversion? – user3488765 Nov 20 '16 at 08:09
  • @ScottChamberlain I added my method of getHashcode. Can you have a look at it? I don't think my buckets are too full and Equals gets called too often. My Equals method is just a direct check on the exact position – user3488765 Nov 20 '16 at 09:14

0 Answers0