⚡️ Speed up function _parse_latex_cell_styles by 200%
#388
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.
📄 200% (2.00x) speedup for
_parse_latex_cell_stylesinpandas/io/formats/style_render.py⏱️ Runtime :
17.3 milliseconds→5.78 milliseconds(best of100runs)📝 Explanation and details
The optimization achieves a 199% speedup by eliminating redundant operations and reducing object allocations in the core processing loop of
_parse_latex_cell_styles.Key optimizations:
Eliminated formatter dictionary creation: The original code created a 5-element dictionary on every iteration (9,095 times), causing significant overhead. The optimized version uses direct conditional branches instead, reducing allocation and lookup costs.
Reduced string conversions:
str(options)was called multiple times per iteration. Now it's computed once and stored asoptions_str, eliminating redundant conversions.Precompiled regex patterns: Moved regex compilation outside the
colorfunction to avoid recompiling patterns on each CSS color conversion, improving RGB/RGBA parsing performance.Optimized wrap argument handling: Extracted
_WRAP_ARGSas a module-level constant and converted to tuple for membership tests, reducing list recreation overhead.Simplified list operations: Replaced
.extend([single_item])with.append(single_item)to avoid unnecessary list wrapping.Performance impact by test type:
The function is called from
_parse_latex_header_spanfor table cell formatting in pandas styling, making this optimization beneficial for LaTeX export performance, especially with complex styled DataFrames containing many cells.✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
io/formats/style/test_to_latex.py::test_parse_latex_cell_styles_basicio/formats/style/test_to_latex.py::test_parse_latex_cell_styles_braces🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_parse_latex_cell_styles-mio8bna7and push.