Skip to content

Commit ba933ea

Browse files
committed
pythongh-119786: move frames documentation to InternalDocs and add details
1 parent f385d99 commit ba933ea

File tree

2 files changed

+35
-38
lines changed

2 files changed

+35
-38
lines changed

InternalDocs/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ it is not, please report that through the
1616

1717
[Exception Handling](exception_handling.md)
1818

19+
[Frames](frames.md)
20+
1921
[Adaptive Instruction Families](adaptive.md)

Objects/frame_layout.md renamed to InternalDocs/frames.md

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,33 @@
1-
# The Frame Stack
2-
3-
Each call to a Python function has an activation record,
4-
commonly known as a "frame".
5-
Python semantics allows frames to outlive the activation,
6-
so they have (before 3.11) been allocated on the heap.
7-
This is expensive as it requires many allocations and
8-
results in poor locality of reference.
9-
10-
In 3.11, rather than have these frames scattered about memory,
11-
as happens for heap-allocated objects, frames are allocated
12-
contiguously in a per-thread stack.
13-
This improves performance significantly for two reasons:
14-
* It reduces allocation overhead to a pointer comparison and increment.
15-
* Stack allocated data has the best possible locality and will always be in
16-
CPU cache.
17-
18-
Generator and coroutines still need heap allocated activation records, but
19-
can be linked into the per-thread stack so as to not impact performance too much.
1+
# Frames
202

21-
## Layout
22-
23-
Each activation record consists of four conceptual sections:
3+
Each call to a Python function has an activation record, commonly known as a
4+
"frame". It contains information about the function being executed, consisting
5+
of four conceptual sections:
246

257
* Local variables (including arguments, cells and free variables)
26-
* Evaluation stack
8+
* Evaluation stack and instruction pointer
279
* Specials: The per-frame object references needed by the VM: globals dict,
2810
code object, etc.
2911
* Linkage: Pointer to the previous activation record, stack depth, etc.
3012

31-
### Layout
13+
The definition of the ``_PyInterpreterFrame`` struct is in
14+
[Include/internal/pycore_frame.h](https://github.com/python/cpython/blob/main/Include/internal/pycore_frame.h).
15+
16+
# Allocation
17+
18+
Python semantics allows frames to outlive the activation, so they need to
19+
be allocated outside the C call stack. To reduce overhead and improve locality
20+
of reference, most frames are allocated contiguously in a per-thread stack
21+
(see ``_PyThreadState_PushFrame`` in
22+
[Python/pystate.c](https://github.com/python/cpython/blob/main/Python/pystate.c)).
23+
24+
Frames of generators and coroutines are allocated on the heap, embedded in the
25+
generator and coroutine objects. See ``PyGenObject`` in
26+
[Include/internal/pycore_genobject.h](https://github.com/python/cpython/blob/main/Include/internal/pycore_genobject.h).
3227

33-
The specials and linkage sections are a fixed size, so are grouped together.
28+
## Layout
29+
30+
The specials and linkage sections have a fixed size, so are grouped together.
3431

3532
Each activation record is laid out as:
3633
* Specials and linkage
@@ -53,7 +50,7 @@ as the arguments on the stack are (usually) already in the correct
5350
location for the parameters. However, it requires the VM to maintain
5451
an extra pointer for the locals, which can hurt performance.
5552

56-
A variant that only needs the need two pointers is to reverse the numbering
53+
A variant that only needs two pointers is to reverse the numbering
5754
of the locals, so that the last one is numbered `0`, and the first in memory
5855
is numbered `N-1`.
5956
This allows the locals, specials and linkage to accessed from the frame pointer.
@@ -62,7 +59,7 @@ We may implement this in the future.
6259
#### Note:
6360

6461
> In a contiguous stack, we would need to save one fewer registers, as the
65-
> top of the caller's activation record would be the same at the base of the
62+
> top of the caller's activation record would be the same as the base of the
6663
> callee's. However, since some activation records are kept on the heap we
6764
> cannot do this.
6865
@@ -99,18 +96,16 @@ frames for each activation, but with low runtime overhead.
9996

10097
### Generators and Coroutines
10198

102-
103-
Generator objects have a `_PyInterpreterFrame` embedded in them.
104-
This means that creating a generator requires only a single allocation,
105-
reducing allocation overhead and improving locality of reference.
106-
The embedded frame is linked into the per-thread frame when iterated or
107-
awaited.
99+
Generators (objects of type ``PyGen_Type``, ``PyCoro_Type`` or
100+
``PyAsyncGen_Type``) have a `_PyInterpreterFrame` embedded in them, so
101+
that they can be created with a single memory allocation.
102+
When such an embedded frame is iterated or awaited, it can be linked with
103+
frames on the per-thread stack via the linkage fields.
108104

109105
If a frame object associated with a generator outlives the generator, then
110-
the embedded `_PyInterpreterFrame` is copied into the frame object.
111-
112-
113-
All the above applies to coroutines and async generators as well.
106+
the embedded `_PyInterpreterFrame` is copied into the frame object (see
107+
``take_ownership()`` in
108+
[Python/frame.c](https://github.com/python/cpython/blob/main/Python/frame.c)).
114109

115110
### Field names
116111

@@ -119,7 +114,7 @@ Thus, some of the field names may be a bit misleading.
119114

120115
For example the `f_globals` field has a `f_` prefix implying it belongs to the
121116
`PyFrameObject` struct, although it belongs to the `_PyInterpreterFrame` struct.
122-
We may rationalize this naming scheme for 3.12.
117+
We may rationalize this naming scheme for a later version.
123118

124119

125120
### Shim frames

0 commit comments

Comments
 (0)