Author serhiy.storchaka
Recipients brett.cannon, eric.smith, eric.snow, ncoghlan, pitrou, serhiy.storchaka, vstinner
Date 2016-07-24.16:57:07
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
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
Date User Action Args
2016-07-24 16:57:09serhiy.storchakasetrecipients: + serhiy.storchaka, brett.cannon, ncoghlan, pitrou, vstinner, eric.smith, eric.snow
2016-07-24 16:57:09serhiy.storchakasetmessageid: <>
2016-07-24 16:57:09serhiy.storchakalinkissue22557 messages
2016-07-24 16:57:08serhiy.storchakacreate