A few minor perf tweaks
authorStephen Toub <stoub@microsoft.com>
Mon, 24 Jul 2017 14:16:26 +0000 (10:16 -0400)
committerStephen Toub <stoub@microsoft.com>
Mon, 24 Jul 2017 14:44:01 +0000 (10:44 -0400)
- Update ValueTask to use IsCompletedSynchronously: IsCompletedSynchronously is faster/slimmer than Status == RanToCompletion.
- Remove a few readonly's causing unnecessary struct copies

src/mscorlib/shared/System/Runtime/CompilerServices/AsyncValueTaskMethodBuilder.cs
src/mscorlib/shared/System/Runtime/CompilerServices/ConfiguredValueTaskAwaitable.cs
src/mscorlib/shared/System/Runtime/CompilerServices/ValueTaskAwaiter.cs
src/mscorlib/shared/System/Threading/Tasks/ValueTask.cs

index e4c40a2..86eacaf 100644 (file)
@@ -14,7 +14,7 @@ namespace System.Runtime.CompilerServices
     public struct AsyncValueTaskMethodBuilder<TResult>
     {
         /// <summary>The <see cref="AsyncTaskMethodBuilder{TResult}"/> to which most operations are delegated.</summary>
-        private AsyncTaskMethodBuilder<TResult> _methodBuilder;
+        private AsyncTaskMethodBuilder<TResult> _methodBuilder; // mutable struct; do not make it readonly
         /// <summary>The result for this builder, if it's completed before any awaits occur.</summary>
         private TResult _result;
         /// <summary>true if <see cref="_result"/> contains the synchronous result for the async method; otherwise, false.</summary>
index 0c2d61d..4ec931c 100644 (file)
@@ -37,7 +37,7 @@ namespace System.Runtime.CompilerServices
         public struct ConfiguredValueTaskAwaiter : ICriticalNotifyCompletion
         {
             /// <summary>The value being awaited.</summary>
-            private readonly ValueTask<TResult> _value;
+            private ValueTask<TResult> _value; // Methods are called on this; avoid making it readonly so as to avoid unnecessary copies
             /// <summary>The value to pass to ConfigureAwait.</summary>
             private readonly bool _continueOnCapturedContext;
 
index ee9578c..c419482 100644 (file)
@@ -11,7 +11,7 @@ namespace System.Runtime.CompilerServices
     public struct ValueTaskAwaiter<TResult> : ICriticalNotifyCompletion
     {
         /// <summary>The value being awaited.</summary>
-        private readonly ValueTask<TResult> _value;
+        private ValueTask<TResult> _value; // Methods are called on this; avoid making it readonly so as to avoid unnecessary copies
 
         /// <summary>Initializes the awaiter.</summary>
         /// <param name="value">The value to be awaited.</param>
index 452424a..bafc8f5 100644 (file)
@@ -115,7 +115,7 @@ namespace System.Threading.Tasks
         public bool IsCompleted => _task == null || _task.IsCompleted;
 
         /// <summary>Gets whether the <see cref="ValueTask{TResult}"/> represents a successfully completed operation.</summary>
-        public bool IsCompletedSuccessfully => _task == null || _task.Status == TaskStatus.RanToCompletion;
+        public bool IsCompletedSuccessfully => _task == null || _task.IsCompletedSuccessfully;
 
         /// <summary>Gets whether the <see cref="ValueTask{TResult}"/> represents a failed operation.</summary>
         public bool IsFaulted => _task != null && _task.IsFaulted;
@@ -134,14 +134,14 @@ namespace System.Threading.Tasks
         /// true to attempt to marshal the continuation back to the captured context; otherwise, false.
         /// </param>
         public ConfiguredValueTaskAwaitable<TResult> ConfigureAwait(bool continueOnCapturedContext) =>
-            new ConfiguredValueTaskAwaitable<TResult>(this, continueOnCapturedContext: continueOnCapturedContext);
+            new ConfiguredValueTaskAwaitable<TResult>(this, continueOnCapturedContext);
 
         /// <summary>Gets a string-representation of this <see cref="ValueTask{TResult}"/>.</summary>
         public override string ToString()
         {
             if (_task != null)
             {
-                return _task.Status == TaskStatus.RanToCompletion && _task.Result != null ?
+                return _task.IsCompletedSuccessfully && _task.Result != null ?
                     _task.Result.ToString() :
                     string.Empty;
             }
@@ -153,7 +153,8 @@ namespace System.Threading.Tasks
             }
         }
 
-        // TODO: Remove CreateAsyncMethodBuilder once the C# compiler relies on the AsyncBuilder attribute.
+        // TODO https://github.com/dotnet/corefx/issues/22171:
+        // Remove CreateAsyncMethodBuilder once the C# compiler relies on the AsyncBuilder attribute.
 
         /// <summary>Creates a method builder for use with an async method.</summary>
         /// <returns>The created builder.</returns>