-
Notifications
You must be signed in to change notification settings - Fork 52
Description
Consider map(func, seq)
, where func
is a Python function.
This calls from Python into a builtin function, and then calls back into Python.
Once we have fast Python to Python calls, it might be beneficial to convert map
and similar functions into Python.
E.g. consider the common case of a single iterable. map(f, i)
is equivalent to (f(x) for x in i)
The above would allow us to avoid C calls, with the possibility of a speedup within the interpreter and the possibility of a larger speedup with a hypothetical JIT.
For maximum efficiency we might want to write map
in Python.
def map1(func, seq):
for x in seq:
yield func(x)
compiles to:
0 GEN_START 0
2 2 LOAD_FAST 1 (seq)
4 GET_ITER
>> 6 FOR_ITER 7 (to 22)
8 STORE_FAST 2 (x)
3 10 LOAD_FAST 0 (func)
12 LOAD_FAST 2 (x)
14 CALL_FUNCTION 1
16 YIELD_VALUE
18 POP_TOP
20 JUMP_ABSOLUTE 3 (to 6)
2 >> 22 LOAD_CONST 0 (None)
24 RETURN_VALUE
Which has an loop of 8 instructions (7 allowing for superinstructions).
The question is:
Does the additional interpreter overhead of the dispatch cost more or less than the call to a C function and the call back into the interpreter?
My guess is that it with specialized CALL_FUNCTION
and FOR_ITER
, the Python version will be about a little faster than the C version if func
is a Python function, and a bit slower if func
is a builtin function.
But that is just guess.