python / python/cpython

Optimize robotparser for long list of rules

Open
#149,381 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

performance stdlib type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Previously, robotparser implemented old pre-standard specification which nobody uses now. It returned the result after finding the first matching rule, which worked incorrectly in many cases (see #83368). After #138907 it follows the longest path rule.

The code can be optimized, for example by sorting rules by the path length, matching them from longest to shorter and stopping if the match is longer than the remaining paths. This can only be used for paths which do not contain metacharacters * and $.

Other optimizations can also be used, for example a trie-like structure, which could also be used for paths with metacharacters. But this will significantly complicate the code.

I am not actually sure that such optimization is necessary. In most cases the number of rules should not be too large. This is why I did not include it in the previous PR. We need to collect some data first. So I publish my code as a draft.

Linked PRs
  • gh-149382

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the robotparser rule-matching implementation and the draft work linked as gh-149382. Collect data on rule-list sizes and matching performance before choosing between sorted rules and a trie-like structure; done requires evidence that an optimization is necessary and that the longest-path behavior remains correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
networking
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.