Skip to content

Commit

Permalink
Avoid an extra copy of T inside System.Diagnostics.Enumerator<T>. (#6…
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeBlanch authored Mar 23, 2022
1 parent 5ca9223 commit c5d40c9
Showing 1 changed file with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,28 +156,30 @@ public void AddFront(T value)
// Note: Some consumers use this Enumerator dynamically to avoid allocations.
internal struct Enumerator<T> : IEnumerator<T>
{
private static readonly DiagNode<T> s_Empty = new DiagNode<T>(default!);

private DiagNode<T>? _nextNode;
[AllowNull, MaybeNull] private T _currentItem;
private DiagNode<T> _currentNode;

public Enumerator(DiagNode<T>? head)
{
_nextNode = head;
_currentItem = default;
_currentNode = s_Empty;
}

public T Current => _currentItem!;
public T Current => _currentNode.Value;

object? IEnumerator.Current => Current;

public bool MoveNext()
{
if (_nextNode == null)
{
_currentItem = default;
_currentNode = s_Empty;
return false;
}

_currentItem = _nextNode.Value;
_currentNode = _nextNode;
_nextNode = _nextNode.Next;
return true;
}
Expand Down

0 comments on commit c5d40c9

Please sign in to comment.