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
20
2
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:
24
6
25
7
* Local variables (including arguments, cells and free variables)
26
8
* 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 ) .
30
15
31
- ### Layout
16
+ # Allocation
32
17
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
34
29
35
30
Each activation record is laid out as:
36
- * Specials and linkage
31
+ * Specials
37
32
* Locals
38
33
* Stack
39
34
40
35
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.
42
38
43
39
#### Alternative layout
44
40
45
41
An alternative layout that was used for part of 3.11 alpha was:
46
42
47
43
* Locals
48
- * Specials and linkage
44
+ * Specials
49
45
* Stack
50
46
51
47
This has the advantage that no copying is required when making a call,
52
48
as the arguments on the stack are (usually) already in the correct
53
49
location for the parameters. However, it requires the VM to maintain
54
50
an extra pointer for the locals, which can hurt performance.
55
51
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
-
69
52
### Generators and Coroutines
70
53
71
54
Generators and coroutines contain a ` _PyInterpreterFrame `
@@ -92,25 +75,23 @@ section. The `frame_obj` field is initially `NULL`.
92
75
The ` PyFrameObject ` may outlive a stack-allocated ` _PyInterpreterFrame ` .
93
76
If it does then ` _PyInterpreterFrame ` is copied into the ` PyFrameObject ` ,
94
77
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.
96
79
97
80
This mechanism provides the appearance of persistent, heap-allocated
98
81
frames for each activation, but with low runtime overhead.
99
82
100
83
### Generators and Coroutines
101
84
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.
108
90
109
91
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 ) ).
114
95
115
96
### Field names
116
97
@@ -119,7 +100,7 @@ Thus, some of the field names may be a bit misleading.
119
100
120
101
For example the ` f_globals ` field has a ` f_ ` prefix implying it belongs to the
121
102
` 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 .
123
104
124
105
125
106
### Shim frames
0 commit comments