Skip to content
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

Change Task.FromResult to use same task cache as async methods #43894

Merged
merged 2 commits into from
Oct 28, 2020

Conversation

stephentoub
Copy link
Member

@stephentoub stephentoub commented Oct 27, 2020

Task.FromResult today always creates a new task. This leads developers that are aware of this to then create their own caches for common values, typically 0, true, and false, to avoid task allocations for those common values. But we already have such values cached internally, used for async method return values. We can just use the same cache for Task.FromResult.

Fixes #22627

@stephentoub stephentoub added area-System.Threading.Tasks breaking-change Issue or PR that represents a breaking API or functional change over a prerelease. labels Oct 27, 2020
@stephentoub stephentoub added this to the 6.0.0 milestone Oct 27, 2020
@ghost
Copy link

ghost commented Oct 27, 2020

Tagging subscribers to this area: @tarekgh
See info in area-owners.md if you want to be subscribed.

@ghost ghost added the needs-breaking-change-doc-created Breaking changes need an issue opened with https://github.com/dotnet/docs/issues/new?template=dotnet label Oct 27, 2020
Task.FromResult today always creates a new task.  This leads developers that are aware of this to then create their own caches for common values, typically 0, true, and false, to avoid task allocations for those common values.  But we already have such values cached internally, used for async method return values.  We can just use the same cache for Task.FromResult.
@stephentoub stephentoub merged commit cf276ce into dotnet:master Oct 28, 2020
@stephentoub stephentoub deleted the cachedtaskfromresult branch October 28, 2020 10:50
@ghost ghost locked as resolved and limited conversation to collaborators Dec 6, 2020
@stephentoub
Copy link
Member Author

dotnet/docs#26321

@stephentoub stephentoub removed the needs-breaking-change-doc-created Breaking changes need an issue opened with https://github.com/dotnet/docs/issues/new?template=dotnet label Sep 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Threading.Tasks breaking-change Issue or PR that represents a breaking API or functional change over a prerelease.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provide common Task results for "true", "false", "empty", "null", etc
5 participants