Skip to content

Conversation

dhellmann
Copy link
Contributor

Importing pkg_resources has a side-effect of scanning every installed
distribution on sys.path to load the metadata, especially the entry
points defined in the packages. This can have a significant
launch-time cost for command line applications when there are a lot of
distributions to scan.

Since cmd2 is only using pkg_resources to find the version of the
installed package, pkg_resources can be replaced with
importlib.metadata. The implementation in the new library is
significantly faster because it goes immediately to the metadata file
for the requested distribution, instead of scanning all of them. There
are also no import-time side-effects.

importlib.metadata is a new standard library module starting with
python 3.8. For earlier versions, a compatible library has been
released to PyPI as 'importlib_metadata'. This change adds the new
dependency with a qualifier so that it is only applied to older
versions of python, and then updates the places that were importing
pkg_resources to look for the different versions of the new library
instead. The documentation configuration is changed to import cmd2
itself to get its version, since the package has to be installed for
the metadata to be available anyway.

Importing pkg_resources has a side-effect of scanning every installed
distribution on sys.path to load the metadata, especially the entry
points defined in the packages. This can have a significant
launch-time cost for command line applications when there are a lot of
distributions to scan.

Since cmd2 is only using pkg_resources to find the version of the
installed package, pkg_resources can be replaced with
importlib.metadata. The implementation in the new library is
significantly faster because it goes immediately to the metadata file
for the requested distribution, instead of scanning all of them. There
are also no import-time side-effects.

importlib.metadata is a new standard library module starting with
python 3.8. For earlier versions, a compatible library has been
released to PyPI as 'importlib_metadata'. This change adds the new
dependency with a qualifier so that it is only applied to older
versions of python, and then updates the places that were importing
pkg_resources to look for the different versions of the new library
instead. The documentation configuration is changed to import cmd2
itself to get its version, since the package has to be installed for
the metadata to be available anyway.

Signed-off-by: Doug Hellmann <doug@doughellmann.com>
@codecov
Copy link

codecov bot commented Jul 12, 2020

Codecov Report

Merging #953 into master will decrease coverage by 0.02%.
The diff coverage is 78.57%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #953      +/-   ##
==========================================
- Coverage   97.72%   97.70%   -0.03%     
==========================================
  Files          19       19              
  Lines        4177     4183       +6     
==========================================
+ Hits         4082     4087       +5     
- Misses         95       96       +1     
Impacted Files Coverage Δ
plugins/ext_test/cmd2_ext_test/__init__.py 81.81% <75.00%> (+6.81%) ⬆️
cmd2/__init__.py 91.66% <83.33%> (+1.19%) ⬆️
cmd2/utils.py 98.00% <0.00%> (-0.25%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6e19fb1...859d45b. Read the comment docs.

Copy link
Member

@tleonhardt tleonhardt left a comment

Choose a reason for hiding this comment

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

@dhellmann Hey Doug, thanks a ton for this fix! I had noticed that on some systems cmd2 had an abnormally large start time, but I had not had the time to get to the bottom of why.

@tleonhardt tleonhardt merged commit aeacd50 into python-cmd2:master Jul 12, 2020
@dhellmann
Copy link
Contributor Author

Thanks for the quick review, @tleonhardt !

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