◐ Shell
clean mode source ↗

Message 286161 - Python tracker

Serhiy Storchaka: "Calling _PyStack_AsDict() with non-string or non-unique keywords means that the var-keyword argument was first unpacked to arrays of keyword names and values and then converted back to a dict. Seems something is done non-efficiently." (msg286159 of the issue #29360)

Python code from test_dict:

   dict(**invalid)

Bytecode:

    LOAD_GLOBAL              1 (dict)
    BUILD_TUPLE              0
    LOAD_FAST                1 (invalid)
    CALL_FUNCTION_EX         1

Call stack:

* _PyEval_EvalFrameDefault()
* do_call_core()
* PyObject_Call()
* _Py_RawFastCallDict() -- conversion from dict to stack+kwnames
* type_call()
* call_init()
* _PyStack_AsDict() -- convertsion from stack+kwnames to dict

Oh right, there are two conversions using a temporary FASTCALL format for (keyword) arguments.

This code was not upstream yet, it comes from the pull request of this issue.

Maybe we need two flavors of type_call(): type_call(args: tuple, kwargs: tuple) if tp_fastinit isn't set, type_fastcall(stack, nargs, kwnames) (FASTCALL) if tp_fastinit is set.

But it seems that the logic should be implemented in PyObject_Call() and _PyObject_FastCallDict(), it cannot be implemented in type_call(), it's too late.

For best performances (avoid any kind of conversion and avoid any temporary object), we should implement something like that.