Reduce number of modules imported by runpy #85178
Copy link
Copy link
Closed
Labels
3.10 (EOL)end of lifeend of lifestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
Description
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:
assignee = None
closed_at = <Date 2020-06-17.21:36:12.127>
created_at = <Date 2020-06-17.13:58:21.130>
labels = ['library', '3.10']
title = 'Reduce number of modules imported by runpy'
updated_at = <Date 2020-06-17.23:20:57.312>
user = 'https://github.com/vstinner'bugs.python.org fields:
activity = <Date 2020-06-17.23:20:57.312>
actor = 'vstinner'
assignee = 'none'
closed = True
closed_date = <Date 2020-06-17.21:36:12.127>
closer = 'vstinner'
components = ['Library (Lib)']
creation = <Date 2020-06-17.13:58:21.130>
creator = 'vstinner'
dependencies = []
files = ['49240']
hgrepos = []
issue_num = 41006
keywords = ['patch']
message_count = 12.0
messages = ['371741', '371742', '371746', '371750', '371761', '371765', '371766', '371774', '371776', '371777', '371781', '371782']
nosy_count = 2.0
nosy_names = ['vstinner', 'shihai1991']
pr_nums = ['20938', '20939', '20940', '20953', '20954', '20955']
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue41006'
versions = ['Python 3.10']Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
3.10 (EOL)end of lifeend of lifestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
Activity
vstinner commentedon Jun 17, 2020
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
python -m pyperf command -v -o patch.json -- env/bin/python -m empty
Currently, runpy imports +21 modules:
Example with attached mod.py:
vstinner commentedon Jun 17, 2020
My local changes removed the following imports from runpy:
vstinner commentedon Jun 17, 2020
Oops sorry, that's with my local changes!
Currently, runpy imports not less than 37 modules:
vstinner commentedon Jun 17, 2020
See also bpo-40275: "test.support has way too many imports".
vstinner commentedon Jun 17, 2020
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.
vstinner commentedon Jun 17, 2020
New changeset 7824cc0 by Victor Stinner in branch 'master':
bpo-41006: collections imports lazily heap (GH-20940)
7824cc0
vstinner commentedon Jun 17, 2020
New changeset 98ce7b1 by Victor Stinner in branch 'master':
bpo-41006: pkgutil imports lazily re (GH-20939)
98ce7b1
vstinner commentedon Jun 17, 2020
New changeset 9e09849 by Victor Stinner in branch 'master':
bpo-41006: importlib.util no longer imports typing (GH-20938)
9e09849
vstinner commentedon Jun 17, 2020
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%)
vstinner commentedon Jun 17, 2020
New changeset 4c18fc8 by Victor Stinner in branch 'master':
bpo-41006: Document the runpy optimization (GH-20953)
4c18fc8
vstinner commentedon Jun 17, 2020
New changeset 1bf7959 by Victor Stinner in branch 'master':
bpo-41006: Remove init_sys_streams() hack (GH-20954)
1bf7959
vstinner commentedon Jun 17, 2020
New changeset 2c2a4f3 by Victor Stinner in branch 'master':
bpo-41006: What's New: less => fewer modules (GH-20955)
2c2a4f3