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

spir denis.spir at gmail.com
Wed Feb 12 11:12:37 CET 2014


On 02/12/2014 06:35 AM, Skip Montanaro wrote:
> 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.

What the compiler need, more than the type, is the "weight" (memory size); so as 
to be able to (statically) reserve slots in the stack (or register, wherever). 
[In general, static efficiency comes from knowing the weight of values, also at 
times their structures (say globally their "formats"), rather than their types.]

At first sight, it seems to me that python's optional and "default" args may 
also be a big obstacle to inlining. (I wonder how they're done for regular func 
def and cal, by the way.)

d


More information about the Python-ideas mailing list