⚡️ Speed up function _parse_latex_css_conversion by 40%
#390
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.
📄 40% (0.40x) speedup for
_parse_latex_css_conversioninpandas/io/formats/style_render.py⏱️ Runtime :
13.2 milliseconds→9.43 milliseconds(best of46runs)📝 Explanation and details
The optimization achieves a 40% speedup by targeting the most expensive operations in CSS-to-LaTeX conversion, particularly for RGB color processing and loop inefficiencies.
Key Optimizations Applied
1. Pre-compiled Regex for RGB Parsing
The original code used multiple
re.findall()calls with complex regex patterns for each RGB color. The optimized version compiles the regex once (RGB_RE) and uses a singlefindall()call to extract all RGB values at once, eliminating repeated regex compilation overhead.2. Streamlined RGB Channel Processing
Instead of separate regex calls and repeated
int/floatbranching for each RGB channel, the optimization extracts all values first, then processes them through a unifiedchannel()function. This reduces the computational complexity from O(3n) regex operations to O(n) per RGB color.3. Optimized Loop Structure
The original nested type checking (
isinstance(value, str) and "--latex" in value) was restructured to checkisinstance(value, str)once, then branch into string-specific logic. This avoids redundant type checks and string operations for non-string values.4. Method Reference Caching
latex_styles.appendis cached as a local variable to avoid attribute lookup overhead in tight loops, providing faster list mutations.5. Eliminated Unnecessary Operations
str()calls when the type is already known to be stringextend([single_item])to directappend(single_item)callsPerformance Impact by Test Case
The optimization particularly excels with RGB/RGBA color processing (8-49% faster) and large-scale operations (25-49% faster), which aligns with the function being called from
_parse_latex_cell_styleswhere it processes multiple CSS styles per cell in pandas styling operations. The regex optimization significantly benefits workloads with many RGB colors, while the streamlined control flow helps all test cases avoid unnecessary work.Simple conversions show minor overhead due to regex compilation, but this is overwhelmed by gains in realistic workloads with mixed or repeated color processing.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
io/formats/style/test_to_latex.py::test_parse_latex_css_conversionio/formats/style/test_to_latex.py::test_parse_latex_css_conversion_option🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_parse_latex_css_conversion-mio99pk4and push.