summaryrefslogtreecommitdiffstats
path: root/Lib/importlib/_bootstrap.py
diff options
context:
space:
mode:
authorGermán Méndez Bravo <kronuz@fb.com>2021-08-12 18:23:29 (GMT)
committerGitHub <noreply@github.com>2021-08-12 18:23:29 (GMT)
commit03648a2a91f9f1091cd21bd4cd6ca092ddb25640 (patch)
tree0f7f969f440abdce580317be0ba70aaf5216bcdc /Lib/importlib/_bootstrap.py
parent953d27261e455066b17a106d9e07bab3bf12983b (diff)
downloadcpython-03648a2a91f9f1091cd21bd4cd6ca092ddb25640.zip
cpython-03648a2a91f9f1091cd21bd4cd6ca092ddb25640.tar.gz
cpython-03648a2a91f9f1091cd21bd4cd6ca092ddb25640.tar.bz2
bpo-43392: Optimize repeated calls to `__import__()` (GH-24735)
Implements a two steps check in `importlib._bootstrap._find_and_load()` to avoid locking when the module has been already imported and it's ready. --- Using `importlib.__import__()`, after this, does show a big difference: Before: ``` $ ./python -c 'import timeit; print(timeit.timeit("__import__(\"timeit\")", setup="from importlib import __import__"))' 15.92248619502061 ``` After: ``` $ ./python -c 'import timeit; print(timeit.timeit("__import__(\"timeit\")", setup="from importlib import __import__"))' 1.206068897008663 ``` ---
Diffstat (limited to 'Lib/importlib/_bootstrap.py')
-rw-r--r--Lib/importlib/_bootstrap.py21
1 files changed, 16 insertions, 5 deletions
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
index f8b77e3..e64f7ad 100644
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -1032,17 +1032,28 @@ _NEEDS_LOADING = object()
def _find_and_load(name, import_):
"""Find and load the module."""
- with _ModuleLockManager(name):
- module = sys.modules.get(name, _NEEDS_LOADING)
- if module is _NEEDS_LOADING:
- return _find_and_load_unlocked(name, import_)
+
+ # Optimization: we avoid unneeded module locking if the module
+ # already exists in sys.modules and is fully initialized.
+ module = sys.modules.get(name, _NEEDS_LOADING)
+ if (module is _NEEDS_LOADING or
+ getattr(getattr(module, "__spec__", None), "_initializing", False)):
+ with _ModuleLockManager(name):
+ module = sys.modules.get(name, _NEEDS_LOADING)
+ if module is _NEEDS_LOADING:
+ return _find_and_load_unlocked(name, import_)
+
+ # Optimization: only call _bootstrap._lock_unlock_module() if
+ # module.__spec__._initializing is True.
+ # NOTE: because of this, initializing must be set *before*
+ # putting the new module in sys.modules.
+ _lock_unlock_module(name)
if module is None:
message = ('import of {} halted; '
'None in sys.modules'.format(name))
raise ModuleNotFoundError(message, name=name)
- _lock_unlock_module(name)
return module