python / importlib_metadata

Library to access metadata for Python packages
https://importlib-metadata.readthedocs.io
Apache License 2.0
127 stars 82 forks source link

Defer import of json and platform #503

Closed danielhollas closed 2 months ago

danielhollas commented 3 months ago

Staged on top of #502

platform and json modules take together around 1.5ms to import, around 10% of total import time. Since they are both used in a single place it probably makes sense to inline their import.

After this PR, this is an output of python -Ximporttime -c "import importlib_metadata" visualized with tuna using cpython main branch.

image

jaraco commented 2 months ago

Attempting to disentangle the performance impacts of json and platform:

 importlib_metadata main 🐚 .tox/py/bin/python -Ximporttime -c "import importlib_metadata" 2>1 | grep -E r'platform|\sjson$|importlib_metadata$'
import time:        89 |        800 |   json
import time:       221 |        251 |     platform
import time:       636 |      13692 | importlib_metadata
 importlib_metadata main 🐚 800/13692
0.05842827928717499
 importlib_metadata main 🐚 251/13692
0.018331872626351154

I see json accounts for ~6% of the startup and platform under 2% (on my machine under Python 3.12).