[Python-ideas] combining two threads: switch statements and inline functions

Skip Montanaro skip at pobox.com
Wed Feb 12 06:35:31 CET 2014


On Tue, Feb 11, 2014 at 10:06 PM, Haoyi Li <haoyi.sg at gmail.com> wrote:
> You can write totally dictionaries with inline functions (i.e. lambdas) in them.

Lambdas are not what I think of when I think of inline functions.
Substituting them for named functions gains you nothing. They are just
(very limited) functions you call in the usual fashion, and which have
all the usual overhead you associate with calling Python functions.

I think of inline functions as they exist in C++ (or before that, in
GCC). If you declare a function to be inline, the compiler is free to
inline the body of the function's code at the call point and make the
necessary fix-ups to the prolog and epilog to preserve semantics (as
if it had not been declared inline), but eliminate call overhead, and
much of the stack manipulation.

This works in statically typed languages like C and C++, because at
compile time the compiler knows everything about the types of the
functions arguments and return values, as well as the environment in
which the function and the call point exist. I don't think that kind
of function inlining would be possible in CPython. At minimum, the
compiler simply doesn't know the types of the arguments to the
function call at compile time. Heck, it probably wouldn't even know
(except in the most trivial of circumstances) that any particular
function available is the one to inline. You'd have to look to PyPy
for the necessary tools to perform this feat, and it would be
accomplished at run-time.

Skip


More information about the Python-ideas mailing list