⚡️ Speed up function _parse_latex_header_span by 37%
#389
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.
📄 37% (0.37x) speedup for
_parse_latex_header_spaninpandas/io/formats/style_render.py⏱️ Runtime :
913 microseconds→668 microseconds(best of139runs)📝 Explanation and details
The optimized code achieves a 36% speedup through several targeted performance improvements in the LaTeX style processing functions:
Key optimizations in
_parse_latex_cell_styles:formatterdictionary on every loop iteration, even when most entries were never used. The optimized version only generates the specific format string needed for the detected wrap argument.optionsto string once per iteration instead of repeatedly callingstr(options)in the inner loop.reversed()instead of slicing: Replacedlatex_styles[::-1]withreversed(latex_styles)to avoid creating a full reversed copy of the list.Key optimizations in
_parse_latex_header_span:find()twice for the same substring (once for detection, once for extraction), the code now caches the index and uses direct slicing withfind('"', start)for the end position..get()for safer attribute access: Replaced"attributes" in cellcheck withcell.get("attributes")to handle missing keys more efficiently.Performance impact by test category:
test_large_number_of_styles) due to eliminated dictionary creation overheadThe optimizations particularly excel in scenarios with multiple CSS styles or frequent LaTeX processing, making them valuable for pandas DataFrame styling operations that process many cells with complex formatting.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
io/formats/style/test_to_latex.py::test_parse_latex_header_span🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_parse_latex_header_span-mio8qx6band push.