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 four conceptual sections:
24
6
25
7
* Local variables (including arguments, cells and free variables)
26
- * Evaluation stack
8
+ * Evaluation stack and instruction pointer
27
9
* Specials: The per-frame object references needed by the VM: globals dict,
28
10
code object, etc.
29
11
* Linkage: Pointer to the previous activation record, stack depth, etc.
30
12
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 ) .
32
27
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.
34
31
35
32
Each activation record is laid out as:
36
33
* Specials and linkage
@@ -53,7 +50,7 @@ as the arguments on the stack are (usually) already in the correct
53
50
location for the parameters. However, it requires the VM to maintain
54
51
an extra pointer for the locals, which can hurt performance.
55
52
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
57
54
of the locals, so that the last one is numbered ` 0 ` , and the first in memory
58
55
is numbered ` N-1 ` .
59
56
This allows the locals, specials and linkage to accessed from the frame pointer.
@@ -62,7 +59,7 @@ We may implement this in the future.
62
59
#### Note:
63
60
64
61
> 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
66
63
> callee's. However, since some activation records are kept on the heap we
67
64
> cannot do this.
68
65
@@ -99,18 +96,16 @@ frames for each activation, but with low runtime overhead.
99
96
100
97
### Generators and Coroutines
101
98
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.
108
104
109
105
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 ) ).
114
109
115
110
### Field names
116
111
@@ -119,7 +114,7 @@ Thus, some of the field names may be a bit misleading.
119
114
120
115
For example the ` f_globals ` field has a ` f_ ` prefix implying it belongs to the
121
116
` 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 .
123
118
124
119
125
120
### Shim frames
0 commit comments