Browse Source

Some small memory improvements in Ix-Async.

Bart De Smet 10 years ago
parent
commit
4e18edd415

+ 7 - 7
Ix.NET/Source/System.Interactive.Async/AsyncEnumerable.Creation.cs

@@ -76,7 +76,7 @@ namespace System.Linq
             public Task<bool> MoveNext(CancellationToken cancellationToken)
             {
                 if (_disposed)
-                    return TaskExt.Return(false, CancellationToken.None);
+                    return TaskExt.False;
 
                 return _moveNext(cancellationToken);
             }
@@ -110,7 +110,7 @@ namespace System.Linq
                 throw new ArgumentNullException("exception");
 
             return Create(() => Create<TValue>(
-                ct => TaskExt.Throw<bool>(exception, ct),
+                ct => TaskExt.Throw<bool>(exception),
                 () => { throw new InvalidOperationException(); },
                 () => { })
             );
@@ -128,7 +128,7 @@ namespace System.Linq
         public static IAsyncEnumerable<TValue> Empty<TValue>()
         {
             return Create(() => Create<TValue>(
-                ct => TaskExt.Return(false, ct),
+                ct => TaskExt.False,
                 () => { throw new InvalidOperationException(); },
                 () => { })
             );
@@ -155,7 +155,7 @@ namespace System.Linq
             return Create(() =>
             {
                 return Create(
-                    ct => TaskExt.Return(true, ct),
+                    ct => TaskExt.True,
                     () => element,
                     () => { }
                 );
@@ -201,16 +201,16 @@ namespace System.Linq
                         }
                         catch (Exception ex)
                         {
-                            return TaskExt.Throw<bool>(ex, ct);
+                            return TaskExt.Throw<bool>(ex);
                         }
 
                         if (!b)
-                            return TaskExt.Return(false, ct);
+                            return TaskExt.False;
 
                         if (!started)
                             started = true;
 
-                        return TaskExt.Return(true, ct);
+                        return TaskExt.True;
                     },
                     () => current,
                     () => { }

+ 1 - 1
Ix.NET/Source/System.Interactive.Async/AsyncEnumerable.Single.cs

@@ -482,7 +482,7 @@ namespace System.Linq
                     (ct, tcs) =>
                     {
                         if (n == 0)
-                            return TaskExt.Return(false, cts.Token);
+                            return TaskExt.False;
 
                         e.MoveNext(cts.Token).Then(t =>
                         {

+ 11 - 2
Ix.NET/Source/System.Interactive.Async/TaskExt.cs

@@ -7,14 +7,23 @@ namespace System.Threading.Tasks
 {
     static class TaskExt
     {
-        public static Task<T> Return<T>(T value, CancellationToken cancellationToken)
+        public static readonly Task<bool> True;
+        public static readonly Task<bool> False;
+
+        static TaskExt()
+        {
+            True = Return(true);
+            False = Return(false);
+        }
+
+        public static Task<T> Return<T>(T value)
         {
             var tcs = new TaskCompletionSource<T>();
             tcs.TrySetResult(value);
             return tcs.Task;
         }
 
-        public static Task<T> Throw<T>(Exception exception, CancellationToken cancellationToken)
+        public static Task<T> Throw<T>(Exception exception)
         {
             var tcs = new TaskCompletionSource<T>();
             tcs.TrySetException(exception);

+ 3 - 3
Ix.NET/Source/Tests/AsyncTests.Bugs.cs

@@ -101,11 +101,11 @@ namespace Tests
         [TestMethod]
         public void CorrectDispose()
         {
-            var disposed = false;
+            var disposed = new TaskCompletionSource<bool>();
 
             var xs = new[] { 1, 2, 3 }.WithDispose(() =>
             {
-                disposed = true;
+                disposed.TrySetResult(true);
             }).ToAsyncEnumerable();
 
             var ys = xs.Select(x => x + 1);
@@ -113,7 +113,7 @@ namespace Tests
             var e = ys.GetEnumerator();
             e.Dispose();
 
-            Assert.IsTrue(disposed);
+            Assert.IsTrue(disposed.Task.Result);
 
             Assert.IsFalse(e.MoveNext().Result);
         }

+ 9 - 9
Ix.NET/Source/Tests/AsyncTests.Creation.cs

@@ -309,13 +309,13 @@ namespace Tests
         public void Using4()
         {
             var i = 0;
-            var disposed = false;
+            var disposed = new TaskCompletionSource<bool>();
 
             var xs = AsyncEnumerable.Using(
                 () =>
                 {
                     i++;
-                    return new MyD(() => { disposed = true; });
+                    return new MyD(() => { disposed.TrySetResult(true); });
                 },
                 _ => AsyncEnumerable.Return(42)
             );
@@ -328,7 +328,7 @@ namespace Tests
             HasNext(e, 42);
             NoNext(e);
 
-            Assert.IsTrue(disposed);
+            Assert.IsTrue(disposed.Task.Result);
         }
 
         [TestMethod]
@@ -336,13 +336,13 @@ namespace Tests
         {
             var ex = new Exception("Bang!");
             var i = 0;
-            var disposed = false;
+            var disposed = new TaskCompletionSource<bool>();
 
             var xs = AsyncEnumerable.Using(
                 () =>
                 {
                     i++;
-                    return new MyD(() => { disposed = true; });
+                    return new MyD(() => { disposed.TrySetResult(true); });
                 },
                 _ => AsyncEnumerable.Throw<int>(ex)
             );
@@ -354,20 +354,20 @@ namespace Tests
 
             AssertThrows<Exception>(() => e.MoveNext().Wait(), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex);
 
-            Assert.IsTrue(disposed);
+            Assert.IsTrue(disposed.Task.Result);
         }
 
         [TestMethod]
         public void Using6()
         {
             var i = 0;
-            var disposed = false;
+            var disposed = new TaskCompletionSource<bool>();
 
             var xs = AsyncEnumerable.Using(
                 () =>
                 {
                     i++;
-                    return new MyD(() => { disposed = true; });
+                    return new MyD(() => { disposed.TrySetResult(true); });
                 },
                 _ => AsyncEnumerable.Range(0, 10)
             );
@@ -393,7 +393,7 @@ namespace Tests
                 ex.Flatten().Handle(inner => inner is TaskCanceledException);
             }
 
-            Assert.IsTrue(disposed);
+            Assert.IsTrue(disposed.Task.Result);
         }
 
         class MyD : IDisposable