[issue22557] Local import is too slow
Serhiy Storchaka
report at bugs.python.org
Sun Jul 24 12:57:09 EDT 2016
Serhiy Storchaka added the comment:
Seems not all such easy. Looking in sys.module is not enough, we should check __spec__._initializing.
Following patch moves optimizations to PyImport_ImportModuleLevelObject (C implementation of standard __import__). Main optimizations:
1. PyImport_ImportModuleLevelObject is called directly if builtins.__import__ is standard __import__.
2. Import lock is not acquired for looking up in sys.modules and other operations. Some of these operations are atomic in C (guarded by GIL), others are used only for optimization and race condition can cause only insignificant slow down.
3. Avoided creating empty dict for globals, looking up __package__ and __spec__ if they are not needed.
4. Saving standard __import__ in interpreter state.
Microbenchmarking results:
$ ./python -m timeit 'import os'
Unpatched: 1000000 loops, best of 3: 0.845 usec per loop
Patched: 1000000 loops, best of 3: 0.338 usec per loop
$ ./python -m timeit 'import os.path'
Unpatched: 100000 loops, best of 3: 2.07 usec per loop
Patched: 1000000 loops, best of 3: 0.884 usec per loop
$ ./python -m timeit 'from os import path'
Unpatched: 100000 loops, best of 3: 3.7 usec per loop
Patched: 100000 loops, best of 3: 2.77 usec per loop
----------
Added file: http://bugs.python.org/file43869/faster_import_6.patch
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue22557>
_______________________________________
More information about the Python-bugs-list
mailing list