Skip to content

Commit c5d40c9

Browse files
authored
Avoid an extra copy of T inside System.Diagnostics.Enumerator<T>. (#67012)
1 parent 5ca9223 commit c5d40c9

File tree

1 file changed

+7
-5
lines changed
  • src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics

1 file changed

+7
-5
lines changed

src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/DiagLinkedList.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,28 +156,30 @@ public void AddFront(T value)
156156
// Note: Some consumers use this Enumerator dynamically to avoid allocations.
157157
internal struct Enumerator<T> : IEnumerator<T>
158158
{
159+
private static readonly DiagNode<T> s_Empty = new DiagNode<T>(default!);
160+
159161
private DiagNode<T>? _nextNode;
160-
[AllowNull, MaybeNull] private T _currentItem;
162+
private DiagNode<T> _currentNode;
161163

162164
public Enumerator(DiagNode<T>? head)
163165
{
164166
_nextNode = head;
165-
_currentItem = default;
167+
_currentNode = s_Empty;
166168
}
167169

168-
public T Current => _currentItem!;
170+
public T Current => _currentNode.Value;
169171

170172
object? IEnumerator.Current => Current;
171173

172174
public bool MoveNext()
173175
{
174176
if (_nextNode == null)
175177
{
176-
_currentItem = default;
178+
_currentNode = s_Empty;
177179
return false;
178180
}
179181

180-
_currentItem = _nextNode.Value;
182+
_currentNode = _nextNode;
181183
_nextNode = _nextNode.Next;
182184
return true;
183185
}

0 commit comments

Comments
 (0)