Repository navigation
Conversation
`LegacyAttrs.handleAttributes` ran `ATTR_RE.sub` over each text. At a
`{@` with no closing `}`, the first group runs to the end of the text,
backtracks to every `=`, and the second group runs to the end again
from each one, so text made of n unclosed `{@A=` costs O(n^3): 8 KB
takes about 25 seconds to convert.
Neither group can contain `}`, so a definition that starts at `{@` ends
at the next `}`, and if the first `{@` before a `}` does not match, no
later one before that `}` can. Split the text on `}` and try
`ATTR_RE.match` once per piece, at its first `{@`. The text returned
and the attributes set are unchanged.
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.
Description
LegacyAttrs.handleAttributesranATTR_RE.sub(\{@([^\}]*)=([^\}]*)}) over every text and tail. At a{@with no closing}, the first group runs to the end of the text, backtracks to each=, and the second group runs to the end again from each one. Every{@repeats this, so text made of n unclosed{@a=costs O(n³). Withextensions=['legacy_attrs']on CPython 3.11:Neither group can contain
}, so a definition that starts at{@ends at the next}. If the first{@before a}does not match, no later{@before that}can either, because its text is a suffix of the first one's.handleAttributesnow splits the text on}and triesATTR_RE.matchonce per piece, at its first{@.ATTR_REis unchanged and still defines the syntax.I compared the returned text and the
(name, value)calls toel.set, in order, against the oldATTR_RE.subon all 47 million strings up to length 9 over{,@,=,},a, newline and space, and on 300,000 random strings built from fragments like{@id=,}}and{{@. They were identical in every case. A deliberately broken variant of the new code was caught by the same comparison. The new test takes 26 s on master and 0.3 s with the fix.AI Assistance Disclosure
No AI tools were used in preparing this PR.
AI tools were used and I have disclosed which ones, and have fully reviewed and verified their output.
Claude Code (Claude Opus 5.5) was used for everything ticked below.
AI tools were use to assist with the following:
Checklist