Skip to content

Remove an indirect double check in codegen.c through co == NULL and Py_XDECREF(co); #144822

@benediktjohannes

Description

@benediktjohannes

Feature or enhancement

Proposal:

In static int codegen_function_body(compiler *c, stmt_ty s, int is_async, Py_ssize_t funcflags, int firstlineno) we use a check whether co == NULL in

PyCodeObject *co = _PyCompile_OptimizeAndAssemble(c, 1);
_PyCompile_ExitScope(c);
if (co == NULL) {
    Py_XDECREF(co);
    return ERROR;
}

and then use Py_XDECREF(co); which always returns a check failure in the case co == NULL which we already checked previously and for this reason never calls Py_DECREF(co); (which would anyway not work because it would cause errors) before we return the ERROR. Because of this, that condition check (which is triggered again) is unnecessary and just decreases performance slightly.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions