Repository navigation
Closed
Labels
3.13only security fixesonly security fixesperformancePerformance or resource usagePerformance or resource usagetopic-typingtype-featureA feature request or enhancementA feature request or enhancement
Description
Feature or enhancement
Proposal:
As noted in https://discuss.python.org/t/deferred-computation-evalution-for-toplevels-imports-and-dataclasses/34173, typing isn't the slowest stdlib module in terms of import time, but neither is it one of the quickest. We should speed it up, if possible.
Links to previous discussion of this feature:
Linked PRs
- gh-109653:
typing.py: improve import time by creating soft-deprecated members on demand #109651 - gh-109653: Improve
enumimport time by avoiding import offunctools#109789 - gh-109653: Remove unused imports in the
Lib/directory #109803 - gh-109653: Avoid a top-level import of
typesinfunctools#109804 - gh-109653: Just import
recursive_reprindataclasses#109822 - gh-109653: Improve the import time of
email.utils#109824 - GH-109653: Defer import of
importlib.metadata._adapters#109829 - gh-109653: Fix py312 regression in the import time of
random#110221 - [3.12] gh-109653: Fix py312 regression in the import time of
random(GH-110221) #110247 - gh-109653: Defer importing
warningsin several modules #110286 - gh-109653: Improve import time of
loggingby lazy loadingtraceback#112995 - gh-109653: Speedup import of threading module #114509
- gh-109653: Improve import time of importlib.metadata / email.utils #114664
- gh-109653: Reduce import overhead of uuid module on Linux #115160
- gh-109653: What's new: Note improved import times for several stdlib modules in Python 3.13 #118697
- gh-154675: Improve import performance of
tkinterlibrary with lazy imports #148409
Reacted by Daniel Hollas, Ofek Lev, Erlend E. Aasland, Hugo van Kemenade, Pathompong Kwangtong and Brett Holman
Reacted by Ofek Lev, Hugo van Kemenade, Pathompong Kwangtong and EDM115
Reacted by Daniel Hollas, Ofek Lev, Erlend E. Aasland, Hugo van Kemenade and Pathompong Kwangtong
Metadata
Metadata
Assignees
Labels
3.13only security fixesonly security fixesperformancePerformance or resource usagePerformance or resource usagetopic-typingtype-featureA feature request or enhancementA feature request or enhancement
Activity
gh-109653: `typing.py`: improve import time by creating soft-deprecat…
[-]Improve import time of `typing.py`[/-][+]Improve import time of various stdlib modules[/+]AlexWaygood commentedon Sep 23, 2023
Retitling this issue to reflect a change of scope: part of the reason for the slow import time of
typingis the slow import time of some of the modules it imports. There may be low-hanging fruit in some other stdlib modules as well.pythongh-109653: Improve `enum` import time by avoiding import of `fu…
hugovk commentedon Sep 23, 2023
An idea: review the output of a linter for "unused" imports.
Some may be false positives due to needing an import's side effects, but I expect there will be some that are no longer needed.
itamaro commentedon Sep 23, 2023
How do you feel about making an intentional effort to get rid of such reliance on side effects in the stdlib?
Explicit is better than implicit :)
AlexWaygood commentedon Sep 23, 2023
FWIW, I can only find one in the stdlib that I'm 100% confident with, using ruff/pycln:
cpython/Lib/asyncio/tasks.py
Line 20 in e8be0c9
I think this is because Victor went through them all fairly recently, in #105411. There are a fair few in
Lib/test/, though. I'll file a PR in a bit.gh-109653: Improve `enum` import time by avoiding import of `functool…
sobolevn commentedon Sep 24, 2023
This is also interesting:
dataclassescopiesrecursive_reprto avoid a dependency:cpython/Lib/dataclasses.py
Lines 248 to 266 in 51863b7
functools:cpython/Lib/dataclasses.py
Line 7 in 51863b7
reprlib.recursive_repr:cpython/Lib/functools.py
Line 20 in 51863b7
sobolevn commentedon Sep 24, 2023
One more interesting case:
functoolssays thatimport typesis deferred tosingledispatch():cpython/Lib/functools.py
Line 19 in 51863b7
typesmodule is imported:cpython/Lib/functools.py
Line 22 in 51863b7
pythongh-109653: Avoid a top-level import of `types` in `functools`
43 remaining items
pythongh-109653: What's new: Note improved import times for several s…
gh-109653: What's new: Note improved import times for several stdlib …
layday commentedon May 8, 2024
I believe there are futher improvements to be made here - how would you like to handle these? For example,
importlib.metadataimportszipfileat the top for a function that won't be called in the vast majority of cases. It also importsimportlib.abc, which in turn importsimportlib.resources, to subclass an ABC with a single, non-abstract method - I assume redefining the method inimportlib.metadatawould be harmless.AlexWaygood commentedon May 8, 2024
Sure -- I felt like the low-hanging fruit had been tackled here, and this issue hadn't seen much activity in quite a while, so I felt like it was a good time to close. But more improvements are obviously always welcome. Maybe open a new issue if you have some more targeted improvements you want to propose?
ofek commentedon May 8, 2024
Please link the new issue here so we all can start following that as well!
layday commentedon May 8, 2024
#118761
pythongh-109653: What's new: Note improved import times for several s…
pythongh-109653: `typing.py`: improve import time by creating soft-de…
pythongh-109653: Improve `enum` import time by avoiding import of `fu…
pythongh-109653: Remove unused imports in the `Lib/` directory (pytho…
pythongh-109653: Avoid a top-level import of `types` in `functools` (p…
pythongh-109653: Reduce the import time of `random` by 60% (python#11…
pythongh-109653: Defer importing `warnings` in several modules (pytho…
pythongh-109653: Improve the import time of `email.utils` (python#109824