gh-152666: Avoid reference counting of code objects#152955
Conversation
markshannon
left a comment
There was a problem hiding this comment.
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?
| struct _PyInterpreterFrame { | ||
| _PyStackRef f_executable; /* Borrowed reference (code object or None) */ | ||
| /* Borrowed reference (code object or None) */ | ||
| /* Strong reference for generators */ |
There was a problem hiding this comment.
This should be a borrowed reference for generators as well.
The function object still keeps the code object alive, regardless of whether it is a generator function or not.
There was a problem hiding this comment.
I'm not sure if there is a trivial way for a generator to hold onto a borrowed reference since for a generator g, you can always access the code object by doing g.gi_code. The function can get freed but the code object is still needed. See, for example, this test:
cpython/Lib/test/test_generators.py
Lines 116 to 135 in 4221ce7
| def h(): | ||
| pass | ||
|
|
||
| old_codes = _testcapi.function_get_old_codes(f) |
There was a problem hiding this comment.
The old codes list is an implementation detail.
Would it be possible to check the behavior rather than the implementation?
Specifically, test that:
- reassigning the code object for a running function still works
- reassigning many code objects doesn't leak memory
|
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 |
Co-authored-by: Mark Shannon <Mark.Shannon@arm.com>
Functions keep a list of past code objects so that a frame only needs borrowed references to them.