Skip to content

Commit

Permalink
Use ConcurrentDictionary's GetOrAdd with delegate instead.
Browse files Browse the repository at this point in the history
  • Loading branch information
iNinja committed Jan 29, 2025
1 parent c44ba6c commit c860581
Showing 1 changed file with 3 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit c860581

Please sign in to comment.