Repository navigation
Improve import time of various stdlib modules #109653
Description
Activity
- addedtype-featureA feature request or enhancementA feature request or enhancementperformancePerformance or resource usagePerformance or resource usage3.13only security fixesonly security fixes
on Sep 21, 2023 - added a commit that references this issue
on Sep 23, 2023 - changed the title
[-]Improve import time of `typing.py`[/-][+]Improve import time of various stdlib modules[/+]on 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.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.
Reacted by Alex Waygood, Itamar Oren, Jelle Zijlstra and Eric V. SmithSome may be false positives due to needing an import's side effects ...
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 :)Reacted by Hugo van KemenadeAn idea: review the output of a linter for "unused" imports.
FWIW, I can only find one in the stdlib that I'm 100% confident with, using ruff/pycln:
Line 20 in e8be0c9
import warnings 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.Reacted by Itamar OrenReacted by Hugo van Kemenade- added a commit that references this issue
on Sep 23, 2023 This is also interesting:
dataclassescopiesrecursive_reprto avoid a dependency:Lines 248 to 266 in 51863b7
# This function's logic is copied from "recursive_repr" function in # reprlib module to avoid dependency. def _recursive_repr(user_function): # Decorator to make a repr function return "..." for a recursive # call. repr_running = set() @functools.wraps(user_function) def wrapper(self): key = id(self), _thread.get_ident() if key in repr_running: return '...' repr_running.add(key) try: result = user_function(self) finally: repr_running.discard(key) return result return wrapper - But, it imports
functools:Line 7 in 51863b7
import functools - And it imports
reprlib.recursive_repr:Line 20 in 51863b7
from reprlib import recursive_repr
Reacted by Alex Waygood43 remaining items
- added a commit that references this issue
on May 7, 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.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.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?
Reacted by layday and Hugo van KemenadePlease link the new issue here so we all can start following that as well!
- Reacted by Daniel Hollas and Ofek Lev
- added 7 commits that reference this issue
on Sep 2, 2024
Feature or enhancement
Proposal:
As noted in https://discuss.python.org/t/deferred-computation-evalution-for-toplevels-imports-and-dataclasses/34173,
typingisn'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:
https://discuss.python.org/t/deferred-computation-evalution-for-toplevels-imports-and-dataclasses/34173
Linked PRs
typing.py: improve import time by creating soft-deprecated members on demand #109651enumimport time by avoiding import offunctools#109789Lib/directory #109803typesinfunctools#109804recursive_reprindataclasses#109822email.utils#109824importlib.metadata._adapters#109829random#110221random(GH-110221) #110247warningsin several modules #110286loggingby lazy loadingtraceback#112995tkinterlibrary with lazy imports #148409