⚡️ Speed up function contains_tex_string by 88%#154
Open
codeflash-ai[bot] wants to merge 1 commit into
Open
Conversation
The optimization moves regex pattern compilation from inside the function to module-level scope, eliminating redundant compilation overhead on every function call.
**Key Changes:**
- **Pattern pre-compilation**: The regex pattern `re.compile(f"{dollars}|{braces}|{parens}", flags=re.S)` is compiled once at module load time instead of on every function call
- **Reduced per-call overhead**: Each call now only performs a pattern search instead of string concatenation + regex compilation + search
**Performance Impact:**
The line profiler shows the original function spent 71.8% of its time (914.7μs out of 1273μs total) just compiling the regex pattern on each call. The optimized version eliminates this overhead entirely, reducing total runtime from 348μs to 185μs (87% speedup).
**Hot Path Considerations:**
Based on `function_references`, this function is called from `_model_requires_mathjax()` which checks multiple model properties (text annotations, slider titles, axis labels, div/paragraph content) for MathJax requirements during bundle generation. Since this function may be called repeatedly during model processing, the regex compilation overhead compounds significantly.
**Test Case Performance:**
The optimization shows consistent 100-300% speedups across all test cases, with particularly strong gains on:
- Simple strings without delimiters (200-300% faster)
- Large text processing (30-60% faster but still significant absolute time savings)
- Repeated delimiter detection scenarios (100-170% faster)
This optimization is especially beneficial for applications processing many text elements or large documents, where the cumulative regex compilation cost would be substantial.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
📄 88% (0.88x) speedup for
contains_tex_stringinsrc/bokeh/embed/util.py⏱️ Runtime :
348 microseconds→185 microseconds(best of231runs)📝 Explanation and details
The optimization moves regex pattern compilation from inside the function to module-level scope, eliminating redundant compilation overhead on every function call.
Key Changes:
re.compile(f"{dollars}|{braces}|{parens}", flags=re.S)is compiled once at module load time instead of on every function callPerformance Impact:
The line profiler shows the original function spent 71.8% of its time (914.7μs out of 1273μs total) just compiling the regex pattern on each call. The optimized version eliminates this overhead entirely, reducing total runtime from 348μs to 185μs (87% speedup).
Hot Path Considerations:
Based on
function_references, this function is called from_model_requires_mathjax()which checks multiple model properties (text annotations, slider titles, axis labels, div/paragraph content) for MathJax requirements during bundle generation. Since this function may be called repeatedly during model processing, the regex compilation overhead compounds significantly.Test Case Performance:
The optimization shows consistent 100-300% speedups across all test cases, with particularly strong gains on:
This optimization is especially beneficial for applications processing many text elements or large documents, where the cumulative regex compilation cost would be substantial.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
unit/bokeh/embed/test_util__embed.py::Test__tex_helpers.test_contains_tex_string🌀 Generated Regression Tests and Runtime
🔎 Concolic Coverage Tests and Runtime
codeflash_concolic_sstvtaha/tmpgpm146z7/test_concolic_coverage.py::test_contains_tex_stringTo edit these changes
git checkout codeflash/optimize-contains_tex_string-mhwtjmd8and push.