-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Encapsulate the logic of caching the last synchronously completed task. #61781
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
237afa1
86313f0
bb36bd0
027f5e7
5e1dd50
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace System.Threading.Tasks | ||
{ | ||
/// <summary> | ||
/// Encapsulates the logic of caching the last synchronously completed task of integer. | ||
/// Used in classes like <see cref="MemoryStream"/> to reduce allocations. | ||
/// </summary> | ||
internal struct CachedCompletedInt32Task | ||
{ | ||
private Task<int>? _task; | ||
|
||
/// <summary> | ||
/// Gets a completed <see cref="Task{Int32}"/> whose result is <paramref name="result"/>. | ||
/// </summary> | ||
/// <remarks> | ||
/// This method will try to return an already cached task if available. | ||
/// </remarks> | ||
/// <param name="result">The task's result.</param> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public Task<int> GetTask(int result) | ||
{ | ||
Task<int>? task; | ||
#pragma warning disable CA1849 // Call async methods when in an async method | ||
if ((task = _task) is not null && task.Result == result) | ||
#pragma warning restore CA1849 // Call async methods when in an async method | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't have CA1849 enabled. Why is this suppression needed? There are many other places in the code where it would trigger if it were to be enabled, which is why we haven't enabled it... it still needs work. |
||
{ | ||
Debug.Assert(task.IsCompletedSuccessfully, | ||
"Expected that a stored last task completed successfully"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This assert is kind of meaningless: if the task didn't complete successfully, the task.Result in the if condition would have thrown. It would make more sense if it were between the check for null and accessing Result. |
||
return task; | ||
} | ||
else | ||
{ | ||
return _task = Task.FromResult(result); | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could have been: