Repository navigation
fix quadratic time of escaped quotes in closes_quoted_section - #2117
shamsManna wants to merge 2 commits into
Conversation
closes_quoted_section sliced the rest of the content line on every call, and it runs once per escaped quote in a quoted parameter value, so parsing a value made of many \" pairs was quadratic. Iterate by index over the same range instead. AI: Claude Opus 5.5 found the quadratic scan while reviewing the parser, wrote the fix, the regression test and the change log entry, and benchmarked the before and after times. I reviewed and ran the changes.
|
One or more PR-quality checks did not pass. Review the PR Quality Checks summary in the workflow run for the failed checks and their details, then make any necessary updates. Checklist items may remain unchecked while work is in progress. Please tick each item as you complete it. If you used AI assistance, follow the project's Responsible AI use policy and provide the required disclosures. |
|
Thanks for the pull request, @shamsManna! A maintainer will review it shortly. |
Mixed activityActivity patterns show a mix of organic and automated signals. Evidence
Last 5 PRs: This is an automated analysis by AgentScan |
| # index instead of slicing: a slice copies the rest of the line on every | ||
| # call, which is quadratic for a value with many escaped quotes |
There was a problem hiding this comment.
Thank you for the explanation but at this location, no one understands it. Since you reviewed it, what is the essence of this change and what is worth mentionen in this function?
There was a problem hiding this comment.
fair, that comment only made sense next to the diff. the point worth keeping in the function is that the parsers call it once for every DQUOTE in a quoted value, so it has to stop at the first non-whitespace character after the quote without copying the rest of the line. going back to text[quote_index + 1 :] would bring the quadratic case back. reworded the comment to say that in c4fca79.
Linked issue
Description
closes_quoted_sectionruns once for every escaped quote (\") inside a quoted parameter value, fromq_split,Contentline.parts,Contentline.value_separator_indexand_strip_ows_around_delimiters. It iterated overtext[quote_index + 1 :], and that slice copies the rest of the content line on every call even though the loop usually returns after one character. So a parameter value made of n escaped quotes costs O(n^2) inCalendar.from_ical, and any service that parses an uploaded .ics can be stalled by one long line.Indexing over the same range gives the same result without the copy, so all four callers are linear again.
Checklist
Additional information
Input:
X;X-P="+\"* n +":vinside a VCALENDAR, parsed withCalendar.from_ical.The new test in
test_parser_quadratic_unfolding.pytakes 13 s without the fix and fails its 5 s bound. With the fix it passes, and so does the rest of the suite.