From c860581c4eaa0eee1476d0a23c001b1665a8a5db Mon Sep 17 00:00:00 2001 From: Ignacio Inglese Date: Wed, 29 Jan 2025 21:29:41 +0000 Subject: [PATCH] Use ConcurrentDictionary's GetOrAdd with delegate instead. --- .../Validation/Results/Details/ValidationError.cs | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/Microsoft.IdentityModel.Tokens/Validation/Results/Details/ValidationError.cs b/src/Microsoft.IdentityModel.Tokens/Validation/Results/Details/ValidationError.cs index 5176d4a505..7b74662e98 100644 --- a/src/Microsoft.IdentityModel.Tokens/Validation/Results/Details/ValidationError.cs +++ b/src/Microsoft.IdentityModel.Tokens/Validation/Results/Details/ValidationError.cs @@ -288,16 +288,9 @@ public static StackFrame GetCurrentStackFrame( // String is allocated, but it goes out of scope immediately after the call string key = filePath + lineNumber; - // Try to get the stack frame from the cache, - // otherwise create a new one and add it to the cache - if (!CachedStackFrames.TryGetValue(key, out StackFrame? frame)) - { - frame = new StackFrame(skipFrames, true); - CachedStackFrames.TryAdd(key, frame); - } - - // By this point the frame cannot be null, but the compiler doesn't know that - return frame!; + return CachedStackFrames.GetOrAdd( + key, + _ => new StackFrame(skipFrames, true)); } // ConcurrentDictionary is thread-safe and only locks when adding a new item.