KEMBAR78
[mypyc] Generate smaller code for casts by JukkaL · Pull Request #12839 · python/mypy · GitHub
Skip to content

Conversation

JukkaL
Copy link
Collaborator

@JukkaL JukkaL commented May 22, 2022

Merge a cast op followed by a branch that does an error check and adds a
traceback entry. Since casts are very common, this reduces the size of
the generated code a fair amount.

Old code generated for a cast:

    if (likely(PyUnicode_Check(cpy_r_x)))
        cpy_r_r0 = cpy_r_x;
    else {
        CPy_TypeError("str", cpy_r_x);
        cpy_r_r0 = NULL;
    }
    if (unlikely(cpy_r_r0 == NULL)) {
        CPy_AddTraceback("t/t.py", "foo", 2, CPyStatic_globals);
        goto CPyL2;
    }

New code:

    if (likely(PyUnicode_Check(cpy_r_x)))
        cpy_r_r0 = cpy_r_x;
    else {
        CPy_TypeErrorTraceback("t/t.py", "foo", 2, CPyStatic_globals, "str", cpy_r_x);
        goto CPyL2;
    }

The generated assembly is the same in the successful case, but assembly for the
error handler is more compact, reducing the size of binaries.

We could also do a similar thing to unbox ops in the future.

@JukkaL JukkaL merged commit 040f3ab into master May 23, 2022
@JukkaL JukkaL deleted the mypyc-cast-codegen branch May 23, 2022 14:18
@msullivan
Copy link
Collaborator

Great cleanup

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants