This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Add vectorcall for generic alias object
Type: Stage: patch review
Components: Library (Lib) Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Dennis Sweeney, JelleZijlstra, corona10, gvanrossum, kj, penguin_wwy
Priority: normal Keywords: patch

Created on 2022-03-19 15:31 by penguin_wwy, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 31996 merged penguin_wwy, 2022-03-19 15:32
Messages (8)
msg415556 - (view) Author: penguin_wwy (penguin_wwy) * Date: 2022-03-19 15:31
Although `ga_call` determines whether `origin` has a vectorcall, it needs to be unpacked the parameters that are already packed.
                                              /-> origin.vectorcall(unpacked)
MakeTpCall(packed) -> ga_call -> PyObject_Call
                                               \-> origin.tp_call

We can advance the `vectorcall` judgment to the `setup` phase.

ga_vectorcall -> origin.vectorcall

or

ga_make_tp_call -> _PyObject_MakeTpCall(packed argument) -> origin.tp_call

This will have no effect on tp_call, which still only needs to be packed once, while vectorcall does not need packed/unpacked
msg415589 - (view) Author: Dong-hee Na (corona10) * (Python committer) Date: 2022-03-20 03:48
We decided not to add it 
see bpo-40369
msg415613 - (view) Author: penguin_wwy (penguin_wwy) * Date: 2022-03-20 15:04
The point of bpo-40369 issue seems to be to provide vectorcall to the `GenericAliasType`, means vectorcall for `Queue[int]`

However, my idea is to add vectorcall to gaobject, like this:

```
d = dict[int]
d(a=1, b=2) <-- vectorcall
```

The idea came from my desire to implement different subclasses of a type when implementing some kind of factory model:

```
class ChildClassBuilder:
    ...

    def set_type(self, t: Type):
        self.t = t

    def build():
        instance = self.t()
        ...
        return instance
```

In the case of high type-hint coverage, the `self.t` is often a gaobject.

Benchmark example:
```
def test():
    d = dict[int]
    for _ in range(1000000):
        d(a=1, b=2)
```

Speedup 20% in my wsl2
```
opt:
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    1.728    1.728 <string>:1(<module>)
        1    1.728    1.728    1.728    1.728 test.py:4(test)

master:
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    2.271    2.271 <string>:1(<module>)
        1    2.271    2.271    2.271    2.271 test.py:4(test)
```
msg415615 - (view) Author: penguin_wwy (penguin_wwy) * Date: 2022-03-20 15:43
There is a small problem with the example:

```
d = dict[int]
```

should

```
d = dict[str, int]
```
msg415616 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2022-03-20 15:46
@Dennis, if/when the PR looks good to you, you can merge it.
msg415630 - (view) Author: Dennis Sweeney (Dennis Sweeney) * (Python committer) Date: 2022-03-20 19:54
I profiled dict[str, int](a=1, b=2), and it looks like a decent chunk of time comes from PyUnicode_New as used by PyObject_SetAttrString.

You could also try replacing PyObject_SetAttrString with PyObject_SetAttr and adding "__orig_class__" to the global strings with Tools/scripts/generate_global_objects.py, probably for a later PR.
msg415656 - (view) Author: penguin_wwy (penguin_wwy) * Date: 2022-03-21 03:20
> You could also try replacing PyObject_SetAttrString with PyObject_SetAttr and adding "__orig_class__" to the global strings with Tools/scripts/generate_global_objects.py, probably for a later PR.

Already done via `make regen-global-objects`

Also, I found that `regen-global-objects` depends on `regen-deepfreeze`, which should be a bug. This causes the `regen-global-objects` to be executed with the compile task first, and it does not compile successfully at this point because of the new symbols(_Py_ID(__orig_class__)) added.
msg415699 - (view) Author: Dennis Sweeney (Dennis Sweeney) * (Python committer) Date: 2022-03-21 20:33
New changeset 1ea055bd53ccf976e88018983a3c13447c4502be by penguin_wwy in branch 'main':
bpo-47067: Optimize calling GenericAlias objects (GH-31996)
https://github.com/python/cpython/commit/1ea055bd53ccf976e88018983a3c13447c4502be
History
Date User Action Args
2022-04-11 14:59:57adminsetgithub: 91223
2022-03-21 21:15:20gvanrossumsettitle: Add vectorcall for generica alias object -> Add vectorcall for generic alias object
2022-03-21 20:33:25Dennis Sweeneysetmessages: + msg415699
2022-03-21 03:20:17penguin_wwysetmessages: + msg415656
2022-03-20 19:54:52Dennis Sweeneysetmessages: + msg415630
2022-03-20 15:46:56gvanrossumsetmessages: + msg415616
2022-03-20 15:43:03penguin_wwysetmessages: + msg415615
2022-03-20 15:20:04AlexWaygoodsetnosy: + gvanrossum, JelleZijlstra, kj
2022-03-20 15:04:25penguin_wwysetmessages: + msg415613
2022-03-20 03:51:04corona10setnosy: + Dennis Sweeney
2022-03-20 03:48:15corona10setnosy: + corona10
messages: + msg415589
2022-03-19 15:32:44penguin_wwysetkeywords: + patch
stage: patch review
pull_requests: + pull_request30085
2022-03-19 15:31:34penguin_wwycreate