gh-109653: Improve import preformance of tkinter library with lazy imports#148409
gh-109653: Improve import preformance of tkinter library with lazy imports#148409sharktide wants to merge 9 commits intopython:mainfrom
tkinter library with lazy imports#148409Conversation
|
cc @AlexWaygood |
|
|
||
| import _tkinter # If this fails your Python may not be configured for Tk | ||
| lazy import sys | ||
| lazy import types |
There was a problem hiding this comment.
Looking at the code, I think both sys and types will end up being imported always, because of:
https://github.com/python/cpython/pull/148409/changes#diff-46c8ee2e6d4e02b7a2839d0cabde53331f5ad74fb09fdd6149b70e9d5cfb2cddR5107
https://github.com/sharktide/cpython/blob/f0bd1ebd7c4c6ac7126d7fc8332e59744427d6fc/Lib/tkinter/__init__.py#L2635
At the same time, there are some inline imports that you can convert to lazy syntax: traceback and os
https://github.com/sharktide/cpython/blob/f0bd1ebd7c4c6ac7126d7fc8332e59744427d6fc/Lib/tkinter/__init__.py#L2622
https://github.com/sharktide/cpython/blob/f0bd1ebd7c4c6ac7126d7fc8332e59744427d6fc/Lib/tkinter/__init__.py#L2539
(though you should checj again whether os will end up being imported anyways)
| _magic_re = re.compile(r'([\\{}])') | ||
| _space_re = re.compile(r'([\s])', re.ASCII) | ||
| def _get_magic_re(): | ||
| """Internal function.""" |
There was a problem hiding this comment.
this docstring (and below) doesn't add any info. (you can say that this function hides the re import to make it lazy
| """Internal function.""" |
Similar to #109789 but for Tkinter
EDIT: I can’t generate a direct comparison graph currently, but expect a 20-30% performance increase.