summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorStephen Toub <stoub@microsoft.com>2017-09-11 14:40:57 -0700
committerStephen Toub <stoub@microsoft.com>2017-09-11 14:40:57 -0700
commiteb9bfb1607bcb7f681ca3eda670982b16fec96c7 (patch)
tree4bca3254196bf1162fbc3a1290b216cdffcd4565 /src
parent35a8c82f0ab9fe2e9ee8974ecca01e317b467800 (diff)
downloadcoreclr-eb9bfb1607bcb7f681ca3eda670982b16fec96c7.tar.gz
coreclr-eb9bfb1607bcb7f681ca3eda670982b16fec96c7.tar.bz2
coreclr-eb9bfb1607bcb7f681ca3eda670982b16fec96c7.zip
Use AsyncTaskCache in ValueTask.AsTask()
We can avoid task allocations for common values by using the same task cache that async methods do. This is important to avoid allocations in certain cases when switching from Task-returning to ValueTask-returning methods. If at some point we change Task.FromResult to use the same cache, this can be reverted.
Diffstat (limited to 'src')
-rw-r--r--src/mscorlib/shared/System/Threading/Tasks/ValueTask.cs2
-rw-r--r--src/mscorlib/src/System/Runtime/CompilerServices/AsyncMethodBuilder.cs2
2 files changed, 2 insertions, 2 deletions
diff --git a/src/mscorlib/shared/System/Threading/Tasks/ValueTask.cs b/src/mscorlib/shared/System/Threading/Tasks/ValueTask.cs
index 495388043f..384e4a8ab3 100644
--- a/src/mscorlib/shared/System/Threading/Tasks/ValueTask.cs
+++ b/src/mscorlib/shared/System/Threading/Tasks/ValueTask.cs
@@ -114,7 +114,7 @@ namespace System.Threading.Tasks
// Return the task if we were constructed from one, otherwise manufacture one. We don't
// cache the generated task into _task as it would end up changing both equality comparison
// and the hash code we generate in GetHashCode.
- _task ?? Task.FromResult(_result);
+ _task ?? AsyncTaskMethodBuilder<TResult>.GetTaskForResult(_result);
/// <summary>Gets whether the <see cref="ValueTask{TResult}"/> represents a completed operation.</summary>
public bool IsCompleted => _task == null || _task.IsCompleted;
diff --git a/src/mscorlib/src/System/Runtime/CompilerServices/AsyncMethodBuilder.cs b/src/mscorlib/src/System/Runtime/CompilerServices/AsyncMethodBuilder.cs
index 4e4de98ed0..eb198ba087 100644
--- a/src/mscorlib/src/System/Runtime/CompilerServices/AsyncMethodBuilder.cs
+++ b/src/mscorlib/src/System/Runtime/CompilerServices/AsyncMethodBuilder.cs
@@ -719,7 +719,7 @@ namespace System.Runtime.CompilerServices
/// <param name="result">The result for which we need a task.</param>
/// <returns>The completed task containing the result.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)] // method looks long, but for a given TResult it results in a relatively small amount of asm
- private Task<TResult> GetTaskForResult(TResult result)
+ internal static Task<TResult> GetTaskForResult(TResult result)
{
Contract.Ensures(
EqualityComparer<TResult>.Default.Equals(result, Contract.Result<Task<TResult>>().Result),