Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit 152a16f

Browse files
authored
Fix quadratic rendering time for many inline links (#1621)
Scanning from index 0 after every inline-pattern match rescanned the unprocessed text repeatedly, making conversion quadratic in the number of inline elements. Return the index just past the inserted placeholder so the next scan starts at the unprocessed tail. Fixes #1619.
1 parent 7be0cff commit 152a16f

3 files changed

Lines changed: 47 additions & 2 deletions

File tree

‎docs/changelog.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ See the [Contributing Guide](contributing.md) for details.
1212

1313
## [Unreleased]
1414

15+
### Changed
16+
17+
* Inline processors now resume searching after the previous match, improving
18+
performance for repeated inline patterns (#1619).
19+
1520
### Fixed
1621

1722
* Fix an issue with excessive backtracking when matching inline code blocks (#1617).

‎markdown/treeprocessors.py‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,12 +323,17 @@ def __applyPattern(
323323
placeholder = self.__stashNode(node, pattern.type())
324324

325325
if new_style:
326+
# Return the index just past the inserted placeholder so the
327+
# next call scans only the unprocessed tail. Scanning from 0
328+
# after every match makes repeated inline patterns quadratic.
326329
return "{}{}{}".format(data[:start],
327-
placeholder, data[end:]), True, 0
330+
placeholder, data[end:]), True, start + len(placeholder)
328331
else: # pragma: no cover
329332
return "{}{}{}{}".format(leftData,
330333
match.group(1),
331-
placeholder, match.groups()[-1]), True, 0
334+
placeholder, match.groups()[-1]), True, (
335+
len(leftData) + len(match.group(1)) + len(placeholder)
336+
)
332337

333338
def __build_ancestors(self, parent: etree.Element | None, parents: list[str]) -> None:
334339
"""Build the ancestor list."""

‎tests/test_apis.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,13 +725,48 @@ def testInlineProcessorDoesntCrashWithWrongAtomicString(self):
725725
'<div><p>a &lt;b&gt;atomic&lt;/b&gt; c</p></div>'
726726
)
727727

728+
def testInlineProcessorAdvancesSearchIndex(self):
729+
"""Test that repeated matches resume after the previous match."""
730+
pattern = _InlineProcessorThatRecordsSearchIndex(r'x', self.md)
731+
self.md.inlinePatterns.register(pattern, 'record-search-index', 1000)
732+
733+
self.assertEqual(self.md.convert('xxxx'), '<p>xxxx</p>')
734+
self.assertEqual(pattern.start_indices[0], 0)
735+
self.assertGreater(pattern.start_indices[1], pattern.start_indices[0])
736+
728737

729738
class _InlineProcessorThatReturnsAtomicString(inlinepatterns.InlineProcessor):
730739
""" Return a simple text of `group(1)` of a Pattern. """
731740
def handleMatch(self, m, data):
732741
return markdown.util.AtomicString('<b>atomic</b>'), m.start(0), m.end(0)
733742

734743

744+
class _RecordingPattern:
745+
"""Proxy a compiled pattern while recording the search offsets."""
746+
747+
def __init__(self, pattern, start_indices):
748+
self.pattern = pattern
749+
self.start_indices = start_indices
750+
751+
def finditer(self, data, start_index=0):
752+
self.start_indices.append(start_index)
753+
return self.pattern.finditer(data, start_index)
754+
755+
756+
class _InlineProcessorThatRecordsSearchIndex(inlinepatterns.InlineProcessor):
757+
"""Record each offset passed to the processor's compiled expression."""
758+
759+
def __init__(self, pattern, md):
760+
super().__init__(pattern, md)
761+
self.start_indices = []
762+
763+
def getCompiledRegExp(self):
764+
return _RecordingPattern(self.compiled_re, self.start_indices)
765+
766+
def handleMatch(self, m, data):
767+
return m.group(0), m.start(0), m.end(0)
768+
769+
735770
class TestConfigParsing(unittest.TestCase):
736771
def assertParses(self, value, result):
737772
self.assertIs(markdown.util.parseBoolValue(value, False), result)

0 commit comments

Comments
 (0)