@@ -443,7 +443,7 @@ private bool TryInsert(TKey key, TValue value, InsertionBehavior behavior)
443443 // If we hit the collision threshold we'll need to switch to the comparer which is using randomized string hashing
444444 // i.e. EqualityComparer<string>.Default.
445445
446- if ( collisionCount > HashHelpers . HashCollisionThreshold && _comparer is NonRandomizedStringEqualityComparer )
446+ if ( default ( TKey ) == null && collisionCount > HashHelpers . HashCollisionThreshold && _comparer is NonRandomizedStringEqualityComparer )
447447 {
448448 _comparer = ( IEqualityComparer < TKey > ) EqualityComparer < string > . Default ;
449449 Rehash ( ) ;
@@ -500,29 +500,34 @@ public virtual void OnDeserialization(object sender)
500500
501501 private void Rehash ( )
502502 {
503- int [ ] buckets = _buckets ;
504- for ( int i = 0 ; i < buckets . Length ; i ++ )
503+ // Structs are never rehashed
504+ Debug . Assert ( default ( TKey ) == null ) ;
505+ if ( default ( TKey ) == null )
505506 {
506- buckets [ i ] = - 1 ;
507- }
507+ int [ ] buckets = _buckets ;
508+ for ( int i = 0 ; i < buckets . Length ; i ++ )
509+ {
510+ buckets [ i ] = - 1 ;
511+ }
508512
509- int count = _count ;
510- int length = buckets . Length ;
511- Entry [ ] entries = _entries ;
513+ int count = _count ;
514+ int length = buckets . Length ;
515+ Entry [ ] entries = _entries ;
512516
513- IEqualityComparer < TKey > comparer = _comparer ;
517+ IEqualityComparer < TKey > comparer = _comparer ;
514518
515- for ( int i = 0 ; i < count ; i ++ )
516- {
517- ref Entry entry = ref entries [ i ] ;
518- int hashCode = entry . hashCode ;
519- if ( hashCode >= 0 )
519+ for ( int i = 0 ; i < count ; i ++ )
520520 {
521- hashCode = ( comparer . GetHashCode ( entry . key ) & 0x7FFFFFFF ) ;
522- int bucket = hashCode % length ;
523- entry . hashCode = hashCode ;
524- entry . next = buckets [ bucket ] ;
525- buckets [ bucket ] = i ;
521+ ref Entry entry = ref entries [ i ] ;
522+ int hashCode = entry . hashCode ;
523+ if ( hashCode >= 0 )
524+ {
525+ hashCode = ( comparer . GetHashCode ( entry . key ) & 0x7FFFFFFF ) ;
526+ int bucket = hashCode % length ;
527+ entry . hashCode = hashCode ;
528+ entry . next = buckets [ bucket ] ;
529+ buckets [ bucket ] = i ;
530+ }
526531 }
527532 }
528533 }
0 commit comments