Skip to content

Generic __new__ has serious performance overhead #681

Description

@bartv

In our code base (https://github.com/inmanta/inmanta) we are in progress of typing our code. We progressed pretty far and now noticed in our compiler that it has a serious overhead. In a certain performance benchmark Generic.__new__ amounts to 8.7% of the execution time. (It is called ~1M times)

The following piece of code demonstrates the large difference in performance:

import time
from typing import *

T = TypeVar("T")

class Test(Generic[T]):
    def __init__(self, x: T) -> None:
        pass

class Test2():
    def __init__(self, x) -> None:
        pass

a = time.time()
[Test("test") for i in range(1, 1000000)]
b = time.time()
[Test2("test") for i in range(1, 1000000)]
c = time.time()

print(f"Generic {b-a}")
print(f"Non-Generic {c-b}")

With python 3.7.4 the results are:

Generic 0.8733069896697998
Non-Generic 0.3357818126678467

With python 3.6 the results are slightly worse (but not so relevant due to the new implementation)

(#196 seems related but because the implementation is entirely different I did not post it there)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions