Avoid refcounting on code objects during frame creation/destruction
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
The __code__ attribute of function objects is assignable.
As a consequence, frames must hold a strong reference to the code object in case the reference held by the function is lost.
Ideally, the __code__ attribute would be read-only, but to fix that would be a multi-year deprecation effort.
Until that happens, if ever, we can replace the strong reference with a borrowed reference to the code object as long as code object is guaranteed to outlive the frame.
Since the function will outlive the frame, if the function object keeps references to its prior __code__ attributes
when __code__ is assigned, then the code object will always outlive the frame.
This will need an extra field in each function, but the cost of one assignment to NULL during function creation, is much less than the cost of an incref and a decref on every call to that function.
The only downside is that in some obscure cases the lifetime of a few code objects will be extended.
Since the free-threading build makes all code objects immortal, this could actually be a big saving for the free-threading build.
Linked PRs
- gh-152955
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing the linked PR gh-152955, then trace function objects, frames, and code objects around the code attribute and frame creation/destruction. Done means proving code objects outlive their frames while avoiding per-call reference-count operations, with the relevant CPython tests still passing.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Refactor
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100