Skip to content

fix quadratic time of escaped quotes in closes_quoted_section - #2117

Open
shamsManna wants to merge 2 commits into
PyCalendar:mainfrom
shamsManna:escaped-quote-scan-linear
Open

shamsManna wants to merge 2 commits into
PyCalendar:mainfrom
shamsManna:escaped-quote-scan-linear

Conversation

@shamsManna

Copy link
Copy Markdown
Contributor

Linked issue

  • Closes #ISSUE_NUMBER

Description

closes_quoted_section runs once for every escaped quote (\") inside a quoted parameter value, from q_split, Contentline.parts, Contentline.value_separator_index and _strip_ows_around_delimiters. It iterated over text[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) in Calendar.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

  • I added a change log entry, following the instructions in all subsections under Change log requirements.
  • I followed icalendar's Artificial intelligence policy and disclosed my Responsible AI use in my commit messages, if applicable.
  • I added or updated tests, if applicable.
  • I ran and ensured all tests pass locally by following Run tests.
  • I added or edited documentation as necessary, both as docstrings to be rendered in the API documentation and narrative documentation, following the Style guide.

Additional information

Input: X;X-P=" + \" * n + ":v inside a VCALENDAR, parsed with Calendar.from_ical.

line size before after
156 KiB 1.66 s 0.23 s
977 KiB 99.99 s 1.55 s

The new test in test_parser_quadratic_unfolding.py takes 13 s without the fix and fails its 5 s bound. With the fix it passes, and so does the rest of the suite.

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.
@github-actions github-actions Bot added the ai-suspicion This contribution is possibly created with lots of AI help without enough human understanding. label Nov 14, 2026
@github-actions

Copy link
Copy Markdown
Contributor

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.

@github-actions

Copy link
Copy Markdown
Contributor

Thanks for the pull request, @shamsManna! A maintainer will review it shortly.

@github-actions

Copy link
Copy Markdown
Contributor

Mixed activity

Activity patterns show a mix of organic and automated signals.

View full analysis →

Evidence
  • Rapid fork→PR pattern: 12/12 fork branches followed by upstream PRs within 8s
  • Young account: Account is 54 days old

Last 5 PRs:

This is an automated analysis by AgentScan

@github-actions github-actions Bot added the agentscan:needs-review AgentScan flagged this item for review because it has mixed signals label Nov 14, 2026
Comment thread src/icalendar/parser/parameter.py Outdated
Comment on lines +165 to +166
# 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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agentscan:needs-review AgentScan flagged this item for review because it has mixed signals ai-suspicion This contribution is possibly created with lots of AI help without enough human understanding.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants