@@ -399,57 +399,52 @@ private bool TryRemoveInternal(TKey key, [MaybeNullWhen(false)] out TValue value
399399 object [ ] locks = tables . _locks ;
400400 ref Node ? bucket = ref GetBucketAndLock ( tables , hashcode , out uint lockNo ) ;
401401
402- // Do a hot read on number of items stored in the bucket. If it's empty, we can avoid
403- // taking the lock and fail fast.
404- if ( tables . _countPerLock [ lockNo ] != 0 )
402+ lock ( locks [ lockNo ] )
405403 {
406- lock ( locks [ lockNo ] )
404+ // If the table just got resized, we may not be holding the right lock, and must retry.
405+ // This should be a rare occurrence.
406+ if ( tables != _tables )
407407 {
408- // If the table just got resized, we may not be holding the right lock, and must retry.
409- // This should be a rare occurrence.
410- if ( tables != _tables )
408+ tables = _tables ;
409+ if ( ! ReferenceEquals ( comparer , tables . _comparer ) )
411410 {
412- tables = _tables ;
413- if ( ! ReferenceEquals ( comparer , tables . _comparer ) )
414- {
415- comparer = tables . _comparer ;
416- hashcode = GetHashCode ( comparer , key ) ;
417- }
418- continue ;
411+ comparer = tables . _comparer ;
412+ hashcode = GetHashCode ( comparer , key ) ;
419413 }
414+ continue ;
415+ }
420416
421- Node ? prev = null ;
422- for ( Node ? curr = bucket ; curr is not null ; curr = curr . _next )
423- {
424- Debug . Assert ( ( prev is null && curr == bucket ) || prev ! . _next == curr ) ;
417+ Node ? prev = null ;
418+ for ( Node ? curr = bucket ; curr is not null ; curr = curr . _next )
419+ {
420+ Debug . Assert ( ( prev is null && curr == bucket ) || prev ! . _next == curr ) ;
425421
426- if ( hashcode == curr . _hashcode && NodeEqualsKey ( comparer , curr , key ) )
422+ if ( hashcode == curr . _hashcode && NodeEqualsKey ( comparer , curr , key ) )
423+ {
424+ if ( matchValue )
427425 {
428- if ( matchValue )
426+ bool valuesMatch = EqualityComparer < TValue > . Default . Equals ( oldValue , curr . _value ) ;
427+ if ( ! valuesMatch )
429428 {
430- bool valuesMatch = EqualityComparer < TValue > . Default . Equals ( oldValue , curr . _value ) ;
431- if ( ! valuesMatch )
432- {
433- value = default ;
434- return false ;
435- }
436- }
437-
438- if ( prev is null )
439- {
440- Volatile . Write ( ref bucket , curr . _next ) ;
441- }
442- else
443- {
444- prev . _next = curr . _next ;
429+ value = default ;
430+ return false ;
445431 }
432+ }
446433
447- value = curr . _value ;
448- tables . _countPerLock [ lockNo ] -- ;
449- return true ;
434+ if ( prev is null )
435+ {
436+ Volatile . Write ( ref bucket , curr . _next ) ;
437+ }
438+ else
439+ {
440+ prev . _next = curr . _next ;
450441 }
451- prev = curr ;
442+
443+ value = curr . _value ;
444+ tables . _countPerLock [ lockNo ] -- ;
445+ return true ;
452446 }
447+ prev = curr ;
453448 }
454449 }
455450
0 commit comments