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

perf: cache RepositoryPool.package() in Provider #10114

Merged
merged 1 commit into from
Jan 26, 2025

Conversation

radoering
Copy link
Member

@radoering radoering commented Jan 26, 2025

Pull Request Check List

  • Added tests for changed code.
  • Updated documentation for changed code.

This should be safe because we do not change the RepositoryPool while resolving and the returned package is used read-only.

Testing with the pyproject.toml from #9956 (comment):

poetry version lock (--regenerate) lock (--no-update)
main (with python-poetry/poetry-core#828) 73 s 11.5 s
PR 52 s 5.7 s

Summary by Sourcery

Cache the RepositoryPool.package() method in the Provider class to improve performance during dependency resolution.

Enhancements:

  • Cache package lookups to improve performance.

Tests:

  • Updated tests for the changes.

Copy link

sourcery-ai bot commented Jan 26, 2025

Reviewer's Guide by Sourcery

This pull request introduces a performance optimization by caching the RepositoryPool.package() method in the Provider class. This change avoids redundant calls to the method, resulting in faster dependency resolution.

Sequence diagram showing the optimized package resolution flow

sequenceDiagram
    participant Provider
    participant Cache as Function Cache
    participant RepositoryPool
    participant Repository

    Provider->>Cache: Check cached package
    alt Cache hit
        Cache-->>Provider: Return cached package
    else Cache miss
        Provider->>RepositoryPool: package(name, version)
        RepositoryPool->>Repository: package(name, version)
        Repository-->>RepositoryPool: Return package
        RepositoryPool-->>Provider: Return package
        Provider->>Cache: Store in cache
    end
Loading

Class diagram showing the modified package-related classes

classDiagram
    class RepositoryPool {
        -repositories: list
        +package(name: str, version: Version, repository_name: str|None): Package
        +repository(name: str): Repository
    }
    class Provider {
        -_pool: RepositoryPool
        -_get_package_from_pool: cached_function
        +complete_package(dependency: Dependency, package: Package): DependencyPackage
    }
    class AbstractRepository {
        <<abstract>>
        +package(name: str, version: Version): Package
    }
    class Repository {
        +package(name: str, version: Version): Package
    }
    class CachedRepository {
        +package(name: str, version: Version): Package
    }
    class LegacyRepository {
        +package(name: str, version: Version): Package
    }

    AbstractRepository <|-- Repository
    Repository <|-- CachedRepository
    Repository <|-- LegacyRepository
    Provider o-- RepositoryPool
    note for Provider "Added caching for _pool.package()"
    note for RepositoryPool "Simplified package() method
Removed extras parameter"
Loading

File-Level Changes

Change Details Files
Cache RepositoryPool.package() in Provider
  • Added _get_package_from_pool cached method using functools.cache to cache calls to self._pool.package.
  • Replaced direct calls to self._pool.package with calls to self._get_package_from_pool.
src/poetry/puzzle/provider.py
Removed extras parameter from RepositoryPool.package() and related methods
  • Removed the extras parameter from the package method in RepositoryPool.
  • Removed the extras parameter from the package method in CachedRepository.
  • Removed the extras parameter from the package method in AbstractRepository.
  • Removed the extras parameter from the package method in LegacyRepository.
  • Removed the extras parameter from the to_package method in PackageInfo.
  • Removed the extras parameter from the package method in Repository.
src/poetry/repositories/repository_pool.py
src/poetry/repositories/cached_repository.py
src/poetry/repositories/abstract_repository.py
src/poetry/repositories/legacy_repository.py
src/poetry/inspection/info.py
src/poetry/repositories/repository.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @radoering - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Please add tests to verify the caching behavior and ensure removing the extras parameter doesn't cause any regressions.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@abn abn merged commit 603646c into python-poetry:main Jan 26, 2025
53 checks passed
@radoering radoering mentioned this pull request Feb 9, 2025
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants