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.

Author davidism
Recipients Ruslan Dautkhanov, Wouter De Borger, ZackerySpytz, calebj, davidism, gvanrossum, levkivskyi, miss-islington, navdevl
Date 2020-11-15.21:37:51
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1605476272.93.0.445504904684.issue39168@roundup.psfhosted.org>
In-reply-to
Content
Is this performance issue supposed to be fixed in 3.9? I'm still observing severe slowdown by inheriting from `Generic[T]`.

I'm currently adding typing to Werkzeug, where we define many custom data structures such as `MultiDict`. It would be ideal for these classes to be recognized as generic mappings. I remembered hearing about this performance issue somewhere, so I decided to test what happens.

Here's a minimal example without Werkzeug, the results in Werkzeug are similar or worse. I'd estimate each request creates about 10 of the various data structures, which are then accessed by user code, so I simulated that by creating and iterating a list of objects.

```python
class Test:
    def __init__(self, value):
        self.value = value

def main():
    ts = [Test(x) for x in range(10)]
    sum(t.value for t in ts)
```

```
$ python3.9 -m timeit -n 100000 -s 'from example import main' 'main()'
100000 loops, best of 5: 7.67 usec per loop
```

```python
import typing

V = typing.TypeVar("V")

class Test(typing.Generic[V]):
    def __init__(self, value: V) -> None:
        self.value = value

def main():
    ts = [Test(x) for x in range(10)]
    sum(t.value for t in ts)
```

```
$ python3.9 -m timeit -n 100000 -s 'from example import main' 'main()'
100000 loops, best of 5: 18.2 usec per loop
```

There is more than a 2x slowdown when using `Generic`. The timings (7 vs 18 usec) are the same across Python 3.6, 3.7, 3.8, and 3.9. It seems that 3.9 does not fix the performance issue.

Since we currently support Python 3.6+, I probably won't be able to use generics anyway due to the performance in those versions, but I wanted to make sure I'm not missing something with 3.9.
History
Date User Action Args
2020-11-15 21:37:53davidismsetrecipients: + davidism, gvanrossum, levkivskyi, ZackerySpytz, miss-islington, Ruslan Dautkhanov, navdevl, calebj, Wouter De Borger
2020-11-15 21:37:52davidismsetmessageid: <1605476272.93.0.445504904684.issue39168@roundup.psfhosted.org>
2020-11-15 21:37:52davidismlinkissue39168 messages
2020-11-15 21:37:51davidismcreate