⚡️ Speed up function _get_level_lengths by 8%
#383
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.
📄 8% (0.08x) speedup for
_get_level_lengthsinpandas/io/formats/style_render.py⏱️ Runtime :
10.6 milliseconds→9.87 milliseconds(best of25runs)📝 Explanation and details
The optimization improves performance by replacing list membership checks with set membership checks for the
hidden_elementsparameter, which provides O(1) average-case lookup time instead of O(n) for lists.Key Optimizations:
Set-based membership testing: Converts
hidden_elementsfrom a list to a set at function entry, changingif j not in hidden_elementschecks from O(n) to O(1) operations. This is particularly impactful since these checks occur within nested loops that can iterate thousands of times.Safe dictionary access: Replaces direct dictionary key access
lengths[(i, last_label)]withlengths.get((i, last_label), None)to avoid potential KeyError exceptions and improve robustness.Variable initialization: Adds
last_label = Noneinitialization to ensure the variable is always defined before use in the inner loop.Performance Impact:
The optimization delivers a 7% speedup overall with the most significant gains appearing in large-scale test cases:
test_large_multiindex_with_hidden_and_trimming: 16.6% fastertest_large_index_with_all_hidden: 299% faster (most dramatic improvement)Hot Path Context:
Based on the function references,
_get_level_lengthsis called from_translate()- a core styling method that processes DataFrames for HTML rendering. This function runs in pandas' styling pipeline where large MultiIndex DataFrames are common, making the O(1) membership optimization particularly valuable for real-world performance.The optimization is most effective for cases with larger hidden element lists and higher iteration counts, which aligns well with typical pandas styling workloads involving complex hierarchical indexes.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
io/formats/style/test_style.py::TestStyler.test_get_level_lengthsio/formats/style/test_style.py::TestStyler.test_get_level_lengths_un_sortedio/formats/style/test_style.py::test_get_level_lengths_mi_hidden🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_get_level_lengths-mio5nuydand push.