[Python-ideas] Preallocated tuples and dicts for function calls
storchaka at gmail.com
Sat Mar 9 04:19:14 EST 2019
08.03.19 23:16, Martin Bammer пише:
> what about the idea that the interpreter preallocates and preinitializes the
> tuples and dicts for function calls where possible when loading a module?
> Before calling a function then the interpreter would just need to update the
> items which are dynamic and then call the function.
A kind of this already has been implemented.
Tuples and dicts use free lists, so when you allocate a small tuple or
an empty dict, you usually do not use expensive memory allocating
functions, but take a preallocated object from a free list.
Although, this still has some overhead. This is why the property object
had an attached preallocated tuple for passing the self argument to the
getter function. But this was a complex and errorprone code. There were
at least three attempts to fix it, and new flaws were found month later
after every attempt. Finally this microoptimizations has been removed,
and named tuples will use a special type for getters of their attribute
Internally, CPython uses the private "fast" calling convention for many
builtin functions and methods. It allows to avoid creating an
intermediate tuple and dict at all. In future this convention will be
exposed publically. Cython already uses it, so third-party extensions
written in Cython have an advantage of using it.
More information about the Python-ideas