LazyCache icon indicating copy to clipboard operation
LazyCache copied to clipboard

Use ValueTask instead of Task as async return value

Open joakimriedel opened this issue 3 years ago • 2 comments

[Use the Thumbs Up reaction to vote for this feature, and please avoid adding comments like "+1" as they create noise for others watching the issue.]

I'm looking for a library to cache values at certain hot paths in my code.

Browsing through your API, I see that the async methods are returning Task and not ValueTask. The latter would be preferred since most of the requests would be cached and could be returned as T and not require the overhead of generating an actual Task<T> object.

Enhancement is to change GetAsync<T>, GetOrAddAsync<T> to return ValueTask<T>, but would require changing code all the way down to where T is actually returned in the providers to support ValueTask<T>. Perhaps for 3.0?

joakimriedel avatar Sep 23 '20 08:09 joakimriedel

Yeah this seems like a very sensible suggestion for a v3+ release. Would be best to have done the benchmarking beforehand so we can compare the resulting number of allocations between both versions to ensure it is better, but seems very likely that it will be.

alastairtree avatar Sep 23 '20 08:09 alastairtree

I'm assuming we'd have to migrate off of the Microsoft.Caching.Extensions namespace (https://source.dot.net/#Microsoft.Extensions.Caching.Abstractions/MemoryCacheExtensions.cs,105) and recreate this as ValueTask?

svengeance avatar Sep 26 '20 09:09 svengeance