Skip to content

fix quadratic time of XCalParser.done in Component.from_xcal - #2188

Open
shamsManna wants to merge 1 commit into
PyCalendar:mainfrom
shamsManna:xcal-parser-linear-time
Open

shamsManna wants to merge 1 commit into
PyCalendar:mainfrom
shamsManna:xcal-parser-linear-time

Conversation

@shamsManna

Copy link
Copy Markdown
Contributor

Linked issue

Description

XCalParser.done() marks the current child as parsed by re-slicing the list of remaining children, and _parse_tag() deletes from the front of that list, so every property, parameter value, property value and component parsed from xCal copies all the children that are left. Component.from_xcal is quadratic in the number of sibling elements: 160k empty components (938 KiB) take 49 s and 80k properties (1.9 MiB) take 20.7 s, while ElementTree parses the same XML in 0.04 s, so an untrusted xCal document of about 1 MiB stalls the parser for a minute. Fix: keep the children in a deque, which makes both operations O(1); the same inputs parse in 0.27 s and 0.34 s. Same kind of problem as #2037, in the xCal parser from #1789.

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

Timings for Calendar.from_xcal before -> after: 80k properties 20.7 s -> 0.34 s, 80k components 7.3 s -> 0.13 s, 160k components 49 s -> 0.27 s. The regression test fails on main for all four shapes (properties, values, parameter values, components) and passes with the fix; the full suite passes locally.

XCalParser.done() marked the current child as parsed by re-slicing the
list of remaining children, and _parse_tag() deleted from the front of
that list, so every property, parameter value, property value and
component parsed from xCal copied all the children that were left.
Component.from_xcal took quadratic time in the number of sibling
elements: 80000 properties (1.9 MiB) took 20.7 s and 160000 empty
components (938 KiB) took 49 s, while ElementTree parses the same XML
in 0.04 s.

Keep the children in a deque, so done() is a popleft() and removing
the first match in _parse_tag() is O(1). The same inputs now parse in
0.34 s and 0.27 s. A regression test parses 100k properties, 500k
values, 300k parameter values and 100k components under a time bound.

AI: Claude Fable 5.1 (claude-fable-5-1) via Claude Code.
Prompt: find a security bug in icalendar, reproduce it, and write a
minimal fix with a regression test and change log entry.
Output: (this commit), reviewed by the author.
@github-actions

Copy link
Copy Markdown
Contributor

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

@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 21, 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.

niccokunzmann added a commit to PyCalendar/dashboard.pycal.org that referenced this pull request Nov 21, 2026
Each task on the page of a pull request links the least that shows why
its condition holds or not. The data specs give the links (the pull
request, its labels, its checks, the merge box, each review) and the
conditions choose: as many reviews as decide the comparison, the reason
of an unknown CI, the page of the author for the states of people. The
merge box of GitHub has no stable id, so it links #issue-comment-box,
right below it.

The page /projects/<slug>/contributors/<handle>/ shows the states of a
person with the tabs Profile, Reviews, Issues and Pull requests.

The tab Steps explains the steps in the words of the person.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

AI Prompts:
claude-opus-5-5: <ide_opened_file>The user opened the file ./2188.html in the IDE. This may or may not be related to the current task.</ide_opened_file> https://dashboard.pycal.org/projects/icalendar/pull/2184/  This shows peer reviews required.  As a general rule, conditions show evidence of it They show the minimal evience. Same for the spec, so that can be re-used.  auto merge enabled - a link to the PR at the place where I can see the merge dialog waiting, usually the bottom of the PR if that is possible, the CI box Same for branch outdated and uptodate  Ci failing and passing goes to https://github.com/PyCalendar/icalendar/pull/2188/checks CI unknown just gives a reason (closed/merged) and links to PyCalendar/icalendar#2188  PyCalendar/icalendar#2188 also open/closed/merged and labels  merge conflict also links to the bottom CI box. For that have a look at 2188.html. This box can have several id= elements. See if you can retrieve the HTML in some way and link to one of the ids. We need to do a hack here.   review ratio and reviews are a bit more complicated. It is enough for the reviews condition to show X reviews that are required to satisfy/ falsify the condition. E.g. if 4 are required and only 3 are there, show links to 3 of them If 2 are at most, then show a link to three if there are more, else to all of them.  For the ratio, link to https://dashboard.pycal.org/projects/icalendar/contributors/<username> That should show the minimal required info of the person and is a tabbed interface for Profile, reviews and issues and PRs with their count   Please take the links as examples.
claude-opus-5-5: A pull request is in the left-most step that has a task for it: a condition of its filter that applies. The tasks are what keeps it from being merged.  turns into  Each step has tasks to complete. A pull request advances to the next step only if all tasks in a step are complted. Click on a step and find out what there is to do at each step.

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

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.

1 participant