8000 pystate: move freelists to per-thread state · colesbury/nogil-3.12@2a4c17e · GitHub
[go: up one dir, main page]

Skip to content

Commit 2a4c17e

Browse files
committed
pystate: move freelists to per-thread state
1 parent 9f9b3d0 commit 2a4c17e

20 files changed

+135
-100
lines changed

Include/cpython/pystate.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,6 @@ typedef struct _stack_chunk {
106106
PyObject * data[1]; /* Variable sized */
107107
} _PyStackChunk;
108108

109-
typedef struct _Py_dict_thread_state {
110-
uint64_t dict_version;
111-
} _Py_dict_thread_state;
112-
113109
struct mi_heap_s;
114110
typedef struct mi_heap_s mi_heap_t;
115111
typedef struct _PyEventRc _PyEventRc;
@@ -156,8 +152,6 @@ struct _ts {
156152
* or most recently, executing _PyEval_EvalFrameDefault. */
157153
_PyCFrame *cframe;
158154

159-
_Py_dict_thread_state dict_state;
160-
161155
/* The thread will not stop for GC or other stop-the-world requests.
162156
* Used for *short* critical sections that to prevent deadlocks between
163157
* finalizers and stopped threads. */

Include/internal/pycore_context.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ extern PyTypeObject _PyContextTokenMissing_Type;
1313
/* runtime lifecycle */
1414

1515
PyStatus _PyContext_Init(PyInterpreterState *);
16-
void _PyContext_Fini(PyInterpreterState *);
16+
void _PyContext_Fini(PyThreadState *);
1717

1818

1919
/* other API */

Include/internal/pycore_dict.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ extern "C" {
1616

1717
/* runtime lifecycle */
1818

19-
extern void _PyDict_Fini(PyInterpreterState *interp);
19+
extern void _PyDict_Fini(PyThreadState *tstate);
2020

2121

2222
/* other API */
@@ -166,14 +166,15 @@ static inline PyDictSharedKeysObject* DK_AS_SPLIT(PyDictKeysObject *dk) {
166166
static inline uint64_t
167167
_PyDict_NextVersion(PyThreadState *tstate)
168168
{
169-
uint64_t version = tstate->dict_state.dict_version;
169+
struct _Py_dict_thread_state *dict_state = &((PyThreadStateImpl *)tstate)->dict_state;
170+
uint64_t version = dict_state->dict_version;
170171
if (version % DICT_GLOBAL_VERSION_INCREMENT == 0) {
171172
version = _Py_atomic_add_uint64(
172173
&_PyRuntime.dict_state.global_version,
173174
DICT_GLOBAL_VERSION_INCREMENT);
174175
}
175176
version += DICT_VERSION_INCREMENT;
176-
tstate->dict_state.dict_version = version;
177+
dict_state->dict_version = version;
177178
return version;
178179
}
179180

Include/internal/pycore_dict_state.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,18 @@ struct _Py_dict_runtime_state {
3131

3232
typedef struct PyDictSharedKeysObject PyDictSharedKeysObject;
3333

34-
struct _Py_dict_state {
34+
struct _Py_dict_thread_state {
35+
uint64_t dict_version;
3536
#if PyDict_MAXFREELIST > 0
3637
/* Dictionary reuse scheme to save calls to malloc and free */
3738
PyDictObject *free_list[PyDict_MAXFREELIST];
3839
PyDictKeysObject *keys_free_list[PyDict_MAXFREELIST];
3940
int numfree;
4041
int keys_numfree;
4142
#endif
43+
};
44+
45+
struct _Py_dict_state {
4246
PyDict_WatchCallback watchers[DICT_MAX_WATCHERS];
4347
/* shared keys from deallocated types (i.e., potentially dead) */
4448
PyDictSharedKeysObject *tracked_shared_keys;

Include/internal/pycore_floatobject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ extern "C" {
1313

1414
extern void _PyFloat_InitState(PyInterpreterState *);
1515
extern PyStatus _PyFloat_InitTypes(PyInterpreterState *);
16-
extern void _PyFloat_Fini(PyInterpreterState *);
16+
extern void _PyFloat_Fini(PyThreadState *);
1717
extern void _PyFloat_FiniType(PyInterpreterState *);
1818

1919

Include/internal/pycore_gc.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,12 +222,12 @@ _PyGC_ShouldCollect(struct _gc_runtime_state *gcstate)
222222
}
223223

224224
// Functions to clear types free lists
225-
extern void _PyTuple_ClearFreeList(PyInterpreterState *interp);
226-
extern void _PyFloat_ClearFreeList(PyInterpreterState *interp);
227-
extern void _PyList_ClearFreeList(PyInterpreterState *interp);
228-
extern void _PyDict_ClearFreeList(PyInterpreterState *interp);
229-
extern void _PyAsyncGen_ClearFreeLists(PyInterpreterState *interp);
230-
extern void _PyContext_ClearFreeList(PyInterpreterState *interp);
225+
extern void _PyTuple_ClearFreeList(PyThreadState *tstate);
226+
extern void _PyFloat_ClearFreeList(PyThreadState *tstate);
227+
extern void _PyList_ClearFreeList(PyThreadState *tstate);
228+
extern void _PyDict_ClearFreeList(PyThreadState *tstate);
229+
extern void _PyAsyncGen_ClearFreeLists(PyThreadState *tstate);
230+
extern void _PyContext_ClearFreeList(PyThreadState *tstate);
231231
extern void _Py_RunGC(PyThreadState *tstate);
232232

233233
#ifdef __cplusplus

Include/internal/pycore_genobject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ extern PyObject *_PyAsyncGenValueWrapperNew(PyThreadState *state, PyObject *);
1414

1515
/* runtime lifecycle */
1616

17-
extern void _PyAsyncGen_Fini(PyInterpreterState *);
17+
extern void _PyAsyncGen_Fini(PyThreadState *);
1818

1919

2020
/* other API */

Include/internal/pycore_interp.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ typedef struct PyThreadStateImpl {
6868
// semi-public fields are in PyThreadState
6969
PyThreadState tstate;
7070

71+
struct _Py_float_state float_state;
72+
struct _Py_tuple_state tuple;
73+
struct _Py_list_state list;
74+
struct _Py_dict_thread_state dict_state;
75+
struct _Py_async_gen_state async_gen;
76+
struct _Py_context_state context;
77+
7178
struct brc_state brc;
7279

7380
struct qsbr *qsbr;
@@ -194,14 +201,9 @@ struct _is {
194201
uint8_t active_code_watchers;
195202

196203
struct _Py_unicode_state unicode;
197-
struct _Py_float_state float_state;
198204
struct _Py_long_state long_state;
199205

200-
struct _Py_tuple_state tuple;
201-
struct _Py_list_state list;
202206
struct _Py_dict_state dict_state;
203-
struct _Py_async_gen_state async_gen;
204-
struct _Py_context_state context;
205207
struct _Py_exc_state exc_state;
206208

207209
struct ast_state ast;

Include/internal/pycore_list.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ extern "C" {
1414

1515
/* runtime lifecycle */
1616

17-
extern void _PyList_Fini(PyInterpreterState *);
17+
extern void _PyList_Fini(PyThreadState *);
1818

1919

2020
/* other API */

Include/internal/pycore_pystate.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ _PyThreadState_GET(void)
106106
return _Py_current_tstate;
107107
#endif
108108
}
109+
static inline PyThreadStateImpl*
110+
_PyThreadStateImpl_GET(void)
111+
{
112+
return (PyThreadStateImpl *)_PyThreadState_GET();
113+
}
109114

110115
static inline void
111116
_PyThreadState_SET(PyThreadState *tstate)

0 commit comments

Comments
 (0)
0