Skip to content

gh-152666: Avoid reference counting of code objects - #152955

Open
brijkapadia wants to merge 13 commits into
python:mainfrom
brijkapadia:code-obj
Open

gh-152666: Avoid reference counting of code objects#152955
brijkapadia wants to merge 13 commits into
python:mainfrom
brijkapadia:code-obj

Conversation

@brijkapadia

@brijkapadiabrijkapadia commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Functions keep a list of past code objects so that a frame only needs borrowed references to them.

@brijkapadia
brijkapadia marked this pull request as ready for review July 4, 2026 17:06

@markshannonmarkshannon left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The implementation of the old codes list looks good.
I'm wondering if the behavior, not the implementation, could be tested (or is it already?)

Also, can you make f_executable a PyObject * and always borrow it, to minimize overhead?

Comment threadInclude/internal/pycore_interpframe_structs.h Outdated
Comment threadInclude/internal/pycore_interpframe_structs.h Outdated
Comment threadLib/test/test_capi/test_function.py Outdated
@bedevere-app

Copy link
Copy Markdown

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.

@brijkapadia

Copy link
Copy Markdown
ContributorAuthor

I have made the requested changes; please review again

Unless I'm missing something (which is possible!) I do not think there is a way for generators to hold borrowed references without changing behavior.

@bedevere-app

Copy link
Copy Markdown

Thanks for making the requested changes!

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

@bedevere-app
bedevere-appBot requested a review from markshannonJuly 9, 2026 00:43
@markshannon

Copy link
Copy Markdown
Member

Unless I'm missing something (which is possible!) I do not think there is a way for generators to hold borrowed references without changing behavior.

The generator is no different to the frame: it can borrow the reference to the code object, as long as it has a strong reference to the function. As the generator needs to keep a reference to the code object when cleared, it must also keep a reference to the function.

@brijkapadia

Copy link
Copy Markdown
ContributorAuthor

@markshannon I've tried to implement borrowed references for generators but I've kept running into garbage collection issues with async generators in asyncio (even before this prior commit). There might be an easier solution that I'm missing but I'm not exactly sure how to resolve these issues cleanly.

Sign up for freeto 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.

2 participants

@brijkapadia@markshannon