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 the result of loading pyproject.toml file #3342

Closed
wants to merge 1 commit into from

Conversation

ericwb
Copy link
Contributor

@ericwb ericwb commented May 19, 2024

This change adds the functools cache decorator to avoid repeated calls to _get_package_config, which results in IO hits to read a file.

Closes #3341

All Submissions:

  • Have you followed the guidelines stated in CONTRIBUTING.md file?
  • Have you checked to ensure there aren't any other open Pull Requests for the desired changed?

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)

New Feature Submission:

  • Does your submission pass the tests?
  • Have you linted your code locally prior to submission?

Changes To Core Features:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your core changes, as applicable?
  • Have you successfully ran tests with your changes locally?

@ericwb
Copy link
Contributor Author

ericwb commented May 19, 2024

Oops, functools.cache is only available in Python 3.9. Need to switch to lru_cache

This change adds the functools cache decorator to avoid repeated
calls to _get_package_config, which results in IO hits to read
a file.

Closes reflex-dev#3341

Signed-off-by: Eric Brown <eric_wade_brown@yahoo.com>
@@ -70,6 +71,7 @@ def _create_package_config(module_name: str, package_name: str):
)


@lru_cache(maxsize=None)
def _get_package_config(exit_on_fail: bool = True) -> dict:
Copy link
Contributor

Choose a reason for hiding this comment

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

In _validate_project_info we interactively ask for info, and may end up updating the pyproject.toml, making this caching invalid - since afterwards we try to get the toml in _collect_details_for_gallery.

I'm thinking it may be okay as is originally - the read operations should be pretty fast, and will ensure we're always reading the up to date config. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Possibly. Probably the best thing to do next is write up some unit tests for this module. I do feel a bit uncomfortable changing code that I only tested manually.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The pyproject.toml file is redundantly loaded at least 3 times with no changes until the caching would be invalidated. This code could probably use some heavy refactoring, but I'll leave it for now.

@ericwb ericwb closed this May 29, 2024
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.

custom_components._get_package_config called numerous times
2 participants