From c5d40c9e703fd257db1b26ef4fd1399bbae73ab0 Mon Sep 17 00:00:00 2001 From: Mikel Blanchard Date: Tue, 22 Mar 2022 22:05:46 -0700 Subject: [PATCH] Avoid an extra copy of T inside System.Diagnostics.Enumerator. (#67012) --- .../src/System/Diagnostics/DiagLinkedList.cs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/DiagLinkedList.cs b/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/DiagLinkedList.cs index 3c75f0e5e42..8ec5e93b624 100644 --- a/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/DiagLinkedList.cs +++ b/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/DiagLinkedList.cs @@ -156,16 +156,18 @@ public void AddFront(T value) // Note: Some consumers use this Enumerator dynamically to avoid allocations. internal struct Enumerator : IEnumerator { + private static readonly DiagNode s_Empty = new DiagNode(default!); + private DiagNode? _nextNode; - [AllowNull, MaybeNull] private T _currentItem; + private DiagNode _currentNode; public Enumerator(DiagNode? 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; } -- GitLab