This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Embed interpreter frame in generator.
Type: performance Stage: resolved
Components: Interpreter Core Versions: Python 3.11
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Mark.Shannon Nosy List: Mark.Shannon, pablogsal, vstinner
Priority: Keywords: patch

Created on 2021-12-02 12:27 by Mark.Shannon, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 29891 merged Mark.Shannon, 2021-12-02 13:05
PR 29960 merged Mark.Shannon, 2021-12-07 12:57
Messages (6)
msg407526 - (view) Author: Mark Shannon (Mark.Shannon) * (Python committer) Date: 2021-12-02 12:27
Currently, the InterpreterFrame for a generator is allocated on the heap separately from the generator.
This means that 2 allocations are needed to create a generator, and an extra indirection is needed to get from the generator to the frame.

By embedding the frame in the generator, we only need one alloaction, we save an indirection *and* the code gets simpler: transferring the frame from generator to frame object is just a single memcpy.
msg407781 - (view) Author: Mark Shannon (Mark.Shannon) * (Python committer) Date: 2021-12-06 10:13
New changeset 299483c95d601ddcfdce2f96418b6499c1fc7b9f by Mark Shannon in branch 'main':
bpo-45963: Make space for the InterpreterFrame of a generator in that generator. (GH-29891)
msg407910 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-12-07 10:41
Unfortunately, all refleak buildbots are failing after 299483c95d601ddcfdce2f96418b6499c1fc7b9f was merged. I bisected the problem to it:

299483c95d601ddcfdce2f96418b6499c1fc7b9f is the first bad commit
commit 299483c95d601ddcfdce2f96418b6499c1fc7b9f
Author: Mark Shannon <>
Date:   Mon Dec 6 10:13:49 2021 +0000

    bpo-45963: Make space for the InterpreterFrame of a generator in that generator. (GH-29891)

    * Make generator, coroutine and async gen structs all the same size.

    * Store interpreter frame in generator (and coroutine). Reduces the number of allocations neeeded for a generator from two to one.

 Include/cpython/genobject.h     |  23 ++---
 Include/internal/pycore_ceval.h |   2 +-
 Include/internal/pycore_frame.h |   2 +-
 Lib/test/            |   2 +-
 Objects/genobject.c             | 183 ++++++++++++++++++++++------------------
 Python/ceval.c                  |  41 +++------
 Python/frame.c                  |  18 ++--
 7 files changed, 127 insertions(+), 144 deletions(-)

Following out buildbot policy, the change will need to be reverted if is not fixed in 24 hours.
msg407911 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-12-07 10:43
This is also unfortunately blocking the 3.11.a3 release :(
msg407923 - (view) Author: Mark Shannon (Mark.Shannon) * (Python committer) Date: 2021-12-07 11:51
I'll look into it now, and fix or revert it.
msg415782 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2022-03-22 14:56
Leak fixed by:

commit 064e53d19aea6d6906fa8f7706a2556a2c293ccd
Author: Mark Shannon <>
Date:   Tue Dec 7 18:05:48 2021 +0000

    Fix leak when an exception is raised during generator creation. (GH-29960)
Date User Action Args
2022-04-11 14:59:53adminsetgithub: 90121
2022-03-22 14:56:07vstinnersetnosy: + vstinner
messages: + msg415782
2021-12-07 23:23:17pablogsalsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2021-12-07 23:23:10pablogsalsetpriority: release blocker ->
2021-12-07 12:57:17Mark.Shannonsetpull_requests: + pull_request28185
2021-12-07 11:51:03Mark.Shannonsetmessages: + msg407923
2021-12-07 10:43:25pablogsalsetpriority: normal -> release blocker

messages: + msg407911
2021-12-07 10:41:54pablogsalsetnosy: + pablogsal
messages: + msg407910
2021-12-06 10:13:59Mark.Shannonsetmessages: + msg407781
2021-12-02 13:05:23Mark.Shannonsetkeywords: + patch
stage: patch review
pull_requests: + pull_request28115
2021-12-02 12:27:40Mark.Shannoncreate