Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
PR Feedback, copy _slots to local ref
Browse files Browse the repository at this point in the history
  • Loading branch information
safern committed Mar 20, 2018
1 parent a7d2cb2 commit 4325a73
Showing 1 changed file with 40 additions and 35 deletions.
75 changes: 40 additions & 35 deletions src/System.Collections/src/System/Collections/Generic/HashSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,17 +259,18 @@ public bool Contains(T item)
{
int collisionCount = 0;
int hashCode = InternalGetHashCode(item);
Slot[] slots = _slots;
// see note at "HashSet" level describing why "- 1" appears in for loop
for (int i = _buckets[hashCode % _buckets.Length] - 1; i >= 0; i = _slots[i].next)
for (int i = _buckets[hashCode % _buckets.Length] - 1; i >= 0; i = slots[i].next)
{
if (_slots[i].hashCode == hashCode && _comparer.Equals(_slots[i].value, item))
if (slots[i].hashCode == hashCode && _comparer.Equals(slots[i].value, item))
{
return true;
}

if (collisionCount >= _slots.Length)
if (collisionCount >= slots.Length)
{
// The chain of entries forms a loop; which means a concurrent update has happened.
// The chain of entries forms a loop, which means a concurrent update has happened.
throw new InvalidOperationException(SR.InvalidOperation_ConcurrentOperationsNotSupported);
}
collisionCount++;
Expand Down Expand Up @@ -302,26 +303,27 @@ public bool Remove(T item)
int bucket = hashCode % _buckets.Length;
int last = -1;
int collisionCount = 0;
for (int i = _buckets[bucket] - 1; i >= 0; last = i, i = _slots[i].next)
Slot[] slots = _slots;
for (int i = _buckets[bucket] - 1; i >= 0; last = i, i = slots[i].next)
{
if (_slots[i].hashCode == hashCode && _comparer.Equals(_slots[i].value, item))
if (slots[i].hashCode == hashCode && _comparer.Equals(slots[i].value, item))
{
if (last < 0)
{
// first iteration; update buckets
_buckets[bucket] = _slots[i].next + 1;
_buckets[bucket] = slots[i].next + 1;
}
else
{
// subsequent iterations; update 'next' pointers
_slots[last].next = _slots[i].next;
slots[last].next = slots[i].next;
}
_slots[i].hashCode = -1;
slots[i].hashCode = -1;
if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
{
_slots[i].value = default(T);
slots[i].value = default(T);
}
_slots[i].next = _freeList;
slots[i].next = _freeList;

_count--;
_version++;
Expand All @@ -337,9 +339,9 @@ public bool Remove(T item)
return true;
}

if (collisionCount >= _slots.Length)
if (collisionCount >= slots.Length)
{
// The chain of entries forms a loop; which means a concurrent update has happened.
// The chain of entries forms a loop, which means a concurrent update has happened.
throw new InvalidOperationException(SR.InvalidOperation_ConcurrentOperationsNotSupported);
}
collisionCount++;
Expand Down Expand Up @@ -1213,16 +1215,17 @@ private bool AddIfNotPresent(T value)
int hashCode = InternalGetHashCode(value);
int bucket = hashCode % _buckets.Length;
int collisionCount = 0;
for (int i = _buckets[bucket] - 1; i >= 0; i = _slots[i].next)
Slot[] slots = _slots;
for (int i = _buckets[bucket] - 1; i >= 0; i = slots[i].next)
{
if (_slots[i].hashCode == hashCode && _comparer.Equals(_slots[i].value, value))
if (slots[i].hashCode == hashCode && _comparer.Equals(slots[i].value, value))
{
return false;
}

if (collisionCount >= _slots.Length)
if (collisionCount >= slots.Length)
{
// The chain of entries forms a loop; which means a concurrent update has happened.
// The chain of entries forms a loop, which means a concurrent update has happened.
throw new InvalidOperationException(SR.InvalidOperation_ConcurrentOperationsNotSupported);
}
collisionCount++;
Expand All @@ -1232,11 +1235,11 @@ private bool AddIfNotPresent(T value)
if (_freeList >= 0)
{
index = _freeList;
_freeList = _slots[index].next;
_freeList = slots[index].next;
}
else
{
if (_lastIndex == _slots.Length)
if (_lastIndex == slots.Length)
{
IncreaseCapacity();
// this will change during resize
Expand All @@ -1245,9 +1248,9 @@ private bool AddIfNotPresent(T value)
index = _lastIndex;
_lastIndex++;
}
_slots[index].hashCode = hashCode;
_slots[index].value = value;
_slots[index].next = _buckets[bucket] - 1;
slots[index].hashCode = hashCode;
slots[index].value = value;
slots[index].next = _buckets[bucket] - 1;
_buckets[bucket] = index + 1;
_count++;
_version++;
Expand Down Expand Up @@ -1401,16 +1404,17 @@ private int InternalIndexOf(T item)

int collisionCount = 0;
int hashCode = InternalGetHashCode(item);
for (int i = _buckets[hashCode % _buckets.Length] - 1; i >= 0; i = _slots[i].next)
Slot[] slots = _slots;
for (int i = _buckets[hashCode % _buckets.Length] - 1; i >= 0; i = slots[i].next)
{
if ((_slots[i].hashCode) == hashCode && _comparer.Equals(_slots[i].value, item))
if ((slots[i].hashCode) == hashCode && _comparer.Equals(slots[i].value, item))
{
return i;
}

if (collisionCount >= _slots.Length)
if (collisionCount >= slots.Length)
{
// The chain of entries forms a loop; which means a concurrent update has happened.
// The chain of entries forms a loop, which means a concurrent update has happened.
throw new InvalidOperationException(SR.InvalidOperation_ConcurrentOperationsNotSupported);
}
collisionCount++;
Expand Down Expand Up @@ -1532,17 +1536,18 @@ private bool AddOrGetLocation(T value, out int location)
int hashCode = InternalGetHashCode(value);
int bucket = hashCode % _buckets.Length;
int collisionCount = 0;
for (int i = _buckets[bucket] - 1; i >= 0; i = _slots[i].next)
Slot[] slots = _slots;
for (int i = _buckets[bucket] - 1; i >= 0; i = slots[i].next)
{
if (_slots[i].hashCode == hashCode && _comparer.Equals(_slots[i].value, value))
if (slots[i].hashCode == hashCode && _comparer.Equals(slots[i].value, value))
{
location = i;
return false; //already present
}

if (collisionCount >= _slots.Length)
if (collisionCount >= slots.Length)
{
// The chain of entries forms a loop; which means a concurrent update has happened.
// The chain of entries forms a loop, which means a concurrent update has happened.
throw new InvalidOperationException(SR.InvalidOperation_ConcurrentOperationsNotSupported);
}
collisionCount++;
Expand All @@ -1551,11 +1556,11 @@ private bool AddOrGetLocation(T value, out int location)
if (_freeList >= 0)
{
index = _freeList;
_freeList = _slots[index].next;
_freeList = slots[index].next;
}
else
{
if (_lastIndex == _slots.Length)
if (_lastIndex == slots.Length)
{
IncreaseCapacity();
// this will change during resize
Expand All @@ -1564,9 +1569,9 @@ private bool AddOrGetLocation(T value, out int location)
index = _lastIndex;
_lastIndex++;
}
_slots[index].hashCode = hashCode;
_slots[index].value = value;
_slots[index].next = _buckets[bucket] - 1;
slots[index].hashCode = hashCode;
slots[index].value = value;
slots[index].next = _buckets[bucket] - 1;
_buckets[bucket] = index + 1;
_count++;
_version++;
Expand Down

0 comments on commit 4325a73

Please sign in to comment.