Use named list for regex substitution of namespace prefixes #708
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.
I profiled se lint because it seemed to be surprisingly slow on a fairly small ebook repository. This revealed that half of the execution time was spent in the _replace_shorthand_namespaces method.
For the same repository, changing that method to use a named list (and therefore, only a single call to regex.sub) reduced the time spent in that method from ~15 seconds to ~5 seconds. This optimization is correct, and arguably easier to understand, because a single attribute can only have one prefix.
Using a named list also avoids the need to manually escape the prefixes before including them in the regex pattern, which this code was incorrectly skipping.
Finally, this change corrects a mistake in that method's documentation, as the : suffix is not retained in the output.