Quadratic time in `xml.etree.ElementTree` when parsing text with a large number of comments
Open
Nobody has claimed this yet.
extension-modules
topic-XML
type-bug
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
Bug report
Bug description:
import time
import xml.etree.ElementTree as ET
for N in (5000, 10000, 20000, 40000, 80000):
data = b"<r>" + b"x<!---->" * N + b"</r>"
s = time.perf_counter()
ET.fromstring(data)
dt = time.perf_counter() - s
print(f"{N} {dt}s")
I see:
$ python repro.py
5000 0.025435873976675794s
10000 0.0888163199997507s
20000 0.3610062320076395s
40000 1.4099939750158228s
80000 5.41402202800964s
Found by OSS-Fuzz.
CPython versions tested on:
CPython main branch
Operating systems tested on:
No response
Linked PRs
- gh-155407
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the ET.fromstring() reproducer in the issue and compare timings as the number of comments grows. Review linked PR gh-155407 to understand the work already underway; done means parsing this input no longer shows quadratic-time growth.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- performance
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100