Skip to content

Fixes AB#3588022 Optimize load() and getIdTokensForAccountRecord() with filter-then-clone#3100

Open
siddhijain wants to merge 5 commits intodevfrom
siddhijain/perf-filter-then-clone-load-path
Open

Fixes AB#3588022 Optimize load() and getIdTokensForAccountRecord() with filter-then-clone#3100
siddhijain wants to merge 5 commits intodevfrom
siddhijain/perf-filter-then-clone-load-path

Conversation

@siddhijain
Copy link
Copy Markdown
Contributor

@siddhijain siddhijain commented Apr 28, 2026

Fixes AB#3588022 Apply filter-then-clone optimization to MsalOAuth2TokenCache.load() and getIdTokensForAccountRecord() methods behind the existing ENABLE_FILTER_THEN_CLONE_IN_MEMORY_CACHE flight.

When the flight is enabled, these methods now call the direct (non-input-list) getCredentialsFilteredBy overloads which filter on uncloned in-memory references first, then clone only the matching credentials. This avoids cloning all cached credentials on every AcquireTokenSilent cache-hit path.

Changes:

  • Add new getCredentialsFilteredBy overload with kid parameter to IAccountCredentialCache, SharedPreferencesAccountCredentialCache, and SharedPreferencesAccountCredentialCacheWithMemoryCache
  • Gate load() to skip preload pattern and use direct filtered lookups when flight is enabled
  • Gate getIdTokensForAccountRecord() with same optimization pattern
  • Add tests for new kid overload (flight enabled/disabled)
  • Add MsalOAuth2TokenCache-level tests for flight-gated load() and getIdTokensForAccountRecord() paths

Apply filter-then-clone optimization to MsalOAuth2TokenCache.load() and
getIdTokensForAccountRecord() methods behind the existing
ENABLE_FILTER_THEN_CLONE_IN_MEMORY_CACHE flight.

When the flight is enabled, these methods now call the direct (non-input-list)
getCredentialsFilteredBy overloads which filter on uncloned in-memory
references first, then clone only the matching credentials. This avoids
cloning all cached credentials on every AcquireTokenSilent cache-hit path.

Changes:
- Add new getCredentialsFilteredBy overload with kid parameter to
  IAccountCredentialCache, SharedPreferencesAccountCredentialCache, and
  SharedPreferencesAccountCredentialCacheWithMemoryCache
- Gate load() to skip preload pattern and use direct filtered lookups
  when flight is enabled
- Gate getIdTokensForAccountRecord() with same optimization pattern
- Add tests for new kid overload (flight enabled/disabled)
- Add MsalOAuth2TokenCache-level tests for flight-gated load() and
  getIdTokensForAccountRecord() paths

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 28, 2026 02:12
@siddhijain siddhijain requested review from a team as code owners April 28, 2026 02:12
@github-actions
Copy link
Copy Markdown

❌ Work item link check failed. Description does not contain AB#{ID}.

Click here to Learn more.

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

This PR extends the existing “filter-then-clone” optimization (behind ENABLE_FILTER_THEN_CLONE_IN_MEMORY_CACHE) to MsalOAuth2TokenCache.load() and getIdTokensForAccountRecord() by using new direct getCredentialsFilteredBy(...) overloads (including a kid parameter) so only matching in-memory credentials are cloned on cache-hit paths.

Changes:

  • Added a new IAccountCredentialCache.getCredentialsFilteredBy(..., requestedClaims, kid) overload and implemented it in both SharedPreferencesAccountCredentialCache and SharedPreferencesAccountCredentialCacheWithMemoryCache.
  • Updated MsalOAuth2TokenCache.load() and getIdTokensForAccountRecord() to use direct filtered lookups when the flight is enabled, retaining the legacy “preload all then filter” path when disabled.
  • Added/extended tests for flight-gated behavior and the new overload; updated changelog.txt.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
common4j/src/main/com/microsoft/identity/common/java/cache/SharedPreferencesAccountCredentialCacheWithMemoryCache.java Adds direct kid-aware filtering with filter-then-clone behavior when flight is enabled.
common4j/src/main/com/microsoft/identity/common/java/cache/SharedPreferencesAccountCredentialCache.java Adds direct kid-aware filtering overload (non-memory implementation).
common4j/src/main/com/microsoft/identity/common/java/cache/MsalOAuth2TokenCache.java Adds flight-gated use of direct filtered lookups in load() and getIdTokensForAccountRecord().
common4j/src/main/com/microsoft/identity/common/java/cache/IAccountCredentialCache.java Extends cache interface with a new direct kid overload.
common/src/test/java/com/microsoft/identity/common/SharedPreferencesAccountCredentialCacheWithMemoryCacheTest.java Adds tests for the new overload (flight enabled/disabled).
common/src/test/java/com/microsoft/identity/common/MsalOAuth2TokenCacheTest.java Adds flight-gated tests for load() and getIdTokensForAccountRecord() using memory cache implementation.
changelog.txt Adds a vNext PATCH entry describing the optimization.

Comment thread changelog.txt Outdated
@@ -1,5 +1,6 @@
vNext
----------
- [PATCH] Extend filter-then-clone optimization to load() and getIdTokensForAccountRecord() in MsalOAuth2TokenCache: when ENABLE_FILTER_THEN_CLONE_IN_MEMORY_CACHE flight is enabled, skip clone-all preload and call direct flight-gated overloads that clone only matching credentials; add new getCredentialsFilteredBy overload with kid support (#TBD)
Copy link

Copilot AI Apr 28, 2026

Choose a reason for hiding this comment

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

Changelog entry still contains the placeholder (#TBD). Impact: release notes become ambiguous and the repo’s changelog convention (linking to an issue/PR number) isn’t met. Recommendation: replace #TBD with the actual PR/issue reference before merging.

Suggested change
- [PATCH] Extend filter-then-clone optimization to load() and getIdTokensForAccountRecord() in MsalOAuth2TokenCache: when ENABLE_FILTER_THEN_CLONE_IN_MEMORY_CACHE flight is enabled, skip clone-all preload and call direct flight-gated overloads that clone only matching credentials; add new getCredentialsFilteredBy overload with kid support (#TBD)
- [PATCH] Extend filter-then-clone optimization to load() and getIdTokensForAccountRecord() in MsalOAuth2TokenCache: when ENABLE_FILTER_THEN_CLONE_IN_MEMORY_CACHE flight is enabled, skip clone-all preload and call direct flight-gated overloads that clone only matching credentials; add new getCredentialsFilteredBy overload with kid support (#<ACTUAL_PR_OR_ISSUE_NUMBER>)

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@copilot help me take care of this comment by updating the change log with the right PR number 3100.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Done in cab62bc. Replaced #TBD with #3100 in the changelog entry.

@siddhijain siddhijain changed the title Optimize load() and getIdTokensForAccountRecord() with filter-then-clone Fixes AB#3588022 Optimize load() and getIdTokensForAccountRecord() with filter-then-clone Apr 28, 2026
@github-actions
Copy link
Copy Markdown

✅ Work item link check complete. Description contains link AB#3588022 to an Azure Boards work item.

…WithKid tests

- Set kid="kid1" on AccessTokenRecord in both test methods
- Add assertions for matching kid ("kid1" → returns 1 result)
- Add assertions for non-matching kid ("kid2" → returns 0 results)
- Keep null kid assertions (null → returns all, no kid filter applied)
- Cover both flight-disabled and flight-enabled variants

Agent-Logs-Url: https://github.com/AzureAD/microsoft-authentication-library-common-for-android/sessions/54f98b54-83f3-4bca-92d7-e1544068086f

Co-authored-by: siddhijain <30181294+siddhijain@users.noreply.github.com>
…kid) overload

The caller (MsalOAuth2TokenCache.load()) already gates entry to this
overload via the ENABLE_FILTER_THEN_CLONE_IN_MEMORY_CACHE flight.
The inner check was redundant. Now always uses the optimized
filter-then-clone path.

Note: The 9-param overload retains its flight check since it has
callers (BrokerOAuth2TokenCache, MsalCppOAuth2TokenCache) that
don't check the flight at their level.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.

3 participants