Skip to content

Commit

Permalink
Use tighter locking for Node list enumerator (#7070)
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams committed May 24, 2024
1 parent c903032 commit b6b681f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ public struct BondedItemsEnumerator : IEnumerator<NodeBucketItem>, IEnumerable<N
public BondedItemsEnumerator(NodeBucket nodeBucket)
{
_nodeBucket = nodeBucket;
lock (_nodeBucket._nodeBucketLock)
{
_currentNode = nodeBucket._items.Last;
}
_referenceTime = DateTime.UtcNow;
Monitor.Enter(_nodeBucket._nodeBucketLock);
_currentNode = nodeBucket._items.Last;
Current = null!;
}

Expand All @@ -63,13 +65,16 @@ public BondedItemsEnumerator(NodeBucket nodeBucket)

public bool MoveNext()
{
while (_currentNode is not null)
lock (_nodeBucket._nodeBucketLock)
{
Current = _currentNode.Value;
_currentNode = _currentNode.Previous;
if (Current.IsBonded(_referenceTime))
while (_currentNode is not null)
{
return true;
Current = _currentNode.Value;
_currentNode = _currentNode.Previous;
if (Current.IsBonded(_referenceTime))
{
return true;
}
}
}

Expand All @@ -81,11 +86,6 @@ public bool MoveNext()

public void Dispose()
{
if (_nodeBucket is not null)
{
Monitor.Exit(_nodeBucket._nodeBucketLock);
}
_nodeBucket = null!;
}
public BondedItemsEnumerator GetEnumerator() => this;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,40 +105,33 @@ public ClosestNodesEnumerator(NodeBucket[] buckets, int bucketSize)

public bool MoveNext()
{
try
while (_count < _bucketSize)
{
while (_count < _bucketSize)
if (!_enumeratorSet || !_itemEnumerator.MoveNext())
{
if (!_enumeratorSet || !_itemEnumerator.MoveNext())
_itemEnumerator.Dispose();
_bucketIndex++;
if (_bucketIndex >= _buckets.Length)
{
_itemEnumerator.Dispose();
_bucketIndex++;
if (_bucketIndex >= _buckets.Length)
{
return false;
}

_itemEnumerator = _buckets[_bucketIndex].BondedItems.GetEnumerator();
_enumeratorSet = true;
continue;
return false;
}

Current = _itemEnumerator.Current.Node!;
_count++;
return true;
_itemEnumerator = _buckets[_bucketIndex].BondedItems.GetEnumerator();
_enumeratorSet = true;
continue;
}
}
finally
{
_itemEnumerator.Dispose();

Current = _itemEnumerator.Current.Node!;
_count++;
return true;
}

return false;
}

void IEnumerator.Reset() => throw new NotSupportedException();

public void Dispose() { }
public void Dispose() => _itemEnumerator.Dispose();

public ClosestNodesEnumerator GetEnumerator() => this;

Expand Down

0 comments on commit b6b681f

Please sign in to comment.