⚡️ Speed up function build_list_reverse_order_index by 11%
#461
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.
📄 11% (0.11x) speedup for
build_list_reverse_order_indexinnvflare/tool/job/config/config_indexer.py⏱️ Runtime :
3.99 milliseconds→3.61 milliseconds(best of54runs)📝 Explanation and details
The optimized code achieves a 10% speedup through several key optimizations:
1. Early None Check Optimization
The most significant change moves the
if value is None:check before creating theKeyIndexobject. This avoids expensive object creation for None values, which explains the dramatic 264% speedup in thetest_list_with_all_nonetest case and 38.4% improvement intest_none_values_are_skipped.2. String Processing Optimization
Replaced the verbose dot-finding logic for path parsing with Python's built-in
rsplit(".", 1)[-1], eliminating multiple string operations (find(),rindex(), slicing). This optimization shows up strongly in thetest_path_key_sets_component_nametest (39.4% faster) andtest_path_key_with_no_dot(22.3% faster).3. List Length Check Optimization
Changed
len(value) > 0tovaluefor non-empty list checks, avoiding the overhead of calculating list length when a simple truthiness test suffices.4. Function Lookup Caching
Added local caching of the
has_none_primitives_in_listfunction lookup to avoid repeated global namespace lookups in the loop.5. Dictionary Access Optimization
In
add_to_indices, split thekey_indices.get(key, [])into explicit None checking to avoid creating empty lists unnecessarily and make the control flow more efficient.The optimizations are particularly effective for:
These optimizations maintain identical functionality while reducing computational overhead in the hot paths identified by the line profiler.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-build_list_reverse_order_index-mhe35wfpand push.