Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/+xcal-parser-linear-time.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed the quadratic run time of :meth:`Component.from_xcal <icalendar.cal.component.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
5 changes: 3 additions & 2 deletions src/icalendar/parser/xcal/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

from collections import deque
from collections.abc import Sequence
from typing import TYPE_CHECKING

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
69 changes: 69 additions & 0 deletions src/icalendar/tests/rfc_6321_xcal/test_parser_linear_time.py
Original file line number Diff line number Diff line change
@@ -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 = (
'<icalendar xmlns="urn:ietf:params:xml:ns:icalendar-2.0">'
"<vcalendar>{}</vcalendar></icalendar>"
)


def properties(n):
return XCAL.format(
"<properties>" + "<x-a><text>a</text></x-a>" * n + "</properties>"
)


def values(n):
return XCAL.format(
"<properties><categories>" + "<text>a</text>" * n + "</categories></properties>"
)


def parameters(n):
return XCAL.format(
"<properties><summary><parameters><x-p>"
+ "<text>a</text>" * n
+ "</x-p></parameters><text>s</text></summary></properties>"
)


def components(n):
return XCAL.format("<components>" + "<x-c/>" * n + "</components>")


@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)
Loading