Skip to content

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 previous release. 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 previous release.

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