David Lord <david...@gmail.com> added the comment:
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. ---------- nosy: +davidism _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue39168> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com