Skip to content

Compile calls to NewType types into no-ops #958

Closed
python/mypy
#14398

Description

@JukkaL

Calling a NewType type is currently slow. Consider this code:

from typing import NewType, cast

class C: pass

N = NewType("N", C)

def f(c: C) -> N:
    return N(c)  # Slow!

The IR for the call N(c) looks like this:

L0:
    r0 = tt.globals :: static
    r1 = 'N'
    r2 = CPyDict_GetItem(r0, r1)
    if is_error(r2) goto L3 (error at f:10) else goto L1
L1:
    r3 = [c]
    r4 = load_address r3
    r5 = _PyObject_Vectorcall(r2, r4, 1, 0)
    dec_ref r2
    if is_error(r5) goto L3 (error at f:10) else goto L2

Since calling a NewType type is a no-op at runtime, we can simply compile the call away.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions