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

Optimize provider's is_backtrack_cause by caching resolved names #10621

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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/10621.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Optimize performance of conflict cause resolution while backtracking.
jbylund marked this conversation as resolved.
Show resolved Hide resolved
26 changes: 20 additions & 6 deletions src/pip/_internal/resolution/resolvelib/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Iterator,
Mapping,
Sequence,
Set,
TypeVar,
Union,
)
Expand Down Expand Up @@ -75,6 +76,24 @@ def _get_with_identifier(
return default


key_to_names: Dict[int, Set[str]] = {}


def _get_causes_set(backtrack_causes: Sequence["PreferenceInformation"]) -> Set[str]:
key = id(backtrack_causes)
Copy link
Member

@notatallshaw notatallshaw Oct 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the id being calculated here is the id of a list, but that list mutates:

self.state.backtrack_causes[:] = causes

So I think this is an incorrect assumption and that the backtrack causes could change in the list but your cache would not reflect that.

cached_names = key_to_names.get(key)
if cached_names is None:
key_to_names.clear()
cached_names = set()
for backtrack_cause in backtrack_causes:
cached_names.add(backtrack_cause.requirement.name)
parent = backtrack_cause.parent
if parent:
cached_names.add(parent.name)
key_to_names[key] = cached_names
return cached_names


class PipProvider(_ProviderBase):
"""Pip's provider implementation for resolvelib.

Expand Down Expand Up @@ -240,9 +259,4 @@ def get_dependencies(self, candidate: Candidate) -> Sequence[Requirement]:
def is_backtrack_cause(
identifier: str, backtrack_causes: Sequence["PreferenceInformation"]
) -> bool:
for backtrack_cause in backtrack_causes:
if identifier == backtrack_cause.requirement.name:
return True
if backtrack_cause.parent and identifier == backtrack_cause.parent.name:
return True
return False
return identifier in _get_causes_set(backtrack_causes)