⚡️ Speed up function _get_store_registry by 575%
#175
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.
📄 575% (5.75x) speedup for
_get_store_registryinmlflow/tracking/_model_registry/utils.py⏱️ Runtime :
93.4 microseconds→13.8 microseconds(best of5runs)📝 Explanation and details
The optimized code achieves a 575% speedup through several key micro-optimizations that reduce Python interpreter overhead:
Key Optimizations:
Moved expensive imports to module level - The Unity Catalog store imports (
UcModelRegistryStore,UnityCatalogOssStore) are now at the top instead of being imported every time_get_store_registry()is called. This eliminates repeated import overhead.Reduced attribute lookups in loops - Created local variable
reg = registry.registerto avoid repeated method lookups during the scheme registration loops. Also used_register = self.registerinregister_entrypoints().Optimized global variable access - Used local variable
registry = _model_registry_store_registryto minimize global name lookups within the function.String formatting optimization - In
register_entrypoints(), replaced.format()with f-string for warning message construction, which is faster in modern Python.Changed list to tuple literals - Used tuples
("http", "https")and("", "file")instead of lists, as tuples have slightly less overhead for small, immutable collections.Performance Impact:
The line profiler shows the main bottleneck is still the
register_entrypoints()call (~333ms), but the optimizations reduce the registry setup overhead significantly. Since_get_store_registry()is called from_get_store()which is likely in hot paths for MLflow tracking operations, this 575% speedup for registry access will have meaningful impact on model tracking workflows.Test Results:
The annotated tests show consistent 5-7x speedups across all registry access patterns (503%-738% improvements), indicating the optimizations are effective for both singleton access patterns and fresh registry creation scenarios.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_get_store_registry-mhx67ghuand push.