Repository navigation
[3.15] gh-162282: Fix quadratic cost in urllib.robotparser._add_entry (GH-162283) - #162446
Merged
Merged
Conversation
…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>
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.
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.
(cherry picked from commit 45e9b3d)
Co-authored-by: Vadim Toptunov vtoptunov88@gmail.com
Co-authored-by: Andrew Svetlov andrew.svetlov@gmail.com