⚡️ Speed up function _math_mode_with_dollar by 40%
#391
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
_math_mode_with_dollarinpandas/io/formats/style_render.py⏱️ Runtime :
970 microseconds→693 microseconds(best of83runs)📝 Explanation and details
The optimization achieves a 39% speedup by eliminating the repeated compilation of a regular expression and streamlining the string processing algorithm.
Key optimizations:
Pre-compiled regex pattern: The original code compiled
re.compile(r"\$.*?\$")on every function call (245μs overhead per call). The optimized version moves this to a module-level constant_DOLLAR_PATTERN, eliminating this repeated compilation cost.Single-pass pattern matching: Instead of repeatedly calling
pattern.search()in a while loop, the optimized code useslist(_DOLLAR_PATTERN.finditer(s))to find all matches upfront, then processes them in a simple for loop. This reduces the total regex search operations and improves cache locality.Reduced function call overhead: The original algorithm called
ps.span()twice per match andpattern.search()for each iteration. The optimized version pre-calculates spans withstart, end = m.span()and eliminates the repeated search calls.Performance impact analysis:
Workload impact:
Based on the function reference,
_math_mode_with_dollaris called by_escape_latex_math, which appears to be part of pandas' LaTeX rendering pipeline. This optimization will particularly benefit:The optimization maintains identical behavior while providing substantial performance gains, especially for math-heavy content.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_math_mode_with_dollar-mio9nky6and push.