highlight_regex in rich.text.Text Now Expects a Compiled Regular Expression (re.compile) Has Been Passed #3347
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.
Type of changes
Checklist
CHANGELOG.mdand CONTRIBUTORS.md where appropriate.Description
highlight_regex
inrich.text.Text
now expects the passedre_highlight
to be a compiled regular expression. If astr
is passed, as was the previous expectation, thestr
will be compiled as a regular expression and the method will continue on as before.The proposed change is for a minor performance bump in the
rich.highlighter.RegexHighlighter
class. A user can now pass compiled regular expressions in the the list ofhighlights
, which will be guaranteed to not re-compiled byText
. Previously,highlight_regex
expected it was receiving astr
so it compiled the regular expression every time.There will be no breaking changes to the user as the user can continue to pass
str
in their respectiveRegexHighlighter.highlights
. The only performance degradation will be caused by theisinstance(re_highlight, str)
check, which will be minimal.Below is a silly speed test (hopefully) showing the new
highlight_regex
as performing faster. I used 600 "words" for the regular expressions because Python's currentre.
actions use a shared internal cache of a maximum of 512[0] regular expressions and I wanted demonstrate how relying on that can cause slowdowns.[0] https://github.com/python/cpython/blob/3.12/Lib/re/__init__.py#L277