⚡️ Speed up function _replace_keys by 13%#34
Open
codeflash-ai[bot] wants to merge 1 commit intomainfrom
Open
Conversation
The optimization achieves a 13% speedup by reducing unnecessary function calls and improving type checking efficiency: **Key optimizations:** 1. **Reduced recursive calls**: The optimized version uses inline type checking (`isinstance(v, (dict, list))`) within the comprehensions to avoid calling `_replace_keys` on primitive values (strings, numbers, etc.). This eliminates thousands of unnecessary function calls for leaf nodes. 2. **Early exit for primitives**: Instead of always recursively calling `_replace_keys` and then checking types inside the function, the optimized version checks types first and only recurses when needed. This is particularly effective for structures with many primitive values. 3. **Improved control flow**: Changed the second `if` to `elif` to avoid checking both dict and list conditions for the same object. 4. **Dictionary items caching**: Stores `d.items()` in a local variable to avoid repeated method lookups during iteration. **Performance impact by test type:** - **Large flat dictionaries**: Most beneficial for structures with many primitive values where recursive calls are avoided - **Mixed nested structures**: Effective when there are many leaf nodes (non-dict, non-list values) - **Deep nesting**: Still provides benefits by reducing function call overhead at every level The line profiler shows the optimization reduces total execution time from 27.8ms to 24.2ms, with the most significant improvement coming from avoiding redundant `isinstance` checks and recursive calls for primitive values.
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.
📄 13% (0.13x) speedup for
_replace_keysinframework/py/flwr/simulation/run_simulation.py⏱️ Runtime :
4.62 milliseconds→4.09 milliseconds(best of68runs)📝 Explanation and details
The optimization achieves a 13% speedup by reducing unnecessary function calls and improving type checking efficiency:
Key optimizations:
Reduced recursive calls: The optimized version uses inline type checking (
isinstance(v, (dict, list))) within the comprehensions to avoid calling_replace_keyson primitive values (strings, numbers, etc.). This eliminates thousands of unnecessary function calls for leaf nodes.Early exit for primitives: Instead of always recursively calling
_replace_keysand then checking types inside the function, the optimized version checks types first and only recurses when needed. This is particularly effective for structures with many primitive values.Improved control flow: Changed the second
iftoelifto avoid checking both dict and list conditions for the same object.Dictionary items caching: Stores
d.items()in a local variable to avoid repeated method lookups during iteration.Performance impact by test type:
The line profiler shows the optimization reduces total execution time from 27.8ms to 24.2ms, with the most significant improvement coming from avoiding redundant
isinstancechecks and recursive calls for primitive values.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_replace_keys-mh9yps0jand push.