[Python-ideas] Preallocated tuples and dicts for function calls
Martin Bammer
mrbm74 at gmail.com
Fri Mar 8 16:16:02 EST 2019
Hi,
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.
Some examples:
msgpack.unpackb(b'\x93\x01\x02\x03', use_list=False, raw=False)
The above function call needs a tuple with 1 entry and a dict with 2
entries.
All entries are constant. So in this case the interpreter can immediately
execute the function call.
Without the optimization the interpreter would need to:
- create new tuple (allocate memory)
- write constant into first tuple index.
- create dict (allocate memory)
- add key+value
- add key+value
- call function
Another example:
foo(bar, 3, 5, arg1=bar1, arg2=True)
The above needs a tuple with 3 entries. 2 of them are constant. And a dict
with 2 entries. 1 of them is constant.
With the optimization:
- write bar into first tuple index.
- replace first key+value pair in the dict.
- call function
Without the optimization:
- create new tuple (allocate memory)
- write bar into first tuple index.
- write constant into second tuple index.
- write constant into third tuple index.
- create dict (allocate memory)
- add key+value
- add key+value
- call function
If this idea is possible to implement I assume the function calls would
receive a great speed improvment.
Best regards,
Martin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190308/a42af3f6/attachment-0001.html>
More information about the Python-ideas
mailing list