Skip to content

Commit

Permalink
Issue-20619 early return existing value. (dotnet#46481)
Browse files Browse the repository at this point in the history
* Issue-20619 early return existing value.

* issue-20619 Fix comment: Reuse check inside of SetItem; Also use customer-supplied value comparer for TryUpdate().

* issue-20619 Fix tab-space issue.

* issue-20619 Fix comment: Revert: use customer-supplied value comparer for TryUpdate().
  • Loading branch information
lateapexearlyspeed authored Feb 1, 2021
1 parent 3d8d297 commit 0b23cdb
Showing 1 changed file with 8 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,10 @@ public static TValue AddOrUpdate<TKey, TValue>(ref ImmutableDictionary<TKey, TVa
}

var updatedCollection = priorCollection.SetItem(key, newValue);
if (object.ReferenceEquals(priorCollection, updatedCollection))
{
return oldValue;
}
var interlockedResult = Interlocked.CompareExchange(ref location, updatedCollection, priorCollection);
successful = object.ReferenceEquals(priorCollection, interlockedResult);
priorCollection = interlockedResult; // we already have a volatile read that we can reuse for the next loop
Expand Down Expand Up @@ -397,6 +401,10 @@ public static TValue AddOrUpdate<TKey, TValue>(ref ImmutableDictionary<TKey, TVa
}

var updatedCollection = priorCollection.SetItem(key, newValue);
if (object.ReferenceEquals(priorCollection, updatedCollection))
{
return oldValue;
}
var interlockedResult = Interlocked.CompareExchange(ref location, updatedCollection, priorCollection);
successful = object.ReferenceEquals(priorCollection, interlockedResult);
priorCollection = interlockedResult; // we already have a volatile read that we can reuse for the next loop
Expand Down

0 comments on commit 0b23cdb

Please sign in to comment.