alastairtree / LazyCache

An easy to use thread safe in-memory caching service with a simple developer friendly API for c#
https://nuget.org/packages/LazyCache
MIT License
1.72k stars 159 forks source link

Use ValueTask instead of Task as async return value #132

Open joakimriedel opened 4 years ago

joakimriedel commented 4 years ago

[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?

alastairtree commented 4 years ago

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.

svengeance commented 4 years ago

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?