瀏覽代碼

Save the allocation of a SingleAssignmentDisposable in GetEnumerator. (#657)

Daniel C. Weber 7 年之前
父節點
當前提交
53d0e46ff4
共有 1 個文件被更改,包括 5 次插入6 次删除
  1. 5 6
      Rx.NET/Source/src/System.Reactive/Linq/Observable/GetEnumerator.cs

+ 5 - 6
Rx.NET/Source/src/System.Reactive/Linq/Observable/GetEnumerator.cs

@@ -17,15 +17,14 @@ namespace System.Reactive.Linq.ObservableImpl
         private Exception _error;
         private bool _done;
         private bool _disposed;
+        private IDisposable _subscription;
 
         private readonly SemaphoreSlim _gate;
-        private readonly SingleAssignmentDisposable _subscription;
 
         public GetEnumerator()
         {
             _queue = new ConcurrentQueue<TSource>();
             _gate = new SemaphoreSlim(0);
-            _subscription = new SingleAssignmentDisposable();
         }
 
         public IEnumerator<TSource> Run(IObservable<TSource> source)
@@ -33,7 +32,7 @@ namespace System.Reactive.Linq.ObservableImpl
             //
             // [OK] Use of unsafe Subscribe: non-pretentious exact mirror with the dual GetEnumerator method.
             //
-            _subscription.Disposable = source.Subscribe/*Unsafe*/(this);
+            Disposable.TrySetSingle(ref _subscription, source.Subscribe/*Unsafe*/(this));
             return this;
         }
 
@@ -46,14 +45,14 @@ namespace System.Reactive.Linq.ObservableImpl
         public void OnError(Exception error)
         {
             _error = error;
-            _subscription.Dispose();
+            Disposable.TryDispose(ref _subscription);
             _gate.Release();
         }
 
         public void OnCompleted()
         {
             _done = true;
-            _subscription.Dispose();
+            Disposable.TryDispose(ref _subscription);
             _gate.Release();
         }
 
@@ -81,7 +80,7 @@ namespace System.Reactive.Linq.ObservableImpl
 
         public void Dispose()
         {
-            _subscription.Dispose();
+            Disposable.TryDispose(ref _subscription);
 
             _disposed = true;
             _gate.Release();