@@ -39,10 +39,10 @@ public void AddKnownRef(object from, object to)
3939
4040 private sealed class MiniDictionary
4141 {
42- private readonly struct Entry ( int hashCode , int next , object key , object value )
42+ private struct Entry ( int hashCode , int next , object key , object value )
4343 {
4444 public readonly int HashCode = hashCode ;
45- public readonly int Next = next ;
45+ public int Next = next ;
4646 public readonly object Key = key ;
4747 public readonly object Value = value ;
4848 }
@@ -159,25 +159,24 @@ private void Initialize(int size)
159159 public void Insert ( object key , object value )
160160 {
161161 if ( buckets is null )
162- Initialize ( DefaultCapacity ) ;
163-
162+ Initialize ( GetPrime ( DefaultCapacity ) ) ;
163+
164164 int hashCode = RuntimeHelpers . GetHashCode ( key ) & 0x7FFFFFFF ;
165- int targetBucket = hashCode % buckets ! . Length ;
165+ int [ ] localBuckets = buckets ! ;
166+ int targetBucket = hashCode % localBuckets . Length ;
167+ Entry [ ] localEntries = entries ;
166168
167- if ( count == entries . Length )
169+ if ( count == localEntries . Length )
168170 {
169171 Resize ( ) ;
170- targetBucket = hashCode % buckets . Length ;
172+ localBuckets = buckets ! ;
173+ localEntries = entries ;
174+ targetBucket = hashCode % localBuckets . Length ;
171175 }
172176
173177 int index = count ++ ;
174- entries [ index ] = new Entry (
175- hashCode ,
176- buckets [ targetBucket ] ,
177- key ,
178- value
179- ) ;
180- buckets [ targetBucket ] = index ;
178+ localEntries [ index ] = new Entry ( hashCode , localBuckets [ targetBucket ] , key , value ) ;
179+ localBuckets [ targetBucket ] = index ;
181180 }
182181
183182 private void Resize ( ) => Resize ( ExpandPrime ( count ) ) ;
@@ -188,22 +187,16 @@ private void Resize(int newSize)
188187 Array . Fill ( newBuckets , - 1 ) ;
189188
190189 Entry [ ] newEntries = new Entry [ newSize ] ;
191- Array . Copy ( entries , 0 , newEntries , 0 , count ) ;
190+ Array . Copy ( entries , newEntries , count ) ;
192191
193192 for ( int i = 0 ; i < count ; i ++ )
194193 {
195- if ( newEntries [ i ] . HashCode < 0 )
196- {
194+ ref Entry entry = ref newEntries [ i ] ;
195+ if ( entry . HashCode < 0 )
197196 continue ;
198- }
199-
200- int bucket = newEntries [ i ] . HashCode % newSize ;
201- newEntries [ i ] = new Entry (
202- newEntries [ i ] . HashCode ,
203- newBuckets [ bucket ] ,
204- newEntries [ i ] . Key ,
205- newEntries [ i ] . Value
206- ) ;
197+
198+ int bucket = entry . HashCode % newSize ;
199+ entry . Next = newBuckets [ bucket ] ;
207200 newBuckets [ bucket ] = i ;
208201 }
209202
0 commit comments