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

Cache Requirement Object Creation #10550

Merged
merged 6 commits into from
Oct 9, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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/10550.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve performance of dependency resolution.
jbylund marked this conversation as resolved.
Show resolved Hide resolved
14 changes: 10 additions & 4 deletions src/pip/_internal/req/constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
InstallRequirement.
"""

import functools
import logging
import os
import re
Expand Down Expand Up @@ -39,6 +40,11 @@
operators = Specifier._operators.keys()


@functools.lru_cache(maxsize=None)
def get_or_create_requirement(req_string: str) -> Requirement:
uranusjr marked this conversation as resolved.
Show resolved Hide resolved
return Requirement(req_string)


def _strip_extras(path: str) -> Tuple[str, Optional[str]]:
m = re.match(r"^(.+)(\[[^\]]+\])$", path)
extras = None
Expand All @@ -54,7 +60,7 @@ def _strip_extras(path: str) -> Tuple[str, Optional[str]]:
def convert_extras(extras: Optional[str]) -> Set[str]:
if not extras:
return set()
return Requirement("placeholder" + extras.lower()).extras
return get_or_create_requirement("placeholder" + extras.lower()).extras


def parse_editable(editable_req: str) -> Tuple[Optional[str], str, Set[str]]:
Expand Down Expand Up @@ -83,7 +89,7 @@ def parse_editable(editable_req: str) -> Tuple[Optional[str], str, Set[str]]:
return (
package_name,
url_no_extras,
Requirement("placeholder" + extras.lower()).extras,
get_or_create_requirement("placeholder" + extras.lower()).extras,
)
else:
return package_name, url_no_extras, set()
Expand Down Expand Up @@ -309,7 +315,7 @@ def with_source(text: str) -> str:

def _parse_req_string(req_as_string: str) -> Requirement:
try:
req = Requirement(req_as_string)
req = get_or_create_requirement(req_as_string)
except InvalidRequirement:
if os.path.sep in req_as_string:
add_msg = "It looks like a path."
Expand Down Expand Up @@ -386,7 +392,7 @@ def install_req_from_req_string(
user_supplied: bool = False,
) -> InstallRequirement:
try:
req = Requirement(req_string)
req = get_or_create_requirement(req_string)
except InvalidRequirement:
raise InstallationError(f"Invalid requirement: '{req_string}'")

Expand Down
8 changes: 5 additions & 3 deletions src/pip/_internal/resolution/resolvelib/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
)

from pip._vendor.packaging.requirements import InvalidRequirement
from pip._vendor.packaging.requirements import Requirement as PackagingRequirement
from pip._vendor.packaging.specifiers import SpecifierSet
from pip._vendor.packaging.utils import NormalizedName, canonicalize_name
from pip._vendor.resolvelib import ResolutionImpossible
Expand All @@ -38,7 +37,10 @@
from pip._internal.models.link import Link
from pip._internal.models.wheel import Wheel
from pip._internal.operations.prepare import RequirementPreparer
from pip._internal.req.constructors import install_req_from_link_and_ireq
from pip._internal.req.constructors import (
get_or_create_requirement,
install_req_from_link_and_ireq,
)
from pip._internal.req.req_install import (
InstallRequirement,
check_invalid_constraint_type,
Expand Down Expand Up @@ -365,7 +367,7 @@ def find_candidates(
# If the current identifier contains extras, add explicit candidates
# from entries from extra-less identifier.
with contextlib.suppress(InvalidRequirement):
parsed_requirement = PackagingRequirement(identifier)
parsed_requirement = get_or_create_requirement(identifier)
explicit_candidates.update(
self._iter_explicit_candidates_from_base(
requirements.get(parsed_requirement.name, ()),
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/test_req.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from pip._internal.req.constructors import (
_get_url_from_path,
_looks_like_path,
get_or_create_requirement,
install_req_from_editable,
install_req_from_line,
install_req_from_parsed_requirement,
Expand Down Expand Up @@ -658,6 +659,19 @@ def test_parse_editable_local_extras(
)


def test_get_or_create_caching() -> None:
"""test caching of get_or_create requirement"""
teststr = "affinegap==1.10"
from_helper = get_or_create_requirement(teststr)
freshly_made = Requirement(teststr)
# Requirement doesn't have an equality operator (yet) so test
# equality of attribute for list of attributes
for iattr in ["name", "url", "extras", "specifier", "marker"]:
assert getattr(from_helper, iattr) == getattr(freshly_made, iattr)
assert not (get_or_create_requirement(teststr) is Requirement(teststr))
assert get_or_create_requirement(teststr) is get_or_create_requirement(teststr)


def test_exclusive_environment_markers() -> None:
"""Make sure RequirementSet accepts several excluding env markers"""
eq36 = install_req_from_line("Django>=1.6.10,<1.7 ; python_version == '3.6'")
Expand Down