Speed up codespell:ignore
check by skipping the regex in most cases
#3721
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.
The codespell codebase unsurprisingly spends a vast majority of its runtime in various regex related code such as
search
andfinditer
.The best way to optimize runtime spend in regexes is to not do a regex in the first place, since the regex engine has a rather steep overhead over regular string primitives (that is at the cost of flexibility). If the regex rarely matches and there is a very easy static substring that can be used to rule out the match, then you can speed up the code by using
substring in string
as a conditional to skip the regex. This is assuming the regex is used enough for the performance to matter.An obvious choice here falls on the
codespell:ignore
regex, because it has a very distinctive substring in the form ofcodespell:ignore
, which will rule out almost all lines that will not match.With this little trick, runtime goes from ~5.4s to ~4.5s on the corpus mentioned in #3419.