Skip to content

Defer tempfile import to sites of use #328

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Sachaa-Thanasius
Copy link

@Sachaa-Thanasius Sachaa-Thanasius commented Apr 7, 2025

Part of #326.

tempfile is a relatively heavy import that's only used in a few routines, and they aren't always called when using importlib_resources, especially in the common case of as_file() being passed a pathlib.Path object. Thus, I think it's worthwhile to defer importing it until it's actually used.

This PR effectively inlines the one function using tempfile at module-level scope to avoid an import-time dependency on tempfile. It then "inlines" the necessary import in the two functions that use it.

Based on local testing, doing so improves the upfront import time of importlib_resourcesby about ~30% on supported CPython versions and ~50% on PyPy3.10.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
…import time.
@Sachaa-Thanasius Sachaa-Thanasius changed the title Defer tempfile import to sites of use. Defer tempfile import to sites of use Apr 7, 2025
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.

None yet

1 participant