Why are HashSets of structs with nullable values incredibly slow?

前端 未结 2 831
自闭症患者
自闭症患者 2020-12-24 10:41

I investigated performance degradation and tracked it down to slow HashSets.
I have structs with nullable values that are used as a primary key. For example:

<         


        
2条回答
  •  星月不相逢
    2020-12-24 10:50

    This is due to struct GetHashCode() behavior. If it finds reference types - it tries to get hash from first non-reference type field. In your case it WAS found, and Nullable<> is also struct, so it just poped it's private boolean value (4 bytes)

提交回复
热议问题