Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

gh-163147: Fix quadratic merging of robots.txt groups in urllib.robotparser - #163207

Open
frostyhand wants to merge 1 commit into
python:mainfrom
frostyhand:gh-163147-robotparser-merge
Open

frostyhand wants to merge 1 commit into
python:mainfrom
frostyhand:gh-163147-robotparser-merge

Conversation

@frostyhand

Copy link
Copy Markdown

When two groups name the same user agent, _add_entry merges them, and _merge_into rebuilt the merged entry's agent list by filtering the old entry's whole list. That scan is what made parsing quadratic:

  • Repeated agent (the case in the issue): one group repeats User-agent: a N times, then N more groups name a. Every merge rescans the N repeated names.
  • Group per agent: one group names N different agents, then each agent gets its own group. Every split rescans all N names.

The merged entry applies to exactly the agents being moved to it, and _add_entry already knows them, so I pass that list to merge_entries and drop the filter. The in-place path no longer touches the agent list at all, since every agent sharing that entry is already on it.

Timings on Python 3.15.2 (best of 3, parse() only):

Shape Lines Size main this PR
Repeated agent 48,001 641 KiB 2.19 s 0.037 s
Group per agent 48,001 775 KiB 1.94 s 0.045 s

Both grew about 4x per doubling on main, and grow linearly with this change.

One behaviour change. Because the merged entry now lists the lowercased agent keys (the same keys as self.groups), merging no longer drops agents whose spelling differs in case. On main, with groups for FooBot and foobot plus User-agent: * / Disallow: /, can_fetch("FooBot/1.0", "/c") returns False: the merged entry ended up with no agents, so a versioned product token fell through to the * group. RFC 9309 says user agent matching is case-insensitive, so it now returns True. There is a test for it.

Not covered here. Splitting a group that has many rules still copies those rules once per split (rulelines.copy() in merge_entries), so N agents sharing a group of N rules, each followed by its own group, is still O(N²). Fixing that needs the merge to happen lazily at lookup time, which is a bigger change than this one. I can open a separate PR for it if that is wanted.

Tests: three new tests in RepeatedUserAgentTest. The repeated agent test and the different case test fail on main. test_robotparser and test_urllib2 pass.

….robotparser

A merged entry now takes the list of agents moved to it, instead of
filtering the old entry's agent list on every merge. That filter was
quadratic when the first group repeated a User-agent line, and when a
group naming many agents was split one agent at a time.

Because the merged entry now lists the lowercased agent keys, a group
spelled in different case (FooBot, foobot) also matches a versioned
product token such as FooBot/1.0, instead of falling back to "*".
@frostyhand
frostyhand requested a review from orsenthil as a code owner December 5, 2026 13:56
@python-cla-bot

python-cla-bot Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant