Hi Tobias,

On Wed, Dec 08, 2021 at 05:53:56AM -0000, TobiasHT  wrote:

> The Python community has a 5 year plan to push the limit of speed in 
> Python. One of the things that reduces Python execution speed is 
> calling methods or functions that are not in the nearest scope.

Do you have some benchmarks that the cost of the global lookup is a 
significant slowdown?

> My suggestion is to introduce inline functions just as they are in C.
[...]
> This is just a suggestion, what do you guys think about it.

The first thing I think of this is that I don't know what it does. Let's 
pretend that we're Python programmers, not C programmers, and so might 
not know what the C "inline" keyword does. Can you explain, using Python 
terms, how an inline function and a regular function will be different?

You say that they are:

"created in the scope of the function that is calling it at parsetime."

but I'm not sure I understand the consequences of that. (I assume you 
mean the function's *local* scope, not the function's *surrounding* 
scope.)

So if I have an inline function and it gets used twice:

    inline def spam(*args):
        # Do something with args...

    def eggs(a, b, c):
        thing = spam(a, b, c)
        ...

    def cheese(x):
        obj = spam(x, 2)
        ...

does that mean that the compiler will translate the above to:

    def eggs(a, b, c):
        def spam(*args):
            # Do something with args...
        thing = spam(a, b, c)
        ...

    def cheese(x):
        def spam(*args):
            # Do something with args...
        obj = spam(x, 2)
        ...

and the top level "spam" will not actually exist?

    globals()['spam']  # KeyError

If the top-level spam *does* exist, what kind of object is it?


Going back to the functions eggs() and cheese() that call the inline 
function, and so have a copy of spam compiled into their body. Doesn't 
that double (or triple) the memory used?

Are you sure that this will lead to a performance increase? Nested 
functions are created at runtime, not compile time, same as non-nested 
functions. This is pretty fast, because they are created from 
pre-compiled parts, but there is still some runtime cost. It is not 
clear to me that the cost of assembling the function will be less than 
the cost of a global lookup.


What happens if we write the reference to the inline function before we 
write the inline function?

    def eggs(a, b, c):
        thing = spam(a, b, c)
        ...

    inline def spam(*args):

How does that resolve with Python's execution rules? Modules are 
compiled in one pass. How does the interpreter know that eggs' reference 
to spam is to an inline function?


Note: this is also being discussed on Discuss.

https://discuss.python.org/t/inline-python-functions-and-methods/12412/1

Anyone who cares a lot about this issue may want to follow both 
discussions.



-- 
Steve
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/VCE4V4CYYRNHR6NOISJIKUDYJEETU5ZD/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to