From 027a9105ae2783a7fffd6038bf07a5cf67ba287b Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Fri, 4 Jan 2019 15:52:21 +0100 Subject: [PATCH] Skip additional cast from common EC.Run calls (#21789) * Skip additional cast from common EC.Run calls * Unsafe.As --- .../src/System/Runtime/CompilerServices/AsyncMethodBuilder.cs | 7 ++++++- src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs | 8 +++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/System.Private.CoreLib/src/System/Runtime/CompilerServices/AsyncMethodBuilder.cs b/src/System.Private.CoreLib/src/System/Runtime/CompilerServices/AsyncMethodBuilder.cs index df0e141..cfd45f6 100644 --- a/src/System.Private.CoreLib/src/System/Runtime/CompilerServices/AsyncMethodBuilder.cs +++ b/src/System.Private.CoreLib/src/System/Runtime/CompilerServices/AsyncMethodBuilder.cs @@ -527,7 +527,12 @@ namespace System.Runtime.CompilerServices where TStateMachine : IAsyncStateMachine { /// Delegate used to invoke on an ExecutionContext when passed an instance of this box type. - private static readonly ContextCallback s_callback = s => ((AsyncStateMachineBox)s).StateMachine.MoveNext(); + private static readonly ContextCallback s_callback = s => + { + Debug.Assert(s is AsyncStateMachineBox); + // Only used privately to pass directly to EC.Run + Unsafe.As>(s).StateMachine.MoveNext(); + }; /// A delegate to the method. private Action _moveNextAction; diff --git a/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs b/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs index b9b60dd..c3a4af3 100644 --- a/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs +++ b/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs @@ -17,6 +17,7 @@ using System.Diagnostics.Tracing; using System.Runtime.CompilerServices; using System.Runtime.ExceptionServices; using Internal.Runtime.Augments; +using Internal.Runtime.CompilerServices; // Disable the "reference to volatile field not treated as volatile" error. #pragma warning disable 0420 @@ -2485,7 +2486,12 @@ namespace System.Threading.Tasks } } - private static readonly ContextCallback s_ecCallback = obj => ((Task)obj).InnerInvoke(); + private static readonly ContextCallback s_ecCallback = obj => + { + Debug.Assert(obj is Task); + // Only used privately to pass directly to EC.Run + Unsafe.As(obj).InnerInvoke(); + }; /// /// The actual code which invokes the body of the task. This can be overridden in derived types. -- 2.7.4