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.

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