This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: use FASTCALL for __import__ builtin
Type: performance Stage: resolved
Components: Interpreter Core Versions: Python 3.11
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Dennis Sweeney, Mark.Shannon, kumaraditya, serhiy.storchaka
Priority: normal Keywords:

Created on 2022-03-08 07:52 by kumaraditya, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 31752 merged kumaraditya, 2022-03-08 07:52
Messages (7)
msg414729 - (view) Author: Kumar Aditya (kumaraditya) * (Python triager) Date: 2022-03-08 07:52
Use FASTCALL for __import__ builtin.

Benchmark:
--------------------------------------------------------------------------
import pyperf

runner = pyperf.Runner()
runner.timeit(name="bench __import__",
              stmt="__import__('asyncio')"
------------------------------------------------------------------------

Result:

Mean +- std dev: [base] 191 ns +- 16 ns -> [patch] 112 ns +- 11 ns: 1.71x faster
msg414735 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2022-03-08 09:11
__import__() usually is not called directly, and in common case (when it is not overridden) the overhead of the call is avoided completely in the import statement.

And in non-trivial case, it would only save 80 microseconds if you import 1000 modules. I don't think there are many programs which import more than 1000 modules.
msg414740 - (view) Author: Kumar Aditya (kumaraditya) * (Python triager) Date: 2022-03-08 09:57
The PR uses argument clinic and not hand written parsing code, which in turn is faster.
msg414744 - (view) Author: Kumar Aditya (kumaraditya) * (Python triager) Date: 2022-03-08 10:08
Occurrence of __import__ calls in stdlib:

grep __import__ Lib/*/*.py | wc -l

28

It is common to import directly via __import__ outside the stdlib too.
msg414751 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2022-03-08 12:46
The recommended way is to use importlib.import_module().
msg414755 - (view) Author: Mark Shannon (Mark.Shannon) * (Python committer) Date: 2022-03-08 14:04
Serhiy, what is the advantage of __import__ being slower?

Not counting the argument clinic generated code, the PR doesn't add any code and improves the docstring.
msg414788 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2022-03-09 08:46
What is the advantage of not touching a stable code?

It was never a performance critical part of the code. This is why it avoided multiple previous optimizations. It is still use PyArg_ParseTupleAndKeywords().

Of course now, when optimization is provided by Argument Clinic and its code is stable enough, we can do this. But performance was never a concern here.
History
Date User Action Args
2022-04-11 14:59:57adminsetgithub: 91109
2022-03-12 10:26:38kumaradityasetstatus: open -> closed
resolution: fixed
stage: resolved
2022-03-09 08:46:05serhiy.storchakasetmessages: + msg414788
2022-03-08 14:04:22Mark.Shannonsetmessages: + msg414755
2022-03-08 12:46:36serhiy.storchakasetmessages: + msg414751
2022-03-08 10:08:35kumaradityasetmessages: + msg414744
2022-03-08 09:57:40kumaradityasetmessages: + msg414740
2022-03-08 09:11:31serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg414735
2022-03-08 07:52:36kumaradityacreate