Maybe rewrite small functions like map
in Python.
#131
Replies: 3 comments
-
The idea is definitely worth exploring, though maybe not with |
Beta Was this translation helpful? Give feedback.
-
Though the docs for map() don't mention its type-ness -- it's listed under Builtin Functions and the text just says it returns an iterator. I'm sure it will break someone's code. But maybe specialization can help avoid that. |
Beta Was this translation helpful? Give feedback.
-
Using the code from https://docs.python.org/3/library/functions.html
(Not sure why the
|
Beta Was this translation helpful? Give feedback.
-
Consider
map(func, seq)
, wherefunc
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.compiles to:
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
andFOR_ITER
, the Python version will be about a little faster than the C version iffunc
is a Python function, and a bit slower iffunc
is a builtin function.But that is just guess.
Beta Was this translation helpful? Give feedback.
All reactions