-
-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
Reduce number of modules imported by runpy #85178
Comments
Currently, the runpy module imports many modules. runpy is used by "python3 -m module". I propose to attempt to reduce the number of imports to reduce Python startup time. With my local changes, I reduce Python startup time from 24 ms to 18 ms: Mean +- std dev: [ref] 24.3 ms +- 0.2 ms -> [patch] 18.0 ms +- 0.3 ms: 1.35x faster (-26%) Timing measured by: ./python -m venv env Currently, runpy imports +21 modules:
Example with attached mod.py: $ ./python -m mod
['__main__',
'_abc',
'_codecs',
'_collections',
'_collections_abc',
'_frozen_importlib',
'_frozen_importlib_external',
'_functools',
'_heapq',
'_imp',
'_io',
'_locale',
'_operator',
'_signal',
'_sitebuiltins',
'_sre',
'_stat',
'_thread',
'_warnings',
'_weakref',
'_weakrefset',
'abc',
'builtins',
'codecs',
'collections',
'collections.abc',
'contextlib',
'copyreg',
'encodings',
'encodings.aliases',
'encodings.ascii',
'encodings.latin_1',
'encodings.utf_8',
'enum',
'functools',
'genericpath',
'heapq',
'importlib',
'importlib._bootstrap',
'importlib._bootstrap_external',
'importlib.abc',
'importlib.machinery',
'importlib.util',
'io',
'itertools',
'keyword',
'marshal',
'operator',
'os',
'os.path',
'pkgutil',
'posix',
'posixpath',
're',
'reprlib',
'runpy',
'site',
'sre_compile',
'sre_constants',
'sre_parse',
'stat',
'sys',
'time',
'types',
'typing',
'typing.io',
'typing.re',
'warnings',
'weakref',
'zipimport']
Total 70 |
My local changes removed the following imports from runpy:
|
Oops sorry, that's with my local changes! Currently, runpy imports not less than 37 modules:
|
See also bpo-40275: "test.support has way too many imports". |
I created 3 PRs. I have a few more local branches to avoid types and itertools imports. I may create PRs for these ones as well. |
I close the issue. Making more imports lazy doesn't bring much benefit. -- With the 3 changes, runpy now imports 55 modules, instead of 70. The startup time is 1.3x faster, 18 ms instead of 24 ms: Mean +- std dev: [before] 23.7 ms +- 0.4 ms -> [after] 17.8 ms +- 0.6 ms: 1.33x faster (-25%) -- Avoiding itertools and types doesn't bring much benefit: Mean +- std dev: [after] 17.8 ms +- 0.6 ms -> [WIP] 17.2 ms +- 0.4 ms: 1.03x faster (-3%) |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: