Przeglądaj źródła

Save allocations of closures and allow delegate caching in Windows code that schedules async work. (#664)

* Save allocations of closures and allow delegate caching in Windows code that schedules async work.

* Cleanup some unused references.
Daniel C. Weber 7 lat temu
rodzic
commit
d981f09a5f

+ 3 - 3
Rx.NET/Source/src/System.Reactive/Concurrency/ConcurrencyAbstractionLayerImpl.Windows.cs

@@ -20,7 +20,7 @@ namespace System.Reactive.Concurrency
                 Normalize(dueTime)
             );
 
-            return Disposable.Create(res.Cancel);
+            return res.AsDisposable();
         }
 
         public IDisposable StartPeriodicTimer(Action action, TimeSpan period)
@@ -42,7 +42,7 @@ namespace System.Reactive.Concurrency
                 period
             );
 
-            return Disposable.Create(res.Cancel);
+            return res.AsDisposable();
         }
 
         public IDisposable QueueUserWorkItem(Action<object> action, object state)
@@ -52,7 +52,7 @@ namespace System.Reactive.Concurrency
                 action(state);
             });
 
-            return Disposable.Create(res.Cancel);
+            return res.AsDisposable();
         }
         
         public void Sleep(TimeSpan timeout)

+ 5 - 6
Rx.NET/Source/src/System.Reactive/Concurrency/ThreadPoolScheduler.Windows.cs

@@ -3,7 +3,6 @@
 // See the LICENSE file in the project root for more information. 
 
 #if WINDOWS
-using System.Reactive.Disposables;
 using Windows.System.Threading;
 
 namespace System.Reactive.Concurrency
@@ -75,12 +74,12 @@ namespace System.Reactive.Concurrency
 
             var userWorkItem = new UserWorkItem<TState>(this, state, action);
             
-            var res = global::Windows.System.Threading.ThreadPool.RunAsync(
+            var res = ThreadPool.RunAsync(
                 iaa => userWorkItem.Run(),
                 Priority,
                 Options);
 
-            userWorkItem.CancelQueueDisposable = Disposable.Create(res.Cancel);
+            userWorkItem.CancelQueueDisposable = res.AsDisposable();
 
             return userWorkItem;
         }
@@ -113,11 +112,11 @@ namespace System.Reactive.Concurrency
         {
             var userWorkItem = new UserWorkItem<TState>(this, state, action);
 
-            var res = global::Windows.System.Threading.ThreadPoolTimer.CreateTimer(
+            var res = ThreadPoolTimer.CreateTimer(
                 tpt => userWorkItem.Run(),
                 dueTime);
 
-            userWorkItem.CancelQueueDisposable = Disposable.Create(res.Cancel);
+            userWorkItem.CancelQueueDisposable = res.AsDisposable();
 
             return userWorkItem;
         }
@@ -161,7 +160,7 @@ namespace System.Reactive.Concurrency
                 _state = state;
                 _action = action;
 
-                _timer = global::Windows.System.Threading.ThreadPoolTimer.CreatePeriodicTimer(
+                _timer = ThreadPoolTimer.CreatePeriodicTimer(
                     Tick,
                     period);
             }

+ 1 - 1
Rx.NET/Source/src/System.Reactive/Platforms/UWP/Concurrency/CoreDispatcherScheduler.cs

@@ -126,7 +126,7 @@ namespace System.Reactive.Concurrency
 
             return new CompositeDisposable(
                 d,
-                Disposable.Create(res.Cancel)
+                res.AsDisposable()
             );
         }
 

+ 25 - 0
Rx.NET/Source/src/System.Reactive/Platforms/Windows/ThreadPoolTimerExtensions.cs

@@ -0,0 +1,25 @@
+// 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. 
+
+#if WINDOWS
+using System.Reactive.Disposables;
+using Windows.System.Threading;
+using Windows.Foundation;
+
+namespace System.Reactive.Concurrency
+{
+    internal static class ThreadPoolTimerExtensions
+    {
+        public static IDisposable AsDisposable(this ThreadPoolTimer threadPoolTimer)
+        {
+            return Disposable.Create(threadPoolTimer, _ => _.Cancel());
+        }
+
+        public static IDisposable AsDisposable(this IAsyncInfo asyncInfo)
+        {
+            return Disposable.Create(asyncInfo, _ => _.Cancel());
+        }
+    }
+}
+#endif