Skip to content

Fix eager evaluation in ConcurrentDictionary.GetOrAdd calls and add r…#5950

Merged
bgavrilMS merged 3 commits intomainfrom
bogavril/fix-getorAdd-eager-evaluation
Apr 30, 2026
Merged

Fix eager evaluation in ConcurrentDictionary.GetOrAdd calls and add r…#5950
bgavrilMS merged 3 commits intomainfrom
bogavril/fix-getorAdd-eager-evaluation

Conversation

@bgavrilMS
Copy link
Copy Markdown
Member

…eview rule

GetOrAdd(key, value) eagerly evaluates the second argument before checking the dictionary. When the value is a constructor call, a new object is created and immediately discarded on every cache hit.

Fixed in InMemoryPartitionedUserTokenCacheAccessor: unnecessary ConcurrentDictionary allocated on every SaveRefreshToken, SaveIdToken, and SaveAccount call.

Also added a code review rule to copilot-instructions.md to prevent this pattern from being reintroduced.

Fixes #

Changes proposed in this request

Testing

Performance impact

Documentation

  • All relevant documentation is updated.

Copilot AI review requested due to automatic review settings April 24, 2026 15:51
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Improves performance by preventing unnecessary object allocation in ConcurrentDictionary.GetOrAdd calls and adds a repo-wide Copilot review rule to avoid reintroducing eager GetOrAdd(key, value) patterns.

Changes:

  • Switched GetOrAdd(partitionKey, value) to GetOrAdd(partitionKey, valueFactory) in the in-memory user token cache to avoid eager allocations on cache hits.
  • Added a Copilot code review rule documenting the ConcurrentDictionary.GetOrAdd eager-evaluation pitfall and the preferred factory-delegate pattern.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/client/Microsoft.Identity.Client/PlatformsCommon/Shared/InMemoryPartitionedUserTokenCacheAccessor.cs Uses the GetOrAdd factory overload to avoid allocating inner dictionaries on every cache hit when saving refresh tokens, ID tokens, and accounts.
.github/copilot-instructions.md Adds a review rule explaining why GetOrAdd(key, valueFactory) should be preferred to prevent eager allocations.

Comment thread .github/copilot-instructions.md Outdated
@bgavrilMS bgavrilMS marked this pull request as ready for review April 24, 2026 18:02
@bgavrilMS bgavrilMS requested a review from a team as a code owner April 24, 2026 18:02
Comment thread .github/copilot-instructions.md Outdated
Copilot AI review requested due to automatic review settings April 24, 2026 18:03
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Comment thread .github/copilot-instructions.md Outdated
Copilot AI review requested due to automatic review settings April 30, 2026 16:54
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

bgavrilMS and others added 3 commits April 30, 2026 18:40
…eview rule

GetOrAdd(key, value) eagerly evaluates the second argument before
checking the dictionary. When the value is a constructor call, a new
object is created and immediately discarded on every cache hit.

Fixed in InMemoryPartitionedUserTokenCacheAccessor: unnecessary
ConcurrentDictionary allocated on every SaveRefreshToken,
SaveIdToken, and SaveAccount call.

Also added a code review rule to copilot-instructions.md to prevent
this pattern from being reintroduced.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@bgavrilMS bgavrilMS force-pushed the bogavril/fix-getorAdd-eager-evaluation branch from 3a7ff6a to 939502b Compare April 30, 2026 17:40
@bgavrilMS bgavrilMS merged commit 8dc9f18 into main Apr 30, 2026
15 checks passed
@bgavrilMS bgavrilMS deleted the bogavril/fix-getorAdd-eager-evaluation branch April 30, 2026 21:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants