Browse Source

Don't pay for the _current field if unused.

Bart De Smet 7 years ago
parent
commit
f73236d97f
1 changed files with 17 additions and 5 deletions
  1. 17 5
      Ix.NET/Source/System.Linq.Async/System/Linq/AsyncIterator.cs

+ 17 - 5
Ix.NET/Source/System.Linq.Async/System/Linq/AsyncIterator.cs

@@ -8,15 +8,14 @@ using System.Threading.Tasks;
 
 namespace System.Linq
 {
-    internal abstract partial class AsyncIterator<TSource> : IAsyncEnumerable<TSource>, IAsyncEnumerator<TSource>
+    internal abstract class AsyncIteratorBase<TSource> : IAsyncEnumerable<TSource>, IAsyncEnumerator<TSource>
     {
         private readonly int _threadId;
 
-        protected TSource _current;
         protected AsyncIteratorState _state = AsyncIteratorState.New;
         protected CancellationToken _cancellationToken;
 
-        protected AsyncIterator()
+        protected AsyncIteratorBase()
         {
             _threadId = Environment.CurrentManagedThreadId;
         }
@@ -38,13 +37,12 @@ namespace System.Linq
 
         public virtual ValueTask DisposeAsync()
         {
-            _current = default;
             _state = AsyncIteratorState.Disposed;
 
             return default;
         }
 
-        public TSource Current => _current;
+        public abstract TSource Current { get; }
 
         public async ValueTask<bool> MoveNextAsync()
         {
@@ -73,6 +71,20 @@ namespace System.Linq
         protected abstract ValueTask<bool> MoveNextCore();
     }
 
+    internal abstract partial class AsyncIterator<TSource> : AsyncIteratorBase<TSource>
+    {
+        protected TSource _current;
+
+        public override TSource Current => _current;
+
+        public override ValueTask DisposeAsync()
+        {
+            _current = default;
+
+            return base.DisposeAsync();
+        }
+    }
+
     internal enum AsyncIteratorState
     {
         New = 0,