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 parsed requirements #828

Merged
merged 1 commit into from
Jan 26, 2025

Conversation

radoering
Copy link
Member

@radoering radoering commented Jan 26, 2025

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

Caching the requirements should be safe because we use them read-only.

Testing with the pyproject.toml from python-poetry/poetry#9956 (comment):

poetry version lock (--regenerate) lock (--no-update)
main 98 s 17.5 s
PR 73 s 11.5 s

Summary by Sourcery

Cache parsed requirement strings to improve performance, especially when regenerating lock files.

Enhancements:

  • Cache requirement strings using functools.cache.

Tests:

  • Added tests to verify the caching behavior and ensure there are no regressions.

Copy link

sourcery-ai bot commented Jan 26, 2025

Reviewer's Guide by Sourcery

This pull request introduces caching for parsed requirements using the functools.cache decorator. This change aims to improve performance by avoiding redundant parsing of requirement strings. The caching mechanism is implemented in the parse_requirement function, which is then used in create_from_pep_508 and _make_file_or_dir_dep.

Sequence diagram for cached requirement parsing

sequenceDiagram
    participant Client
    participant Parser
    participant Cache

    Note over Parser: New caching mechanism

    Client->>Parser: parse_requirement(requirement_string)
    alt Cache hit
        Parser->>Cache: Check cache
        Cache-->>Parser: Return cached Requirement
        Parser-->>Client: Return cached Requirement
    else Cache miss
        Parser->>Cache: Check cache
        Cache-->>Parser: Not found
        Parser->>Parser: Create new Requirement
        Parser->>Cache: Store in cache
        Parser-->>Client: Return new Requirement
    end
Loading

Class diagram for requirement parsing changes

classDiagram
    class Requirement {
        +str name
        +str url
        +Sequence[str] extras
        +str constraint
        +__init__(requirement_string: str)
        +__str__()
        +__repr__()
    }

    class RequirementParser {
        +@cache parse_requirement(requirement_string: str)
    }

    RequirementParser ..> Requirement : creates

    note for RequirementParser "New cached parser function"
Loading

File-Level Changes

Change Details Files
Implemented caching for parsed requirements.
  • Added a parse_requirement function decorated with @functools.cache to cache parsed requirements.
  • Modified the Requirement class to include type hints for extras.
src/poetry/core/version/requirements.py
Utilized the cached requirement parser.
  • Replaced direct Requirement instantiation with calls to parse_requirement in create_from_pep_508.
  • Updated the type hint for extras in _make_file_or_dir_dep to Iterable[str].
src/poetry/core/packages/dependency.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 and they look great!

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 afaa128 into python-poetry:main Jan 26, 2025
18 checks passed
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