Repository navigation
Description
Background and Motivation
MemoryCache by default tracks linked cache entries to allow for propagating options.
In the following example, the expirationToken added for child is also applied to parent:
using (var parent = cache.CreateEntry(key))
{
parent.SetValue(obj);
using (var child = cache.CreateEntry(key1))
{
child.SetValue(obj);
child.AddExpirationToken(expirationToken);
}
}Tracking internally uses AsyncLocal<T>, which is expensive and adds non-trivial overhead. The cost of using it has popped out in the performance reports that we have received from our customers (#45436).
Currently, this feature can not be disabled.
Proposed API
As suggested by @Tratcher in #45436 (comment), we can extend the existing MemoryCacheOptions with a flag that disables this behaviour.
Naming is hard and the best idea I currently have is TrackLinkedCacheEntries:
namespace Microsoft.Extensions.Caching.Memory
{
public class MemoryCacheOptions
{
+ public bool TrackLinkedCacheEntries { get ; init; } = true;
}
}I am open to suggestions.
Usage Examples
var options = new MemoryCacheOptions
{
TrackLinkedCacheEntries = false
};
var cache = new MemoryCache(options);Alternative Designs
As an alternative, we could add a new method to MemoryCache that would allow for creating cache entries without tracking:
namespace Microsoft.Extensions.Caching.Memory
{
public class MemoryCache
{
public ICacheEntry CreateEntry(object key); // existing method
+ public ICacheEntry CreateUnlinkedEntry(object key);
}But there is a lot of existing extension methods that allow for adding new cache entries to the cache and we would most probably need to add new overloads for them...
Risks
Introducing this API has no risks as long as we don't change the default settings (enabled by default).
Activity
Tratcher commentedon Dec 7, 2020
Note @davidfowl asked for this to be disabled by default and allow the few people that actually need the feature to opt-in with the new setting. Not sure if that meets the compat bar.
NickCraver commentedon Dec 9, 2020
For what it's worth, at scale we found a lot of the weight of our cache (somewhere in the 30-40% range) was overhead of the
CacheEntryobject itself. The per-entry storage onCacheEntrycurrently is:In our slimmed down version removing a lot of the callback weight we weren't using anywhere (in the vein of this issue), we narrowed down to:
Note: we actually added
_accessCounthere for evaluation of unused cache, which was a valuable feature for seeing where lot of unused waste was. Happy to share code here on all those bits (cc @mgravell) - it's something we found very valuable to have a dashboard for, in the running application, to see where we can easily slim down further.When you're storing a lot of things like integers or bools or any small data type, the weight of the reference overhead is quite large. What is the thinking on how we can reduce that? Note that most of it is inherent to the
ICacheEntryinterface and so a break means a much larger change...which is the route we ended up taking. Happy to help however we can here, and appreciate the kick on this issue!adamsitnik commentedon Dec 9, 2020
@NickCraver thanks for providing very valuable feedback! I've already removed 4 fields from CacheEntry (#45410) but it looks like we still have work to do here. I'll try to come up with more ideas
adamsitnik commentedon Dec 11, 2020
@NickCraver I've sent #45962 to minimize the overhead (280 -> 224 bytes). I'll try to wrap my head if it would be possible to replace the nullable date-related fields with a single
struct. It would be much easier if I could introduce breaking changes ;)bartonjs commentedon Aug 10, 2021
Since this is proposing a breaking change (making the default be non-tracked), going with one centralized option seems the most sensible.
Adding an overload of
MemoryCache.CreateEntry(object key, bool linked)(or something like that) can be done in the future if entry-level support feels warranted.