Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify graph in get_topological_weights. #10574

Merged
merged 13 commits into from
Nov 20, 2021
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/10557.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Optimize installation order calculation to improve performance when installing requirements that form a complex dependency graph with a large amount of edges.
32 changes: 32 additions & 0 deletions src/pip/_internal/resolution/resolvelib/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ def get_installation_order(
"""
assert self._result is not None, "must call resolve() first"

if not req_set.requirements:
# Nothing is left to install, so we do not need an order.
return []

graph = self._result.graph
weights = get_topological_weights(
graph,
Expand Down Expand Up @@ -227,6 +231,34 @@ def visit(node: Optional[str]) -> None:
last_known_parent_count = weights.get(node, 0)
weights[node] = max(last_known_parent_count, len(path))

# Simplify the graph, pruning leaves that have no dependencies.
# This is needed for large graphs (say over 200 packages) because the
# `visit` function is exponentially slower then, taking minutes.
# See https://github.com/pypa/pip/issues/10557
# We will loop until we explicitly break the loop.
while True:
leaves = set()
for key in graph:
if key is None:
continue
for _child in graph.iter_children(key):
# This means we have at least one child
break
else:
# No child.
leaves.add(key)
if not leaves:
# We are done simplifying.
break
# Calculate the weight for the leaves.
weight = len(graph) - 1
for leaf in leaves:
weights[leaf] = weight
# Remove the leaves from the graph, making it simpler.
for leaf in leaves:
graph.remove(leaf)

# Visit the remaining graph.
# `None` is guaranteed to be the root node by resolvelib.
visit(None)

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/resolution_resolvelib/test_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def test_new_resolver_get_installation_order(
("three", "four"),
("four", "five"),
],
{None: 0, "one": 1, "two": 1, "three": 2, "four": 3, "five": 4},
{None: 0, "five": 5, "four": 4, "one": 4, "three": 2, "two": 1},
),
(
"linear",
Expand Down