From caf9698a043832363620d8849d6601a45de43bfd Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Mon, 16 Nov 2020 15:03:10 -0500 Subject: [PATCH] Remove unnecessary closure / delegate allocation from CallSiteFactory.GetCallSite Nothing is ever removed from the cache, and CreateCallSite is already storing the newly created ServiceCallSite into the dictionary, so GetOrAdd isn't providing any meaningful value: we can simply TryGetValue and then call CreateCallSite if it fails. --- .../src/ServiceLookup/CallSiteFactory.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/CallSiteFactory.cs b/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/CallSiteFactory.cs index f3b919212255f7..f9e7e42036ba54 100644 --- a/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/CallSiteFactory.cs +++ b/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/CallSiteFactory.cs @@ -70,10 +70,9 @@ private void Populate() } } - internal ServiceCallSite GetCallSite(Type serviceType, CallSiteChain callSiteChain) - { - return _callSiteCache.GetOrAdd(serviceType, type => CreateCallSite(type, callSiteChain)); - } + internal ServiceCallSite GetCallSite(Type serviceType, CallSiteChain callSiteChain) => + _callSiteCache.TryGetValue(serviceType, out ServiceCallSite site) ? site : + CreateCallSite(serviceType, callSiteChain); internal ServiceCallSite GetCallSite(ServiceDescriptor serviceDescriptor, CallSiteChain callSiteChain) {