⚡️ Speed up method Parser.dfrac by 6%
#263
Open
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.
📄 6% (0.06x) speedup for
Parser.dfracinlib/matplotlib/_mathtext.py⏱️ Runtime :
702 microseconds→665 microseconds(best of12runs)📝 Explanation and details
The optimized code achieves a 5% speedup by reducing redundant string processing during parser initialization, which is particularly beneficial since
Parser.__init__()creates complex pyparsing grammar objects.Key optimizations applied:
Pre-computed regex string escaping and joining: The original code repeatedly called
re.escape()and"|".join()on the same collections (_delims,_fontnames,_accent_map,_function_names) within thecsnames()function. The optimized version pre-computes these joined strings once as instance attributes (_delims_joined,_fontnames_joined, etc.) and reuses them, eliminating redundant string operations.Cached regex pattern compilation: Instead of repeatedly constructing identical regex strings for
symbol,unknown_symbol, andnon_mathpatterns, the optimized version stores these patterns in variables and reuses them, reducing string formatting overhead.Optimized single-character alternatives: Replaced
oneOf(["_", "^"])withLiteral("_") | Literal("^")in thesubsuperdefinition. This avoids the overhead ofoneOf()processing a list when dealing with simple single-character alternatives.Cached style literals: Instead of recomputing
[str(e.value) for e in self._MathStyle]every time, it's computed once and stored in a variable.Performance impact: The test results show consistent improvements across large-scale scenarios (6.19% faster for 1000 calls, 5.40% faster for varied types), indicating the optimizations are most effective when the parser is instantiated multiple times or processes many expressions. The optimizations target initialization overhead rather than parsing runtime, making them valuable for applications that create multiple
Parserinstances or process mathematical expressions frequently.Workload benefits: These optimizations are particularly beneficial for applications that frequently instantiate parsers or process large volumes of mathematical text, such as document rendering systems, mathematical notation processors, or scientific computing interfaces where matplotlib's mathtext functionality is heavily used.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-Parser.dfrac-mjd4x64aand push.