Reduce unnecessary allocation of SeldomUsedFields in MemoryCache. (#51761)
authorSteve Molloy <smolloy@microsoft.com>
Thu, 6 May 2021 22:23:46 +0000 (15:23 -0700)
committerGitHub <noreply@github.com>
Thu, 6 May 2021 22:23:46 +0000 (15:23 -0700)
* Don't create SeldomUsedFields for an empty dependency collection.

* comment typo

Co-authored-by: Christopher Watford <christopher.watford@gmail.com>
Co-authored-by: Christopher Watford <christopher.watford@gmail.com>
src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/MemoryCacheEntry.cs

index b17bf5f..3adfbb7 100644 (file)
@@ -144,7 +144,10 @@ namespace System.Runtime.Caching
 
             _callback = removedCallback;
 
-            if (dependencies != null)
+            // CacheItemPolicy.ChangeMonitors is frequently the source for 'dependencies', and that property
+            // is never null. So check that the collection of dependencies is not empty before allocating
+            // the 'seldom' used fields.
+            if (dependencies != null && dependencies.Count > 0)
             {
                 _fields = new SeldomUsedFields();
                 _fields._dependencies = dependencies;