⚡️ Speed up method RetryingClient.version_supported by 37%
#81
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.
📄 37% (0.37x) speedup for
RetryingClient.version_supportedinwandb/apis/public/api.py⏱️ Runtime :
64.9 milliseconds→47.3 milliseconds(best of32runs)📝 Explanation and details
The optimization implements caching for the parsed max_cli_version to avoid redundant parsing operations.
Key changes:
max_cli_versioninself._parsed_max_cli_versionalong with tracking the source string inself._parsed_max_cli_version_srcmax_cli_versionwhen it changes (checked via string comparison)Why it's faster:
The
packaging.version.parse()function is computationally expensive, involving string parsing and normalization. The line profiler shows that parsing operations dominated the original runtime (94% of time spent on the return line with twoparse()calls). By caching the parsed max version, we eliminate repeated parsing of the same version string.Performance benefits by test case:
RetryingClientinstance is used multiple times (e.g.,test_many_versions_supportedshows 70% improvement)The optimization is particularly effective for scenarios where the same client instance performs multiple version checks, which is the typical usage pattern for this API client.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-RetryingClient.version_supported-mhe1palnand push.