⚡️ Speed up method OidcCliPlugin.write_tokens_to_metadata by 44%
#42
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
📄 44% (0.44x) speedup for
OidcCliPlugin.write_tokens_to_metadatainframework/py/flwr/cli/auth_plugin/oidc_cli_plugin.py⏱️ Runtime :
46.0 microseconds→32.0 microseconds(best of160runs)📝 Explanation and details
The optimization adds a type-aware concatenation strategy that avoids unnecessary list conversion when the input
metadatais already a list.Key optimization:
if isinstance(metadata, list)to detect when metadata is already a listmetadata + [tokens]instead oflist(metadata) + [tokens]list(metadata) + [tokens]approachWhy this is faster:
list(metadata)creates an unnecessary copy when metadata is already a list+) is more efficient than convert-then-concatenatePerformance characteristics:
The optimization maintains identical behavior while providing substantial performance gains for the common case of list-based metadata.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-OidcCliPlugin.write_tokens_to_metadata-mhcxyv43and push.