← Back to Pull requests

perf: avoid repeated dependency scans in pip show #14103

Closed
unknown closed this pull request without merging on Jul 02, 2026 16:11 UTC
View on GitHub
unknown unknown commented on Jun 25, 2026 01:07 UTC
Author

Fixes #14102.

pip show currently computes Required-by separately for each queried package.
For multiple queried packages, this repeatedly scans all installed distributions
and parses dependency metadata.

This PR avoids that repeated work. For a single queried package, it keeps a
direct lookup path and short-circuits dependency scans when possible. For
multiple queried packages, it builds the reverse dependency mapping once while
walking installed distributions, then reuses it for each queried package.

Benchmark
---------

I used python -m timeit with a mocked installed environment containing 2,000
distributions and 1,000 dependents, varying the number of queried packages.

To reproduce, run this once on main and once on this PR branch:

for queries in 1 5 20 50; do
  echo "queries=$queries"
  SETUP=$(cat <<PY
from pip._internal.commands import show

class Metadata(dict):
    def get_all(self, key, default=None):
        return self.get(key, [] if default is None else default)

class Dependency:
    def __init__(self, name):
        self.name = name

class Distribution:
    def __init__(self, name, dependencies):
        self.canonical_name = name
        self.raw_name = name
        self.raw_version = "1.0"
        self.location = ""
        self.editable_project_location = None
        self.metadata = Metadata({"Name": name, "Summary": "", "Home-page": "", "Author": "", "Author-email": "", "License": "", "License-Expression": "", "Classifier": [], "Project-URL": []})
        self.metadata_version = "2.4"
        self.installer = "pip"
        self._dependencies = [Dependency(dep) for dep in dependencies]
    def iter_dependencies(self):
        return iter(self._dependencies)
    def iter_raw_dependencies(self):
        return iter(())
    def read_text(self, name):
        raise FileNotFoundError(name)
    def iter_declared_entries(self):
        return None

class Environment:
    def __init__(self, distributions):
        self._distributions = distributions
    def iter_all_distributions(self):
        return iter(self._distributions)

query_names = [f"target-{i}" for i in range($queries)]
distributions = [Distribution(name, [f"base-dep-{i % 5}"]) for i, name in enumerate(query_names)]
for i in range(2000 - $queries):
    deps = [f"noise-dep-{i % 25}"]
    if i < 1000:
        deps.append(query_names[i % $queries])
    distributions.append(Distribution(f"package-{i}", deps))
show.get_default_environment = lambda: Environment(distributions)
PY
)
  uv run python -m timeit -r 11 -n 50 -s "$SETUP" "list(show.search_packages_info(query_names))"
done

My latest results:

| queried packages | main | PR |
| --- | ---: | ---: |
| 1 | 662 usec/loop | 313 usec/loop |
| 5 | 2.85 msec/loop | 485 usec/loop |
| 20 | 11.1 msec/loop | 509 usec/loop |
| 50 | 27.5 msec/loop | 578 usec/loop |

This keeps the common single-package case faster than main, while preserving
the expected scaling improvement for multiple queried packages.

Diff: 374 lines (+280 / -94) across 4 files Reviewer: 2ykwang Review duration: 10983m
All Vouch review confidence checks passed
Residual risk 0.19 = Change Risk (0.43) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.43 · 374 lines diff across 4 files
0.43
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
0.17
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
56%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.355
Shallow Review Depth shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #14103"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 56.8% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 42.9% (MEDIUM) · Higher = larger blast radius
42.9%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 56.8% · Lower = rushed or shallow review
56.8%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 18.5% · Safe to merge
18.5%
Repository