未验证 提交 c5d40c9e 编写于 作者: M Mikel Blanchard 提交者: GitHub

Avoid an extra copy of T inside System.Diagnostics.Enumerator<T>. (#67012)

上级 5ca92236
......@@ -156,16 +156,18 @@ 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;
......@@ -173,11 +175,11 @@ public bool MoveNext()
{
if (_nextNode == null)
{
_currentItem = default;
_currentNode = s_Empty;
return false;
}
_currentItem = _nextNode.Value;
_currentNode = _nextNode;
_nextNode = _nextNode.Next;
return true;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册