[Python-ideas] (FAT Python) Convert keyword arguments to positional?

Franklin? Lee leewangzhong+python at gmail.com
Thu Jan 14 05:32:46 EST 2016


(FAT Python: http://faster-cpython.readthedocs.org/fat_python.html)

FAT Python uses guards to check whether a global name (for example,
the name for a function) has changed its value. Idea: If you know
exactly which function will be called, you can also optimize based on
the properties of that function.

According to Eli Bendersky's 2012 blog post[1] (which might be
outdated), a function call with keyword arguments is potentially
slower than one with only positional arguments.

    """
    If the function in question accepts no arguments (marked by the
METH_NOARGS flag when the function is created) or just a single object
argument (METH_0 flag), call_function doesn't go through the usual
argument packing and can call the underlying function pointer
directly.

    ...

    do_call ... implements the most generic form of calling. However,
there's one more optimization - if func is a PyFunction (an object
used internally to represent functions defined in Python code), a
separate path is taken - fast_function.

    ...

    ... PyCFunction objects that do [receive] keyword arguments [use
do_call instead of fast_function]. A curious aspect of this fact is
that it's somewhat more efficient to not pass keyword arguments to C
functions that either accept them or are fine with just positional
arguments.
    """

So maybe, in a function which uses FAT Python's guards, we can replace
some of the keyworded-calls to global function with positional-only
calls. It might be a micro-optimization, but it's one that the Python
programmer doesn't have to worry about.

Concerns:
1. Is it possible to correctly determine, for a given function, which
positional parameters have which names?
2. Is it possible to change a function object's named parameters some
time after it's created (and inspected)?

PS: I didn't feel like this was appropriate for either of Victor's
running PEP threads, and the third milestone thread is in the previous
month's archives, so I thought that making a new thread would be best.

[1] http://eli.thegreenplace.net/2012/03/23/python-internals-how-callables-work


More information about the Python-ideas mailing list