Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Use AsyncTaskCache in ValueTask.AsTask() #13907

Merged
merged 1 commit into from
Sep 12, 2017

Conversation

stephentoub
Copy link
Member

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.

cc: @kouvel, @tarekgh

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.
@tarekgh
Copy link
Member

tarekgh commented Sep 11, 2017

LGTM.

Task.FromResult to use the same cache, this can be reverted.

Is there anything preventing us to do that? I mean would be any compat or behavior concern? or just not a priority to do so now?

@stephentoub
Copy link
Member Author

Is there anything preventing us to do that? I mean would be any compat or behavior concern?

Technically there's a potential compat issue, but I'm not concerned about it. It's just the case where if you depend on object identity and we switch from always giving out a new object to potentially giving out a cached one, it could break such object identity comparisons. But we already decided for core we don't care about that (e.g. returning cached empty arrays), and honestly in this case I think it'd even be worth considering for desktop.

or just not a priority to do so now?

I would like to see it happen for 2.1. We just need to find the time. Actually making the change is trivial; we just need to make sure not to regress perf. The issue is that there are only a handful of tasks actually cached, e.g. true/false for bool, -1 to 8 for int, 0 values, etc., but Task.FromResult is used with lots of values, so we need to make sure the overhead of accessing the cache is worthwhile.

@benaadams
Copy link
Member

benaadams commented Sep 11, 2017

or just not a priority to do so now?

Put a PR in for Task.FromResult a long time ago #5186 but the perf was a regression https://github.com/dotnet/coreclr/issues/5185#issuecomment-221335320 so it would likely need more work that a simple tweak

@stephentoub stephentoub merged commit 9b0ba41 into dotnet:master Sep 12, 2017
@stephentoub stephentoub deleted the valuetask_astask_caching branch September 12, 2017 06:49
dotnet-bot pushed a commit to dotnet/corert that referenced this pull request Sep 12, 2017
…task_caching

Use AsyncTaskCache in ValueTask.AsTask()

Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
stephentoub added a commit to dotnet/corert that referenced this pull request Sep 12, 2017
…task_caching

Use AsyncTaskCache in ValueTask.AsTask()

Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants