Skip to content

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

Merged
kumaraditya303 merged 7 commits intopython:mainfrom
benediktjohannes:patch-6
Feb 14, 2026
Merged

gh-144822: Remove an indirect double check in codegen.c through co == NULL and Py_XDECREF(co);#144823
kumaraditya303 merged 7 commits intopython:mainfrom
benediktjohannes:patch-6

Conversation

@benediktjohannes
Copy link
Contributor

@benediktjohannes benediktjohannes commented Feb 14, 2026

Description of the problem

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.

Changes made

I've removed the unnecessary and performance consuming line for slightly increased performance and code readability.

Additional information

This was not tested (and also not performance tested), but seems obvious to me from looking at the code that this is just an unnecessary statement.

Contributed by Benedikt Johannes.

@benediktjohannes benediktjohannes changed the title gh-144822: Slightly improve performance in codegen.c via removing indirectly double check through Py_XDECREF(co); gh-144822: Slightly improve performance in codegen.c via removing an indirect double check through Py_XDECREF(co); Feb 14, 2026
Copy link
Contributor

@eendebakpt eendebakpt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without benchmarks the news entry is a bit misleading and I think we could do without it. The code change itself is good.

Copy link
Contributor

@kumaraditya303 kumaraditya303 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no perf benefit because it is on the error path, please remove the news entry.

@bedevere-app
Copy link

bedevere-app bot commented Feb 14, 2026

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@benediktjohannes
Copy link
Contributor Author

Hi, thanks for the reviews! I agree with your points while I shortly want to mention why I thought of a potential (slightly) improvement in performance (and I still think that this is true, but I'm not quite sure). Although this is in an error path, this does not mean that it does not improve performance in my opinion because in this case just the telling of the fact that there's an error is faster. Now one might say that this won't bring any profit while I think that there could be some (edge) cases in which you for example have live code testing of different inputs for specific tests whether code is renderable or not in some cases which then might (in very edgy cases) lead to performance improvements if these are hot paths, but I agree that this is very edgy and maybe only theoretically, so I'll remove the news entry.

@benediktjohannes
Copy link
Contributor Author

@kumaraditya303 should I remove the whole news entry or only the "performance" wording and change it to something like "removed double check" or something like this?

@benediktjohannes benediktjohannes changed the title gh-144822: Slightly improve performance in codegen.c via removing an indirect double check through Py_XDECREF(co); gh-144822: Remove an indirect double check in codegen.c through Py_XDECREF(co); Feb 14, 2026
@benediktjohannes benediktjohannes changed the title gh-144822: Remove an indirect double check in codegen.c through Py_XDECREF(co); gh-144822: Remove an indirect double check in codegen.c through co == NULL and Py_XDECREF(co); Feb 14, 2026
@benediktjohannes
Copy link
Contributor Author

I've now changed the news entry, but if you want me to completely remove if, please let me know

@benediktjohannes
Copy link
Contributor Author

I have made the requested changes; please review again

@bedevere-app
Copy link

bedevere-app bot commented Feb 14, 2026

Thanks for making the requested changes!

@kumaraditya303: please review the changes made to this pull request.

@kumaraditya303
Copy link
Contributor

Yes, remove it completely.

@benediktjohannes
Copy link
Contributor Author

Ok 👍

@benediktjohannes
Copy link
Contributor Author

It's deleted @kumaraditya303

@benediktjohannes

This comment was marked as resolved.

@benediktjohannes

This comment was marked as resolved.

@benediktjohannes
Copy link
Contributor Author

Ah, only maintainers can add skip news label @kumaraditya303

@benediktjohannes
Copy link
Contributor Author

Thank you @StanFromIreland

@kumaraditya303 kumaraditya303 enabled auto-merge (squash) February 14, 2026 19:09
@kumaraditya303 kumaraditya303 merged commit 645f5c4 into python:main Feb 14, 2026
49 of 51 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants