Repository navigation
Conversation
unique_slug restarted its suffix search at 1 for every duplicate heading, and tasklists scanned back to the list opener for every task item, so N duplicate headings or N task items took O(N^2) time (8,000 '# a' lines: 6.7s; 8,000 '- [ ] a' lines: 11.8s). Remember the next suffix per slug, and track the last token seen at each nesting level while scanning forward. Rendered output is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
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.
Problem
Two plugins take quadratic time when a construct is repeated, so a small document can tie up a server that renders untrusted Markdown:
"# a\n" * 8000withanchors_plugin"- [ ] a\n" * 8000withtasklists_plugin"# a\n" * 32000withanchors_plugin"- [ ] a\n" * 32000withtasklists_pluginunique_slugrestarts its search ata-1for every duplicate heading, so the N-th# atries N suffixes.parent_tokenscans backwards to the list opener for every task item, so the N-th item scans N tokens.The existing
test_redos.pyuses one long run inside a single construct, so it doesn't catch quadratic behavior from repeating a construct.Fix
unique_slugtakes an optionalnext_suffixdict that remembers the next suffix to try for each base slug. Slugs are never removed from the set, so every skipped suffix is still taken, and the result is the same as before. Calls without the new argument behave exactly as before.tasklistsrecords the last token seen at each nesting level while scanning forward, which gives the same parent as the backward scan in O(1).Tests
test_plugin_is_linear_in_repeatstotests/test_redos.py(10,000 repeats, 1 s budget). It times out on the old code and passes with the fix.{#id}s, nested and ordered task lists,label/label_afteroptions) with the old and new code: the output is identical.🤖 Generated with Claude Code