Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

stephentoub
Copy link
Member

For years, async methods / Task.FromResult has cached a Task<T> for default(T), however it was only used when the result value was null. Earlier in this release we extended that optimization to also use the default task when the value was 1, 2, 4, or 8 bytes (and not a reference type). This extends that further to also handle types that are 16 bytes, so as to include types like Decimal, Guid, Int128, and DateTimeOffset.

[Params(42, 0)]
public int Value { get; set; }

[Benchmark]
public async Task<Int128> ReturnValue()
{
    await Task.CompletedTask;
    return Value;
}
Method Toolchain Value Mean Ratio Allocated Alloc Ratio
ReturnValue \main\corerun.exe 0 18.347 ns 1.00 80 B 1.00
ReturnValue \pr\corerun.exe 0 8.659 ns 0.47 - 0.00
ReturnValue \main\corerun.exe 42 17.742 ns 1.00 80 B 1.00
ReturnValue \pr\corerun.exe 42 17.744 ns 1.00 80 B 1.00

… types

For years, async methods / Task.FromResult has cached a `Task<T>` for `default(T)`, however it was only used when the result value was null.  Earlier in this release we extended that optimization to also use the default task when the value was 1, 2, 4, or 8 bytes (and not a reference type).  This extends that further to also handle types that are 16 bytes, so as to include types like Decimal, Guid, Int128, and DateTimeOffset.
@ghost
Copy link

ghost commented Jun 14, 2023

Tagging subscribers to this area: @dotnet/area-system-threading-tasks
See info in area-owners.md if you want to be subscribed.

Issue Details

For years, async methods / Task.FromResult has cached a Task<T> for default(T), however it was only used when the result value was null. Earlier in this release we extended that optimization to also use the default task when the value was 1, 2, 4, or 8 bytes (and not a reference type). This extends that further to also handle types that are 16 bytes, so as to include types like Decimal, Guid, Int128, and DateTimeOffset.

[Params(42, 0)]
public int Value { get; set; }

[Benchmark]
public async Task<Int128> ReturnValue()
{
    await Task.CompletedTask;
    return Value;
}
Method Toolchain Value Mean Ratio Allocated Alloc Ratio
ReturnValue \main\corerun.exe 0 18.347 ns 1.00 80 B 1.00
ReturnValue \pr\corerun.exe 0 8.659 ns 0.47 - 0.00
ReturnValue \main\corerun.exe 42 17.742 ns 1.00 80 B 1.00
ReturnValue \pr\corerun.exe 42 17.744 ns 1.00 80 B 1.00
Author: stephentoub
Assignees: stephentoub
Labels:

area-System.Threading.Tasks

Milestone: -

@stephentoub stephentoub force-pushed the taskresultvector branch 2 times, most recently from ef1aaa6 to 0a05f90 Compare June 14, 2023 12:44
@stephentoub stephentoub merged commit 9a33ae7 into dotnet:main Jun 15, 2023
@stephentoub stephentoub deleted the taskresultvector branch June 15, 2023 01:14
@ghost ghost locked as resolved and limited conversation to collaborators Jul 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants