Skip to content

Commit

Permalink
perf: cache RepositoryPool.package() in Provider
Browse files Browse the repository at this point in the history
  • Loading branch information
radoering committed Jan 26, 2025
1 parent 288b435 commit ddcdcde
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 35 deletions.
5 changes: 1 addition & 4 deletions src/poetry/inspection/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,7 @@ def load(cls, data: dict[str, Any]) -> PackageInfo:
return cls(cache_version=cache_version, **data)

def to_package(
self,
name: str | None = None,
extras: list[str] | None = None,
root_dir: Path | None = None,
self, name: str | None = None, root_dir: Path | None = None
) -> Package:
"""
Create a new `poetry.core.packages.package.Package` instance using metadata from
Expand Down
6 changes: 4 additions & 2 deletions src/poetry/puzzle/provider.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import functools
import itertools
import logging
import re
Expand Down Expand Up @@ -148,6 +149,8 @@ def __init__(
reverse=True,
)

self._get_package_from_pool = functools.cache(self._pool.package)

@property
def pool(self) -> RepositoryPool:
return self._pool
Expand Down Expand Up @@ -446,10 +449,9 @@ def complete_package(
else:
dependency_package = DependencyPackage(
dependency,
self._pool.package(
self._get_package_from_pool(
package.pretty_name,
package.version,
extras=list(dependency.extras),
repository_name=dependency.source_name,
),
)
Expand Down
7 changes: 1 addition & 6 deletions src/poetry/repositories/abstract_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,4 @@ def find_packages(self, dependency: Dependency) -> list[Package]: ...
def search(self, query: str | list[str]) -> list[Package]: ...

@abstractmethod
def package(
self,
name: str,
version: Version,
extras: list[str] | None = None,
) -> Package: ...
def package(self, name: str, version: Version) -> Package: ...
9 changes: 2 additions & 7 deletions src/poetry/repositories/cached_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,7 @@ def get_release_info(self, name: NormalizedName, version: Version) -> PackageInf

return PackageInfo.load(cached)

def package(
self,
name: str,
version: Version,
extras: list[str] | None = None,
) -> Package:
def package(self, name: str, version: Version) -> Package:
return self.get_release_info(canonicalize_name(name), version).to_package(
name=name, extras=extras
name=name
)
6 changes: 2 additions & 4 deletions src/poetry/repositories/legacy_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ def __init__(
pool_size=pool_size,
)

def package(
self, name: str, version: Version, extras: list[str] | None = None
) -> Package:
def package(self, name: str, version: Version) -> Package:
"""
Retrieve the release information.
Expand All @@ -65,7 +63,7 @@ def package(

return self._packages[index]
except ValueError:
package = super().package(name, version, extras)
package = super().package(name, version)
package._source_type = "legacy"
package._source_url = self._url
package._source_reference = self.name
Expand Down
4 changes: 1 addition & 3 deletions src/poetry/repositories/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,7 @@ def __len__(self) -> int:
def find_links_for_package(self, package: Package) -> list[Link]:
return []

def package(
self, name: str, version: Version, extras: list[str] | None = None
) -> Package:
def package(self, name: str, version: Version) -> Package:
canonicalized_name = canonicalize_name(name)
for package in self.packages:
if canonicalized_name == package.name and package.version == version:
Expand Down
12 changes: 3 additions & 9 deletions src/poetry/repositories/repository_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,20 +152,14 @@ def remove_repository(self, name: str) -> RepositoryPool:
return self

def package(
self,
name: str,
version: Version,
extras: list[str] | None = None,
repository_name: str | None = None,
self, name: str, version: Version, repository_name: str | None = None
) -> Package:
if repository_name:
return self.repository(repository_name).package(
name, version, extras=extras
)
return self.repository(repository_name).package(name, version)

for repo in self.repositories:
try:
return repo.package(name, version, extras=extras)
return repo.package(name, version)
except PackageNotFoundError:
continue
raise PackageNotFoundError(f"Package {name} ({version}) not found.")
Expand Down

0 comments on commit ddcdcde

Please sign in to comment.