⚡️ Speed up method RoboflowModelRegistry.get_model by 251%
#637
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.
📄 251% (2.51x) speedup for
RoboflowModelRegistry.get_modelininference/core/registries/roboflow.py⏱️ Runtime :
34.7 microseconds→9.87 microseconds(best of29runs)📝 Explanation and details
The key optimization is the addition of a module-level cache (
_MODEL_TYPE_CACHE) that memoizes the expensiveget_model_type()calls in theget_modelmethod.What changed:
_MODEL_TYPE_CACHE = {}at module levelget_model()to check cache first using key(api_key, model_id, countinference, service_secret)get_model_type()on cache miss, then stores result for future lookupsWhy this provides 251% speedup:
The
get_model_type()function involves expensive operations like:get_roboflow_model_data,get_roboflow_instant_model_data)By caching the final result tuple
(TaskType, ModelType)at the registry level, subsequent calls with identical parameters bypass all this expensive computation entirely - just a fast dictionary lookup.Best for test cases with:
The cache is scoped to the process lifetime and uses a composite key to ensure correctness across different API keys and model configurations. This is a classic time-space tradeoff that dramatically improves performance for repeated model registry lookups.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
inference/unit_tests/core/registries/test_roboflow.py::test_roboflow_model_registry_get_model_on_cache_htinference/unit_tests/core/registries/test_roboflow.py::test_roboflow_model_registry_get_model_on_cache_miss🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-RoboflowModelRegistry.get_model-mhbytxbdand push.