⚡️ Speed up function build_dict_reverse_order_index by 13%
#464
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
build_dict_reverse_order_indexinnvflare/tool/job/config/config_indexer.py⏱️ Runtime :
1.34 milliseconds→1.19 milliseconds(best of44runs)📝 Explanation and details
The optimized code achieves a 12% speedup through several key micro-optimizations that reduce function call overhead and improve conditional checks:
What optimizations were applied:
Function lookup caching: Added local variable assignments (
add_indices = add_to_indices,is_prim = is_primitive,has_non_prim = has_none_primitives_in_list) to avoid repeated global namespace lookups during loops.Improved list emptiness check: Replaced
len(value) > 0withif value:, which is faster as it doesn't require calculating the length.Streamlined isinstance checks: Consolidated
isinstance(value, int) or isinstance(value, float) or isinstance(value, str) or isinstance(value, bool)into a singleisinstance(value, (int, float, str, bool))call.Optimized dictionary access: Replaced
key_indices.get(key, [])followed by assignment withkey_indices.setdefault(key, [])inadd_to_indicesto reduce dictionary lookups.Safer string operations: Added type checking before calling
.find()method on strings to prevent potential errors.Why these optimizations provide speedup:
orconditions as they require only one function callif valuevslen(value) > 0avoids the overhead of length calculation for non-empty containerssetdefaultperforms dictionary lookup once instead of twice (get + assignment)Test case performance patterns:
The optimizations show consistent 8-15% improvements across all test cases, with larger gains on:
The optimizations are particularly effective for workloads with many primitive type checks and dictionary operations, which are common in configuration indexing scenarios.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
unit_test/tool/job/config/config_indexer_test.py::TestConfigIndex.test_dict_indexerunit_test/tool/job/config/config_indexer_test.py::TestConfigIndex.test_extract_file_from_dict_by_index🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-build_dict_reverse_order_index-mhe3slbxand push.