Repository navigation
fix: avoid quadratic re_newline_whitespace scan in process_text - #296
Open
Chandan3456 wants to merge 1 commit into
Open
Chandan3456 wants to merge 1 commit into
Chandan3456 wants to merge 1 commit into
Conversation
On a run of spaces or tabs with no newline, re.sub retried the pattern at every position of the run and each attempt scanned to the end of it, so text conversion was quadratic in the run length (64 KiB of spaces took about 10 s). A match can only start at the beginning of a whitespace run, so anchor it there with a lookbehind: matches are unchanged and the scan is linear.
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.
process_text collapses newline whitespace in every text node with re_newline_whitespace,
[\t \r\n]*[\r\n][\t \r\n]*. on a run of spaces or tabs that contains no newline, re.sub retries the match at each position of the run and every attempt scans to the end of the run before failing, so conversion time grows with the square of the run length. this is the default path (wrap=False, any text outside<pre>), so a single small document like'<p>' + ' ' * 65536 + 'x</p>'takes about ten seconds to convert, and each doubling of the run quadruples that. it is the same kind of regex backtracking (CWE-1333) as the<pre>strip in #286 / #287, but on ordinary text, which that change does not touch.the original pattern can only ever match starting at the first character of a whitespace run, so a
(?<![\t \r\n])lookbehind leaves the matches unchanged and makes every other start position fail in one step. compared against the old pattern on all 488,281 strings of length 0 to 8 over space, tab,\r,\nandx, plus 20,000 random strings that include nbsp,\vand\f: no differences.markdownify() on python 3.8, same process, identical output:
<p><p><p>the new test in tests/test_basic.py fails on develop (about 18 s) and passes in milliseconds with the fix. pytest (84 passed), flake8, restructuredtext-lint, mypy and the wheel/sdist build are clean.