AzureAD / microsoft-identity-web

Helps creating protected web apps and web APIs with Microsoft identity platform and Azure AD B2C

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Reducing memory allocations in DefaultCredentialsLoader

MarkCiliaVincenti opened this issue · comments

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);
                    }
                };

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