Skip to content

[mypyc] Generate smaller code for casts #12839

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 23, 2022
Merged

[mypyc] Generate smaller code for casts #12839

merged 4 commits into from
May 23, 2022

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