diff --git a/news/+xcal-parser-linear-time.bugfix b/news/+xcal-parser-linear-time.bugfix new file mode 100644 index 000000000..c5923b46b --- /dev/null +++ b/news/+xcal-parser-linear-time.bugfix @@ -0,0 +1 @@ +Fixed the quadratic run time of :meth:`Component.from_xcal ` for xCal documents with many properties, parameters, values, or components. I used Claude Fable 5.1 to find the issue and to write the fix and the test. @shamsManna diff --git a/src/icalendar/parser/xcal/base.py b/src/icalendar/parser/xcal/base.py index 26313823e..4511a2fc6 100644 --- a/src/icalendar/parser/xcal/base.py +++ b/src/icalendar/parser/xcal/base.py @@ -2,6 +2,7 @@ from __future__ import annotations +from collections import deque from collections.abc import Sequence from typing import TYPE_CHECKING @@ -30,7 +31,7 @@ class XCalParser: def __init__(self, element: Element | ElementAdapter) -> None: self._element = ElementAdapter.with_element(element) - self._children = self._element.children + self._children = deque(self._element.children) self._consumed = 0 self._element_is_parsed = False @@ -62,7 +63,7 @@ def done(self): """ if self.is_finished(): raise InvalidParserState("No more children available.") - self._children = self._children[1:] + self._children.popleft() self._consumed += 1 @property diff --git a/src/icalendar/tests/rfc_6321_xcal/test_parser_linear_time.py b/src/icalendar/tests/rfc_6321_xcal/test_parser_linear_time.py new file mode 100644 index 000000000..8fad43a0f --- /dev/null +++ b/src/icalendar/tests/rfc_6321_xcal/test_parser_linear_time.py @@ -0,0 +1,69 @@ +"""Regression test for the quadratic run time of the xCal parser. + +``XCalParser.done()`` copied the remaining child elements into a new list for +every element it parsed, and ``_parse_tag()`` deleted from the front of that +list, so an xCal document with many properties, parameters, values or +components took seconds to minutes to parse. The children are kept in a deque +now and parsing is linear in the number of elements. +""" + +import time + +import pytest + +from icalendar import Calendar + +XCAL = ( + '' + "{}" +) + + +def properties(n): + return XCAL.format( + "" + "a" * n + "" + ) + + +def values(n): + return XCAL.format( + "" + "a" * n + "" + ) + + +def parameters(n): + return XCAL.format( + "" + + "a" * n + + "s" + ) + + +def components(n): + return XCAL.format("" + "" * n + "") + + +@pytest.mark.parametrize( + ("xcal", "n", "count"), + [ + (properties, 100_000, lambda calendar, n: len(calendar["X-A"]) == n), + (values, 500_000, lambda calendar, n: len(calendar["CATEGORIES"].cats) == n), + ( + parameters, + 300_000, + lambda calendar, n: len(calendar["SUMMARY"].params["X-P"]) == n, + ), + (components, 100_000, lambda calendar, n: len(calendar.subcomponents) == n), + ], +) +def test_many_elements_parse_quickly(xcal, n, count): + """Parsing is linear in the number of elements. + + The unpatched parser needs 10 s to minutes for these documents; the bound + is generous enough to never flake on slow CI while still catching a return + of the quadratic behaviour. + """ + start = time.perf_counter() + calendar = Calendar.from_xcal(xcal(n).encode())[0] + assert time.perf_counter() - start < 5 + assert count(calendar, n)