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

Victor Stinner victor.stinner at gmail.com
Thu Jan 14 13:07:46 EST 2016


2016-01-14 11:32 GMT+01:00 Franklin? Lee <leewangzhong+python at gmail.com>:
> (FAT Python: http://faster-cpython.readthedocs.org/fat_python.html)

FYI I moved the optimizer into a new project at GitHub to ease
contributions and experiments:
https://github.com/haypo/fatoptimizer

Running tests work on Python 3.4, but running optimized code required
a patched Python 3.6 (http://hg.python.org/sandbox/fatpython
repository which includes all patches).

> 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.

You need a guard on the function. The fat module provides such guard:
https://fatoptimizer.readthedocs.org/en/latest/fat.html#GuardFunc

Right now, it only watch for func.__code__. I'm not sure that it's
enought. A function has many attributes which can change its behaviour
if they are modified: __defaults__, __closure__, __dict__,
__globals__, __kwdefaults__, __module__ (?), __name__ (?),
__qualname__ (?).

> 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.

Yeah, ext_do_call() has to create a temporary dictionary, while
calling a function only with indexed parameters can avoid *completly*
the creation of any temporary object. PyEval_EvalFrameEx() takes a
stack (array of objects) which is used to pass parameters from
CALL_FUNCTION, but only for pure Python functions.

> 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.

It looks like you have a plan, and I think that you can implement the
optimization without changing the Python semantics.

> Concerns:
> 1. Is it possible to correctly determine, for a given function, which
> positional parameters have which names?

I think so. Just "read" the function prototype no? Such info is
available in AST.

> 2. Is it possible to change a function object's named parameters some
> time after it's created (and inspected)?

What do you think?

> 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.

Yeah, it's better to start a separated thread, thanks.

Victor


More information about the Python-ideas mailing list