⚡️ Speed up function get_mappings by 1,365% - #8
Open
codeflash-ai[bot] wants to merge 1 commit into
Open
Conversation
The optimization moves the large 1000-entry ImageNet dictionary from being recreated on every function call to being defined once as a module-level constant `_IMAGENET_WORDNETID_TO_CLASSIDX`. The `get_mappings()` function now simply returns `dict(_IMAGENET_WORDNETID_TO_CLASSIDX)` instead of constructing the entire dictionary from scratch each time. **Key performance improvements:** - **Eliminates dictionary construction overhead**: The original code created 1000 key-value pairs on every call, while the optimized version only copies an existing dictionary - **Reduces memory allocation**: Instead of allocating memory for 1000 string/int pairs repeatedly, it performs one efficient dictionary copy operation - **Maintains behavioral contract**: Still returns a fresh dictionary that callers can safely mutate without affecting future calls **Why this optimization is effective:** The line profiler shows the original code spent 98.3% of its time (555ms out of 565ms total) on the dictionary literal construction. Dictionary copying in Python is highly optimized at the C level, making it orders of magnitude faster than reconstructing the same static data repeatedly. **Test case performance pattern:** All test cases show consistent 13-14x speedups (1200-1400% faster), indicating this optimization benefits any usage pattern - whether calling the function once or thousands of times. The speedup is particularly valuable for ML/computer vision workflows where ImageNet mappings might be accessed frequently during data preprocessing or model inference.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
📄 1,365% (13.65x) speedup for
get_mappingsinsrc/spdl/source/imagenet.py⏱️ Runtime :
2.51 milliseconds→171 microseconds(best of450runs)📝 Explanation and details
The optimization moves the large 1000-entry ImageNet dictionary from being recreated on every function call to being defined once as a module-level constant
_IMAGENET_WORDNETID_TO_CLASSIDX. Theget_mappings()function now simply returnsdict(_IMAGENET_WORDNETID_TO_CLASSIDX)instead of constructing the entire dictionary from scratch each time.Key performance improvements:
Why this optimization is effective:
The line profiler shows the original code spent 98.3% of its time (555ms out of 565ms total) on the dictionary literal construction. Dictionary copying in Python is highly optimized at the C level, making it orders of magnitude faster than reconstructing the same static data repeatedly.
Test case performance pattern:
All test cases show consistent 13-14x speedups (1200-1400% faster), indicating this optimization benefits any usage pattern - whether calling the function once or thousands of times. The speedup is particularly valuable for ML/computer vision workflows where ImageNet mappings might be accessed frequently during data preprocessing or model inference.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
🔎 Concolic Coverage Tests and Runtime
codeflash_concolic_xpyvdxks/tmpxl_hvfrs/test_concolic_coverage.py::test_get_mappingsTo edit these changes
git checkout codeflash/optimize-get_mappings-mgqq2tdnand push.