Skip to content

Commit c878767

Browse files
authored
gh-119786: move frames documentation to InternalDocs and add details (#121009)
1 parent 9e4a81f commit c878767

File tree

2 files changed

+38
-55
lines changed

2 files changed

+38
-55
lines changed

Diff for: InternalDocs/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ it is not, please report that through the
1414

1515
[Compiler Design](compiler.md)
1616

17+
[Frames](frames.md)
18+
1719
[Adaptive Instruction Families](adaptive.md)
1820

1921
[The Source Code Locations Table](locations.md)

Diff for: Objects/frame_layout.md renamed to InternalDocs/frames.md

+36-55
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,54 @@
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 three conceptual sections:
246

257
* Local variables (including arguments, cells and free variables)
268
* Evaluation stack
27-
* Specials: The per-frame object references needed by the VM: globals dict,
28-
code object, etc.
29-
* Linkage: Pointer to the previous activation record, stack depth, etc.
9+
* Specials: The per-frame object references needed by the VM, including
10+
globals dict, code object, instruction pointer, stack depth, the
11+
previous frame, etc.
12+
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).
3015

31-
### Layout
16+
# Allocation
3217

33-
The specials and linkage sections are a fixed size, so are grouped together.
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 embedded in the generator and coroutine
25+
objects, so are not allocated in the per-thread stack. See ``PyGenObject`` in
26+
[Include/internal/pycore_genobject.h](https://github.com/python/cpython/blob/main/Include/internal/pycore_genobject.h).
27+
28+
## Layout
3429

3530
Each activation record is laid out as:
36-
* Specials and linkage
31+
* Specials
3732
* Locals
3833
* Stack
3934

4035
This seems to provide the best performance without excessive complexity.
41-
It needs the interpreter to hold two pointers, a frame pointer and a stack pointer.
36+
The specials have a fixed size, so the offset of the locals is know. The
37+
interpreter needs to hold two pointers, a frame pointer and a stack pointer.
4238

4339
#### Alternative layout
4440

4541
An alternative layout that was used for part of 3.11 alpha was:
4642

4743
* Locals
48-
* Specials and linkage
44+
* Specials
4945
* Stack
5046

5147
This has the advantage that no copying is required when making a call,
5248
as the arguments on the stack are (usually) already in the correct
5349
location for the parameters. However, it requires the VM to maintain
5450
an extra pointer for the locals, which can hurt performance.
5551

56-
A variant that only needs the need two pointers is to reverse the numbering
57-
of the locals, so that the last one is numbered `0`, and the first in memory
58-
is numbered `N-1`.
59-
This allows the locals, specials and linkage to accessed from the frame pointer.
60-
We may implement this in the future.
61-
62-
#### Note:
63-
64-
> 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
66-
> callee's. However, since some activation records are kept on the heap we
67-
> cannot do this.
68-
6952
### Generators and Coroutines
7053

7154
Generators and coroutines contain a `_PyInterpreterFrame`
@@ -92,25 +75,23 @@ section. The `frame_obj` field is initially `NULL`.
9275
The `PyFrameObject` may outlive a stack-allocated `_PyInterpreterFrame`.
9376
If it does then `_PyInterpreterFrame` is copied into the `PyFrameObject`,
9477
except the evaluation stack which must be empty at this point.
95-
The linkage section is updated to reflect the new location of the frame.
78+
The previous frame link is updated to reflect the new location of the frame.
9679

9780
This mechanism provides the appearance of persistent, heap-allocated
9881
frames for each activation, but with low runtime overhead.
9982

10083
### Generators and Coroutines
10184

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.
85+
Generators (objects of type ``PyGen_Type``, ``PyCoro_Type`` or
86+
``PyAsyncGen_Type``) have a `_PyInterpreterFrame` embedded in them, so
87+
that they can be created with a single memory allocation.
88+
When such an embedded frame is iterated or awaited, it can be linked with
89+
frames on the per-thread stack via the linkage fields.
10890

10991
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.
92+
the embedded `_PyInterpreterFrame` is copied into the frame object (see
93+
``take_ownership()`` in
94+
[Python/frame.c](https://github.com/python/cpython/blob/main/Python/frame.c)).
11495

11596
### Field names
11697

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

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

124105

125106
### Shim frames

0 commit comments

Comments
 (0)