Skip to content

gh-162282: Fix remaining quadratic paths in urllib.robotparser - #162442

Merged
asvetlov merged 1 commit into
python:mainfrom
asvetlov:robotparser-quadratic-followup
Nov 22, 2026
Merged

asvetlov merged 1 commit into
python:mainfrom
asvetlov:robotparser-quadratic-followup

Conversation

@asvetlov

@asvetlov asvetlov commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #162283. Two more input shapes were quadratic:

  • Many groups for the same user agent: each merge copied the accumulated rulelines. A merged entry is now extended in place when no other agent shares it.
  • A group with many user agents: _find_entry() checked the same entry once per agent, so every lookup for an unlisted agent was quadratic. All agents of a group now share one merged entry, and each distinct entry is checked once.

Results are unchanged: a differential fuzz against the previous version over 90,000 random robots.txt files found no differences. With 4000 agents (debug build), can_fetch("unknownbot") drops from 10 s to 0.001 s.

🤖 Generated with Claude Code

Merging many groups for the same user agent copied the accumulated rule
lines on every merge, and _find_entry() checked a group once per user
agent in it, so a single lookup for an unlisted agent was quadratic in
the number of agents.

Extend a merged entry in place when no other agent shares it, reuse one
merged entry for all agents of a group that shared the same entry, and
check each distinct entry only once in _find_entry().

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@asvetlov
asvetlov force-pushed the robotparser-quadratic-followup branch from 3d419a5 to 056377c Compare November 22, 2026 16:23
@asvetlov asvetlov added needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes labels Nov 22, 2026
@asvetlov
asvetlov marked this pull request as ready for review November 22, 2026 16:32
@asvetlov
asvetlov requested a review from orsenthil as a code owner November 22, 2026 16:32
@asvetlov
asvetlov enabled auto-merge (squash) November 22, 2026 16:32
@asvetlov
asvetlov merged commit 06eb745 into python:main Nov 22, 2026
60 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @asvetlov for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14, 3.15.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented Nov 22, 2026

Copy link
Copy Markdown

GH-162449 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 bugs and security fixes label Nov 22, 2026
@bedevere-app

bedevere-app Bot commented Nov 22, 2026

Copy link
Copy Markdown

GH-162450 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Nov 22, 2026
asvetlov added a commit that referenced this pull request Nov 22, 2026
…GH-162442) (#162450)

gh-162282: Fix remaining quadratic paths in urllib.robotparser (GH-162442)

Merging many groups for the same user agent copied the accumulated rule
lines on every merge, and _find_entry() checked a group once per user
agent in it, so a single lookup for an unlisted agent was quadratic in
the number of agents.

Extend a merged entry in place when no other agent shares it, reuse one
merged entry for all agents of a group that shared the same entry, and
check each distinct entry only once in _find_entry().
(cherry picked from commit 06eb745)

Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
asvetlov added a commit that referenced this pull request Nov 22, 2026
…GH-162442) (#162449)

gh-162282: Fix remaining quadratic paths in urllib.robotparser (GH-162442)

Merging many groups for the same user agent copied the accumulated rule
lines on every merge, and _find_entry() checked a group once per user
agent in it, so a single lookup for an unlisted agent was quadratic in
the number of agents.

Extend a merged entry in place when no other agent shares it, reuse one
merged entry for all agents of a group that shared the same entry, and
check each distinct entry only once in _find_entry().
(cherry picked from commit 06eb745)

Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@bedevere-bot

Copy link
Copy Markdown

⚠️⚠️⚠️ Buildbot failure ⚠️⚠️⚠️

Hi! The buildbot AMD64 FreeBSD16 3.x (tier-3) has failed when building commit 06eb745.

What do you need to do:

  1. Don't panic.
  2. Check the buildbot page in the devguide if you don't know what the buildbots are or how they work.
  3. Go to the page of the buildbot that failed (https://buildbot.python.org/#/builders/1857/builds/1300) and take a look at the build logs.
  4. Check if the failure is related to this commit (06eb745) or if it is a false positive.
  5. If the failure is related to this commit, please, reflect that on the issue and make a new Pull Request with a fix.

You can take a look at the buildbot page here:

https://buildbot.python.org/#/builders/1857/builds/1300

Failed tests:

  • test_logging

Failed subtests:

  • test_compute_rollover_W6_local_interval - test.test_logging.TimedRotatingFileHandlerTest.test_compute_rollover_W6_local_interval
  • test_compute_rollover_W6_local - test.test_logging.TimedRotatingFileHandlerTest.test_compute_rollover_W6_local
  • test_rollover_delayed_dst - test.test_logging.TimedRotatingFileHandlerTest.test_rollover_delayed_dst

Summary of the results of the build (if available):

==

Click to see traceback logs
Traceback (most recent call last):
  File "/home/buildbot/buildarea/3.x.opsec-fbsd16/build/Lib/unittest/case.py", line 690, in run
    with outcome.testPartExecutor(self):
         ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
ResourceWarning: unclosed file <_io.TextIOWrapper name='/tmp/test_python_fytgriaa/test_logging-2-7ie4uyl3.log' mode='a' encoding='utf-8'>


Traceback (most recent call last):
  File "/home/buildbot/buildarea/3.x.opsec-fbsd16/build/Lib/test/support/__init__.py", line 1256, in inner
    return func(*args, **kwds)
  File "/home/buildbot/buildarea/3.x.opsec-fbsd16/build/Lib/test/test_logging.py", line 7559, in test_compute_rollover_W6_local_interval
    test(DT(2012, 2, 19, 23, 59, 59), DT(2012, 3, 5, 0, 0))
    ~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/buildbot/buildarea/3.x.opsec-fbsd16/build/Lib/test/test_logging.py", line 7554, in test
    self.assertEqual(diff, 0, datetime.timedelta(seconds=diff))
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: 172800.0 != 0 : 2 days, 0:00:00


Traceback (most recent call last):
  File "/home/buildbot/buildarea/3.x.opsec-fbsd16/build/Lib/unittest/case.py", line 690, in run
    with outcome.testPartExecutor(self):
         ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
ResourceWarning: unclosed file <_io.TextIOWrapper name='/tmp/test_python_fytgriaa/test_logging-2-p5zcq3ak.log' mode='a' encoding='utf-8'>


Traceback (most recent call last):
  File "/home/buildbot/buildarea/3.x.opsec-fbsd16/build/Lib/test/support/__init__.py", line 1256, in inner
    return func(*args, **kwds)
  File "/home/buildbot/buildarea/3.x.opsec-fbsd16/build/Lib/test/test_logging.py", line 7382, in test_compute_rollover_W6_local
    test(DT(2012, 3, 4, 23, 59, 59), DT(2012, 3, 5, 0, 0))
    ~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/buildbot/buildarea/3.x.opsec-fbsd16/build/Lib/test/test_logging.py", line 7377, in test
    self.assertEqual(diff, 0, datetime.timedelta(seconds=diff))
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: 172800.0 != 0 : 2 days, 0:00:00


Traceback (most recent call last):
  File "/home/buildbot/buildarea/3.x.opsec-fbsd16/build/Lib/test/test_logging.py", line 6797, in test_rollover_delayed_dst
    self.assert_rollover_contents(
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        when, timestamps, expected, atTime=at_time, utc=utc)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/buildbot/buildarea/3.x.opsec-fbsd16/build/Lib/test/test_logging.py", line 6746, in assert_rollover_contents
    self.assertEqual(actual, expected)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
AssertionError: {'': ['1', '2'], '.2026-10-21': ['0']} != {'.2026-10-19': ['0'], '.2026-10-26': ['1'], '': ['2']}
- {'': ['1', '2'], '.2026-10-21': ['0']}
+ {'': ['2'], '.2026-10-19': ['0'], '.2026-10-26': ['1']}


Traceback (most recent call last):
  File "/home/buildbot/buildarea/3.x.opsec-fbsd16/build/Lib/unittest/case.py", line 690, in run
    with outcome.testPartExecutor(self):
         ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
ResourceWarning: unclosed file <_io.TextIOWrapper name='/tmp/test_python_tewoh1fx/test_logging-2-hzdpuu8p.log' mode='a' encoding='utf-8'>


Traceback (most recent call last):
  File "/home/buildbot/buildarea/3.x.opsec-fbsd16/build/Lib/unittest/case.py", line 690, in run
    with outcome.testPartExecutor(self):
         ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
ResourceWarning: unclosed file <_io.TextIOWrapper name='/tmp/test_python_tewoh1fx/test_logging-2-0995137w.log' mode='a' encoding='utf-8'>

@bedevere-bot

Copy link
Copy Markdown

⚠️⚠️⚠️ Buildbot failure ⚠️⚠️⚠️

Hi! The buildbot AMD64 FreeBSD16 3.15 (tier-3) has failed when building commit a215ad4.

What do you need to do:

  1. Don't panic.
  2. Check the buildbot page in the devguide if you don't know what the buildbots are or how they work.
  3. Go to the page of the buildbot that failed (https://buildbot.python.org/#/builders/2103/builds/397) and take a look at the build logs.
  4. Check if the failure is related to this commit (a215ad4) or if it is a false positive.
  5. If the failure is related to this commit, please, reflect that on the issue and make a new Pull Request with a fix.

You can take a look at the buildbot page here:

https://buildbot.python.org/#/builders/2103/builds/397

Failed tests:

  • test_logging

Failed subtests:

  • test_compute_rollover_W6_local_interval - test.test_logging.TimedRotatingFileHandlerTest.test_compute_rollover_W6_local_interval
  • test_compute_rollover_W6_local - test.test_logging.TimedRotatingFileHandlerTest.test_compute_rollover_W6_local
  • test_fork - test.test_multiprocessing_fork.test_processes.WithProcessesTestQueue.test_fork

Summary of the results of the build (if available):

==

Click to see traceback logs
Traceback (most recent call last):
  File "/home/buildbot/buildarea/3.15.opsec-fbsd16/build/Lib/test/support/__init__.py", line 1217, in inner
    return func(*args, **kwds)
  File "/home/buildbot/buildarea/3.15.opsec-fbsd16/build/Lib/test/test_logging.py", line 7323, in test_compute_rollover_W6_local_interval
    test(DT(2012, 2, 19, 23, 59, 59), DT(2012, 3, 5, 0, 0))
    ~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/buildbot/buildarea/3.15.opsec-fbsd16/build/Lib/test/test_logging.py", line 7318, in test
    self.assertEqual(diff, 0, datetime.timedelta(seconds=diff))
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: 172800.0 != 0 : 2 days, 0:00:00


Traceback (most recent call last):
  File "/home/buildbot/buildarea/3.15.opsec-fbsd16/build/Lib/test/support/__init__.py", line 1217, in inner
    return func(*args, **kwds)
  File "/home/buildbot/buildarea/3.15.opsec-fbsd16/build/Lib/test/test_logging.py", line 7146, in test_compute_rollover_W6_local
    test(DT(2012, 3, 4, 23, 59, 59), DT(2012, 3, 5, 0, 0))
    ~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/buildbot/buildarea/3.15.opsec-fbsd16/build/Lib/test/test_logging.py", line 7141, in test
    self.assertEqual(diff, 0, datetime.timedelta(seconds=diff))
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: 172800.0 != 0 : 2 days, 0:00:00


Traceback (most recent call last):
  File "/home/buildbot/buildarea/3.15.opsec-fbsd16/build/Lib/unittest/case.py", line 690, in run
    with outcome.testPartExecutor(self):
         ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
ResourceWarning: unclosed file <_io.TextIOWrapper name='/tmp/test_python_hnbv7m78/test_logging-2-qslvi5my.log' mode='a' encoding='utf-8'>


Traceback (most recent call last):
  File "/home/buildbot/buildarea/3.15.opsec-fbsd16/build/Lib/contextlib.py", line 116, in inner
    return func(*args, **kwds)
  File "/home/buildbot/buildarea/3.15.opsec-fbsd16/build/Lib/test/_test_multiprocessing.py", line 1360, in test_fork
    self.assertEqual(queue.get(), i)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^
AssertionError: 10 != 1


Traceback (most recent call last):
  File "/home/buildbot/buildarea/3.15.opsec-fbsd16/build/Lib/unittest/case.py", line 690, in run
    with outcome.testPartExecutor(self):
         ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
ResourceWarning: unclosed file <_io.TextIOWrapper name='/tmp/test_python_ep6kd58z/test_logging-2-jgmhr54r.log' mode='a' encoding='utf-8'>


Traceback (most recent call last):
  File "/home/buildbot/buildarea/3.15.opsec-fbsd16/build/Lib/unittest/case.py", line 690, in run
    with outcome.testPartExecutor(self):
         ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
ResourceWarning: unclosed file <_io.TextIOWrapper name='/tmp/test_python_hnbv7m78/test_logging-2-5fy7g1b8.log' mode='a' encoding='utf-8'>


Traceback (most recent call last):
  File "/home/buildbot/buildarea/3.15.opsec-fbsd16/build/Lib/unittest/case.py", line 690, in run
    with outcome.testPartExecutor(self):
         ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
ResourceWarning: unclosed file <_io.TextIOWrapper name='/tmp/test_python_ep6kd58z/test_logging-2-5urp_5fm.log' mode='a' encoding='utf-8'>

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.

2 participants