Repository navigation
gh-163147: Fix quadratic merging of robots.txt groups in urllib.robotparser - #163207
Open
frostyhand wants to merge 1 commit into
Open
frostyhand wants to merge 1 commit into
frostyhand wants to merge 1 commit into
Conversation
….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 "*".
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When two groups name the same user agent,
_add_entrymerges them, and_merge_intorebuilt the merged entry's agent list by filtering the old entry's whole list. That scan is what made parsing quadratic:User-agent: aN times, then N more groups namea. Every merge rescans the N repeated names.The merged entry applies to exactly the agents being moved to it, and
_add_entryalready knows them, so I pass that list tomerge_entriesand 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):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 forFooBotandfoobotplusUser-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()inmerge_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_robotparserandtest_urllib2pass.