AzureAD / microsoft-authentication-library-for-python

Microsoft Authentication Library (MSAL) for Python makes it easy to authenticate to Microsoft Entra ID. General docs are available here https://learn.microsoft.com/entra/msal/python/ Stable APIs are documented here https://msal-python.readthedocs.io. Questions can be asked on www.stackoverflow.com with tag "msal" + "python".
https://stackoverflow.com/questions/tagged/azure-ad-msal+python
Other
795 stars 200 forks source link

Order scopes on save, and optimize the happy path for access token read #644

Closed rayluo closed 8 months ago

rayluo commented 9 months ago

This PR currently contains 2 commits.

  1. The first commit orders scopes on save, which is similar to https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/pull/4479. Existing unit tests need adjustment to match the changed scope order.
  2. The second commit optimizes the happy path of access token read, which was the idea mentioned in this conversation. Basically, when searching for an access token, this PR uses the token key to attempt an O(1) search, and falls back to O(n) search when necessary.
    • No other unit test adjustment is needed, meaning this change is backward compatible.
    • Existing benchmark test shows ~40% time reduction (or 1.6x speed improvement) on "token cache hit" code path, which is also visible in diagram No. 2 and No. 4 in this benchmark page. (Note that the performance gain can be much higher than 1.6x if each tenant has more tokens; it is actually an O(1) vs O(n) improvement.)
bgavrilMS commented 9 months ago

Nice perf result. CC @pmaytak as the same strategy could be done in .NET

rayluo commented 4 months ago

If you can take initiative to resolve that will be highly appreciated.

@Moazzem-Hossain-pixel , you are commenting on a topic that has already been implemented. There are some follow-ups in their own github issues linked above, and we are addressing them respectively. But the main change in the current topic is already stable. And our downstream partner - Azure CLI - has been unblocked by https://github.com/AzureAD/microsoft-authentication-extensions-for-python/issues/127

If you are blocked by any specific issue, please add your comment into those linked issues, or even create a new issue to elaborate. Meanwhile, I'm locking this issue here.