瀏覽代碼

Undo benchmark, undo Finally

Dávid Karnok 7 年之前
父節點
當前提交
ed7bc62379

+ 0 - 164
Ix.NET/Source/Benchmarks.System.Interactive/AsyncReturnBenchmark.cs

@@ -1,164 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the Apache 2.0 License.
-// See the LICENSE file in the project root for more information.
-
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Threading;
-using System.Threading.Tasks;
-using BenchmarkDotNet.Attributes;
-
-namespace Benchmarks.System.Interactive
-{
-    [MemoryDiagnoser]
-    public class AsyncReturnBenchmark
-    {
-        [Benchmark]
-        public async ValueTask ToAsyncEnumerable()
-        {
-            await new[] { 1 }.ToAsyncEnumerable().ForEachAsync(v => { }).ConfigureAwait(false);
-        }
-
-        [Benchmark]
-        public async ValueTask Direct()
-        {
-            await AsyncEnumerableEx.Return(1).ForEachAsync(v => { }).ConfigureAwait(false);
-        }
-
-        [Benchmark]
-        public async ValueTask Iterator()
-        {
-            await new ReturnIterator<int>(1).ForEachAsync(v => { }).ConfigureAwait(false);
-        }
-
-    }
-
-    internal sealed class ReturnIterator<T> : AsyncIterator<T>
-    {
-        private readonly T _value;
-
-        public override AsyncIterator<T> Clone()
-        {
-            return new ReturnIterator<T>(_value);
-        }
-
-        public ReturnIterator(T value)
-        {
-            _value = value;
-        }
-
-        protected override async ValueTask<bool> MoveNextCore(CancellationToken cancellationToken)
-        {
-            if (state == AsyncIteratorState.Allocated)
-            {
-                current = _value;
-                state = AsyncIteratorState.Disposed;
-                return true;
-            }
-
-            await DisposeAsync().ConfigureAwait(false);
-            return false;
-        }
-    }
-
-    internal abstract class AsyncIterator<TSource> : IAsyncEnumerable<TSource>, IAsyncEnumerator<TSource>
-    {
-        private readonly int _threadId;
-
-        private bool _currentIsInvalid = true;
-
-        internal TSource current;
-        internal AsyncIteratorState state = AsyncIteratorState.New;
-        internal CancellationToken token;
-
-        protected AsyncIterator()
-        {
-            _threadId = Environment.CurrentManagedThreadId;
-        }
-
-        public IAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken token)
-        {
-            var enumerator = state == AsyncIteratorState.New && _threadId == Environment.CurrentManagedThreadId
-                ? this
-                : Clone();
-
-            enumerator.state = AsyncIteratorState.Allocated;
-            enumerator.token = token;
-
-            try
-            {
-                enumerator.OnGetEnumerator(token);
-            }
-            catch
-            {
-                enumerator.DisposeAsync(); // REVIEW: fire-and-forget?
-                throw;
-            }
-
-            return enumerator;
-        }
-
-        public virtual ValueTask DisposeAsync()
-        {
-            current = default;
-            state = AsyncIteratorState.Disposed;
-
-            return new ValueTask();
-        }
-
-        public TSource Current
-        {
-            get
-            {
-                if (_currentIsInvalid)
-                    throw new InvalidOperationException("Enumerator is in an invalid state");
-
-                return current;
-            }
-        }
-
-        public async ValueTask<bool> MoveNextAsync()
-        {
-            // Note: MoveNext *must* be implemented as an async method to ensure
-            // that any exceptions thrown from the MoveNextCore call are handled 
-            // by the try/catch, whether they're sync or async
-
-            if (state == AsyncIteratorState.Disposed)
-            {
-                return false;
-            }
-
-            try
-            {
-                var result = await MoveNextCore(token).ConfigureAwait(false);
-
-                _currentIsInvalid = !result; // if move next is false, invalid otherwise valid
-
-                return result;
-            }
-            catch
-            {
-                _currentIsInvalid = true;
-                await DisposeAsync().ConfigureAwait(false);
-                throw;
-            }
-        }
-
-        public abstract AsyncIterator<TSource> Clone();
-
-        protected abstract ValueTask<bool> MoveNextCore(CancellationToken cancellationToken);
-
-        protected virtual void OnGetEnumerator(CancellationToken cancellationToken)
-        {
-        }
-    }
-
-    internal enum AsyncIteratorState
-    {
-        New = 0,
-        Allocated = 1,
-        Iterating = 2,
-        Disposed = -1,
-    }
-}

+ 0 - 2
Ix.NET/Source/Benchmarks.System.Interactive/Benchmarks.System.Interactive.csproj

@@ -32,8 +32,6 @@
   </ItemGroup>
 
   <ItemGroup>
-    <ProjectReference Include="..\System.Interactive.Async\System.Interactive.Async.csproj" />
     <ProjectReference Include="..\System.Interactive.Tests\System.Interactive.Tests.csproj" />
-    <ProjectReference Include="..\System.Linq.Async\System.Linq.Async.csproj" />
   </ItemGroup>
 </Project>

+ 1 - 2
Ix.NET/Source/Benchmarks.System.Interactive/Program.cs

@@ -21,8 +21,7 @@ namespace Benchmarks.System.Interactive
                 typeof(IgnoreElementsBenchmark),
                 typeof(DeferBenchmark),
                 typeof(RetryBenchmark),
-                typeof(MinMaxBenchmark),
-                typeof(AsyncReturnBenchmark)
+                typeof(MinMaxBenchmark)
             });
 
             switcher.Run();

+ 1 - 40
Ix.NET/Source/System.Interactive.Async.Tests/System/Linq/Operators/Finally.cs

@@ -114,46 +114,7 @@ namespace Tests
             var xs = new[] { 1, 2 }.ToAsyncEnumerable().Finally(() => { i++; });
 
             await SequenceIdentity(xs);
-            Assert.Equal(4, i);
-        }
-
-        [Fact]
-        public async Task Finally7_Async()
-        {
-            var i = 0;
-            var xs = new[] { 1, 2 }.ToAsyncEnumerable().Finally(async () => {
-                await Task.CompletedTask;
-                i++;
-            });
-
-            await SequenceIdentity(xs);
-            Assert.Equal(4, i);
-        }
-
-        [Fact]
-        public async Task Finally8()
-        {
-            var i = 0;
-            var en = AsyncEnumerable.Range(1, 5).Finally(() => i++).GetAsyncEnumerator();
-
-            await en.DisposeAsync();
-
-            Assert.Equal(1, i);
-        }
-
-        [Fact]
-        public async Task Finally8_Async()
-        {
-            var i = 0;
-            var en = AsyncEnumerable.Range(1, 5).Finally(async () =>
-            {
-                await Task.CompletedTask;
-                i++;
-            }).GetAsyncEnumerator();
-
-            await en.DisposeAsync();
-
-            Assert.Equal(1, i);
+            Assert.Equal(2, i);
         }
     }
 }

+ 0 - 17
Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Finally.cs

@@ -38,8 +38,6 @@ namespace System.Linq
 
             private IAsyncEnumerator<TSource> _enumerator;
 
-            private bool _finallyOnce;
-
             public FinallyAsyncIterator(IAsyncEnumerable<TSource> source, Action finallyAction)
             {
                 Debug.Assert(source != null);
@@ -61,13 +59,6 @@ namespace System.Linq
                     await _enumerator.DisposeAsync().ConfigureAwait(false);
                     _enumerator = null;
 
-                }
-
-                // Run the _finallyAction even if
-                // the consumer did not call MoveNextAsync
-                if (!_finallyOnce)
-                {
-                    _finallyOnce = true;
                     _finallyAction();
                 }
 
@@ -105,8 +96,6 @@ namespace System.Linq
 
             private IAsyncEnumerator<TSource> _enumerator;
 
-            private bool _finallyOnce;
-
             public FinallyAsyncIteratorWithTask(IAsyncEnumerable<TSource> source, Func<Task> finallyAction)
             {
                 Debug.Assert(source != null);
@@ -127,13 +116,7 @@ namespace System.Linq
                 {
                     await _enumerator.DisposeAsync().ConfigureAwait(false);
                     _enumerator = null;
-                }
 
-                // Await the _finallyAction even if
-                // the consumer did not call MoveNextAsync
-                if (!_finallyOnce)
-                {
-                    _finallyOnce = true;
                     await _finallyAction().ConfigureAwait(false);
                 }