Skip to content

[3.15] gh-162282: Fix quadratic cost in urllib.robotparser._add_entry (GH-162283) - #162446

Merged
asvetlov merged 1 commit into
python:3.15from
miss-islington:backport-45e9b3d-3.15
Nov 22, 2026
Merged

asvetlov merged 1 commit into
python:3.15from
miss-islington:backport-45e9b3d-3.15

Conversation

@miss-islington

Copy link
Copy Markdown
Contributor

Parsing a robots.txt with many identical User-agent: lines in a single
group ran in O(N^2) time: _add_entry iterated entry.useragents without
deduplication, and from the second duplicate on merge_entries was
called once per iteration; merge_entries is itself O(N).

Track the agents already registered during the current call and skip
duplicates. For every input shape other than 'same agent repeated
within one entry' the behavior is identical. On 10,000 duplicate
User-agent lines the parse time drops from ~2.5 s to ~0.003 s.

A regression test parses 50,000 duplicate User-agent lines and
requires completion in under 5 seconds.

  • Update Lib/test/test_robotparser.py

(cherry picked from commit 45e9b3d)

Co-authored-by: Vadim Toptunov vtoptunov88@gmail.com
Co-authored-by: Andrew Svetlov andrew.svetlov@gmail.com

…ythonGH-162283)

* pythongh-162282: Fix quadratic cost in urllib.robotparser._add_entry

Parsing a robots.txt with many identical User-agent: lines in a single
group ran in O(N^2) time: _add_entry iterated entry.useragents without
deduplication, and from the second duplicate on merge_entries was
called once per iteration; merge_entries is itself O(N).

Track the agents already registered during the current call and skip
duplicates. For every input shape other than 'same agent repeated
within one entry' the behavior is identical. On 10,000 duplicate
User-agent lines the parse time drops from ~2.5 s to ~0.003 s.

A regression test parses 50,000 duplicate User-agent lines and
requires completion in under 5 seconds.

* Update Lib/test/test_robotparser.py

---------
(cherry picked from commit 45e9b3db69f4f58a74911ea4b699f254b5edc036)

Co-authored-by: Vadim Toptunov <vtoptunov88@gmail.com>
Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com>
@asvetlov
asvetlov enabled auto-merge (squash) November 22, 2026 16:20
@asvetlov
asvetlov merged commit 047d65c into python:3.15 Nov 22, 2026
56 checks passed
@miss-islington
miss-islington deleted the backport-45e9b3d-3.15 branch November 22, 2026 16:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants