From: David Fowler Date: Tue, 22 Jun 2021 21:07:54 +0000 (-0700) Subject: Improve performance of IsRootScope check (#54555) X-Git-Tag: submit/tizen/20210909.063632~643 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=9da9bfcaffca3329ad9efdfccd89ef9e9aaf2930;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Improve performance of IsRootScope check (#54555) - Stash a field instead of doing an equality comparison. Fixes #54351 --- diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/ServiceProviderEngineScope.cs b/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/ServiceProviderEngineScope.cs index 06498ff..4ad8b15 100644 --- a/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/ServiceProviderEngineScope.cs +++ b/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/ServiceProviderEngineScope.cs @@ -16,10 +16,11 @@ namespace Microsoft.Extensions.DependencyInjection.ServiceLookup private bool _disposed; private List _disposables; - public ServiceProviderEngineScope(ServiceProvider provider) + public ServiceProviderEngineScope(ServiceProvider provider, bool isRootScope) { ResolvedServices = new Dictionary(); RootProvider = provider; + IsRootScope = isRootScope; } internal Dictionary ResolvedServices { get; } @@ -29,7 +30,7 @@ namespace Microsoft.Extensions.DependencyInjection.ServiceLookup // For other scopes, it protects ResolvedServices and the list of disposables internal object Sync => ResolvedServices; - public bool IsRootScope => this == RootProvider.Root; + public bool IsRootScope { get; } internal ServiceProvider RootProvider { get; } diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceProvider.cs b/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceProvider.cs index d21d980..fb7052e 100644 --- a/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceProvider.cs +++ b/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceProvider.cs @@ -36,7 +36,7 @@ namespace Microsoft.Extensions.DependencyInjection _createServiceAccessor = CreateServiceAccessor; _realizedServices = new ConcurrentDictionary>(); - Root = new ServiceProviderEngineScope(this); + Root = new ServiceProviderEngineScope(this, isRootScope: true); CallSiteFactory = new CallSiteFactory(serviceDescriptors); // The list of built in services that aren't part of the list of service descriptors // keep this in sync with CallSiteFactory.IsService @@ -173,7 +173,7 @@ namespace Microsoft.Extensions.DependencyInjection ThrowHelper.ThrowObjectDisposedException(); } - return new ServiceProviderEngineScope(this); + return new ServiceProviderEngineScope(this, isRootScope: false); } private ServiceProviderEngine GetEngine() diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection/tests/DI.Tests/ServiceProviderEngineScopeTests.cs b/src/libraries/Microsoft.Extensions.DependencyInjection/tests/DI.Tests/ServiceProviderEngineScopeTests.cs index 5c19caa..d43752d 100644 --- a/src/libraries/Microsoft.Extensions.DependencyInjection/tests/DI.Tests/ServiceProviderEngineScopeTests.cs +++ b/src/libraries/Microsoft.Extensions.DependencyInjection/tests/DI.Tests/ServiceProviderEngineScopeTests.cs @@ -12,7 +12,7 @@ namespace Microsoft.Extensions.DependencyInjection.ServiceLookup public void DoubleDisposeWorks() { var provider = new ServiceProvider(new ServiceCollection(), ServiceProviderOptions.Default); - var serviceProviderEngineScope = new ServiceProviderEngineScope(provider); + var serviceProviderEngineScope = new ServiceProviderEngineScope(provider, isRootScope: true); serviceProviderEngineScope.ResolvedServices.Add(new ServiceCacheKey(typeof(IFakeService), 0), null); serviceProviderEngineScope.Dispose(); serviceProviderEngineScope.Dispose();