⚡️ Speed up function _get_experiment_schema_version by 26%
#44
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.
📄 26% (0.26x) speedup for
_get_experiment_schema_versioningoogle/cloud/aiplatform/metadata/metadata.py⏱️ Runtime :
11.9 microseconds→9.44 microseconds(best of175runs)📝 Explanation and details
The optimization introduces function-level caching to eliminate repeated dictionary lookups. The original code performs a dictionary lookup (
constants.SCHEMA_VERSIONS[constants.SYSTEM_EXPERIMENT]) on every function call, while the optimized version caches the result after the first lookup.Key changes:
hasattr()to check if the result is already cached on the function object_get_experiment_schema_version._cachedafter the first callWhy this is faster:
hasattr()check and attribute access (._cached) are faster operations than dictionary lookupsPerformance characteristics from tests:
This optimization assumes
constants.SYSTEM_EXPERIMENTandconstants.SCHEMA_VERSIONSare truly constant at runtime, which is appropriate for configuration values in Google Cloud AI Platform.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_get_experiment_schema_version-mglgofvxand push.