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

Importing humanize is slow #209

Closed
ashwin153 opened this issue Jun 2, 2021 · 9 comments
Closed

Importing humanize is slow #209

ashwin153 opened this issue Jun 2, 2021 · 9 comments

Comments

@ashwin153
Copy link

I have noticed that importing humanize noticeably increases the startup time of my CLI. After doing some importtime analysis, I think the underlying problem is that importing pkg_resources is slow.

Is is it possible to replace the usage of pkg_resources.get_distribution in __init__.py with something that isn't as expensive to import?

@hugovk
Copy link
Collaborator

hugovk commented Jun 2, 2021

An alternative is using importlib.metadata:

https://github.com/pypa/setuptools_scm/#retrieving-package-version-at-runtime

Would you like to test to see if that improves things?

It would be nice to see some numbers comparing both, and against setting a dummy value for the version.

@ashwin153
Copy link
Author

ashwin153 commented Jun 2, 2021

Here are the results of importtime analysis of the three options using Tuna. TLDR pkg_resources is about 3 times slower than importlib_metadata, and importlib_metadata is about 5 times slower than hard-coding the version number.

Constant

constant

importlib_metadata

importlib_metadata

pkg_resources

pkg_resources

@hugovk
Copy link
Collaborator

hugovk commented Jun 3, 2021

Thanks for this!

That's such a big difference, perhaps we could try out the write_to option of setuptools_scm to hardcode it.

https://github.com/pypa/setuptools_scm/#configuration-parameters

Would you like to put together a PR?

@ashwin153
Copy link
Author

Sounds good, will do tomorrow.

@ashwin153
Copy link
Author

ashwin153 commented Jun 3, 2021

Note to self and future: Since the version file that setuptools writes to doesn't exist during development, we'll need to guard the import just like in pypa/setuptools-scm#143 (comment).

@hugovk
Copy link
Collaborator

hugovk commented Jun 4, 2021

Does installing in editiable mode (pip install -e .) generate the version file?

@ashwin153
Copy link
Author

Closed by #227.

@hugovk
Copy link
Collaborator

hugovk commented Oct 6, 2021

Is the new importlib_metadata option good enough compared to a constant?

@ashwin153
Copy link
Author

It is not good enough to consider constant but it is 3 times faster than it was before, which is why I closed this. Happy to reopen if you think that there is more to be done.

@hugovk hugovk closed this as completed Oct 8, 2021
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

No branches or pull requests

2 participants