AzureAD / microsoft-identity-web

Helps creating protected web apps and web APIs with Microsoft identity platform and Azure AD B2C
MIT License
683 stars 215 forks source link

Reducing memory allocations in DefaultCredentialsLoader #2809

Open MarkCiliaVincenti opened 6 months ago

MarkCiliaVincenti commented 6 months ago

DefaultCredentialsLoader currently uses a ConcurrentDictionary which will permanently store a string and a SemaphoreSlim for every credential loaded, as it does not clean up after itself.

I am the author of AsyncKeyedLock, a popular library for key locking that has a number of dependents, including two existing Microsoft projects (Vipr and kiota).

I believe that this dependency would help reduce memory allocations through SemaphoreSlim pooling as well as clean up the dictionary after use.

        private readonly AsyncKeyedLocker<string> _loadingSemaphores = new(o =>
        {
            o.PoolSize = 20;
            o.PoolInitialFill = 1;
        });
                // Get or create a semaphore for this credentialDescription and wait on it
                using (await _loadingSemaphores.LockAsync(credentialDescription.Id))
                {
                    if (credentialDescription.CachedValue == null)
                    {
                        if (CredentialSourceLoaders.TryGetValue(credentialDescription.SourceType, out ICredentialSourceLoader? loader))
                            await loader.LoadIfNeededAsync(credentialDescription, parameters);
                    }
                };
jennyf19 commented 5 months ago

Thanks @MarkCiliaVincenti . Unfortunately, we are not able to take a dependency on a 3p library.