8000 gh-94673: Add Per-Interpreter Storage for Static Builtin Types by ericsnowcurrently · Pull Request #95255 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-94673: Add Per-Interpreter Storage for Static Builtin Types #95255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 30 commits into from
Jul 26, 2022
Merged
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
7843524
Add PyInterpreterState.types.
ericsnowcurrently Jul 8, 2022
09fd0be
Add PyInterpreterState.types.num_builtins_initialized.
ericsnowcurrently Jul 9, 2022
afbde1a
Hard-code _Py_NUM_STATIC_BUILTIN_TYPES.
ericsnowcurrently Jul 18, 2022
f3aa649
Store per-interpreter state for static builtin types.
ericsnowcurrently Jul 19, 2022
9a1cc36
Export _PyStaticType_GetState().
ericsnowcurrently Jul 19, 2022
375b2d9
Move _PyStaticType_GetState() down next to _PyStaticType_InitBuiltin().
ericsnowcurrently Jul 19, 2022
b0b32a6
make it compile
kumaraditya303 Jul 19, 2022
eb05d9b
fix test_sys
kumaraditya303 Jul 19, 2022
f5ebd23
Reset tp_static_builtin_index only if a static builtin type.
ericsnowcurrently Jul 20, 2022
d94818b
Factor out get_static_builtin_index() and set_static_builtin_index().
ericsnowcurrently Jul 20, 2022
06799d5
Reset state->type later on.
ericsnowcurrently Jul 21, 2022
d78f10d
Add some asserts.
ericsnowcurrently Jul 21, 2022
44edc49
Decrement num_builtins_initialized.
ericsnowcurrently Jul 21, 2022
bc7fa26
Verify the per-interpreter type state has been cleared.
ericsnowcurrently Jul 22, 2022
3f20f4a
Add tp_static_builtin_index to the docs.
ericsnowcurrently Jul 25, 2022
9f7775d
Revert "Add tp_static_builtin_index to the docs."
ericsnowcurrently Jul 25, 2022
cf04c2c
Update tp_flags sooner in _PyStaticType_InitBuiltin().
ericsnowcurrently Jul 25, 2022
38427a0
Move the state/index-related helpers to the top.
ericsnowcurrently Jul 26, 2022
d313dbd
Add static_builtin_index_is_set() and static_builtin_index_clear().
ericsnowcurrently Jul 26, 2022
b4bbfcb
Hide the 1-based indexing behind the helper functions.
ericsnowcurrently Jul 26, 2022
1bd26c4
Move and edit a comment.
ericsnowcurrently Jul 26, 2022
8000
652fcc6
Factor out static_builtin_state_init() and static_builtin_state_clear().
ericsnowcurrently Jul 26, 2022
d558a46
Un-initialize the type if PyType_Ready() fails.
ericsnowcurrently Jul 26, 2022
39a8fc7
Drop an empty line.
ericsnowcurrently Jul 26, 2022
6019eba
Move a comment.
ericsnowcurrently Jul 26, 2022
f2f10f0
static_builtin_type_state -> static_builtin_state (and factor out sta…
ericsnowcurrently Jul 26, 2022
cea5d34
Pass the type to static_builtin_state_get().
ericsnowcurrently Jul 26, 2022
4348e3f
Handle non-builtin and non-static types in _PyStaticType_GetState().
ericsnowcurrently Jul 26, 2022
1c8b0c3
Use static_builtin_index_is_set() in static_builtin_index_get().
ericsnowcurrently Jul 26, 2022
ae2d440
Restrict _PyStaticType_GetState() to static builtin types.
ericsnowcurrently Jul 26, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move the state/index-related helpers to the top.
  • Loading branch information
ericsnowcurrently committed Jul 26, 2022
commit 38427a0f4dbfd277bcb8edcb23290ddeb5d1a8d2
63 changes: 35 additions & 28 deletions Objects/typeobject.c
48FA
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,37 @@ slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value);

static inline PyTypeObject * subclass_from_ref(PyObject *ref);


/* helpers for for static builtin types */

static inline size_t
static_builtin_index_get(PyTypeObject *self)
{
return self->tp_static_builtin_index;
}

static inline void
static_builtin_index_set(PyTypeObject *self, size_t index)
{
assert(index < _Py_MAX_STATIC_BUILTIN_TYPES);
self->tp_static_builtin_index = index;
}

static_builtin_type_state *
_PyStaticType_GetState(PyTypeObject *self)
{
if (static_builtin_index_get(self) == 0) {
return NULL;
}
PyInterpreterState *interp = _PyInterpreterState_GET();
return &(interp->types.builtins[static_builtin_index_get(self) - 1]);
}

// Also see _PyStaticType_InitBuiltin() and _PyStaticType_Dealloc().

/* end static builtin helpers */


/*
* finds the beginning of the docstring's introspection signature.
* if present, returns a pointer pointing to the first '('.
Expand Down Expand Up @@ -4250,8 +4281,6 @@ clear_static_tp_subclasses(PyTypeObject *type)
Py_CLEAR(type->tp_subclasses);
}

static inline size_t get_static_builtin_index(PyTypeObject *);
static inline void set_static_builtin_index(PyTypeObject *, size_t);

void
_PyStaticType_Dealloc(PyTypeObject *type)
Expand All @@ -4276,11 +4305,11 @@ _PyStaticType_Dealloc(PyTypeObject *type)
if (type->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) {
/* Reset the type's per-interpreter state.
This basically undoes what _PyStaticType_InitBuiltin() did. */
assert(get_static_builtin_index(type) > 0);
assert(static_builtin_index_get(type) > 0);
static_builtin_type_state *state = _PyStaticType_GetState(type);
assert(state != NULL);
state->type = NULL;
set_static_builtin_index(type, 0);
static_builtin_index_set(type, 0);

PyInterpreterState *interp = _PyInterpreterState_GET();
assert(interp->types.num_builtins_initialized > 0);
Expand Down Expand Up @@ -6702,33 +6731,21 @@ PyType_Ready(PyTypeObject *type)
}


static inline size_t
get_static_builtin_index(PyTypeObject *self)
{
return self->tp_static_builtin_index;
}

static inline void
set_static_builtin_index(PyTypeObject *self, size_t index)
{
self->tp_static_builtin_index = index;
}

int
_PyStaticType_InitBuiltin(PyTypeObject *self)
{
self->tp_flags = self->tp_flags | _Py_TPFLAGS_STATIC_BUILTIN;

/* It should only be called once for each builtin type. */
assert(get_static_builtin_index(self) == 0);
assert(static_builtin_index_get(self) == 0);

/* For static types we store some state in an array on each interpreter. */
PyInterpreterState *interp = _PyInterpreterState_GET();
interp->types.num_builtins_initialized++;
assert(interp->types.num_builtins_initialized < _Py_MAX_STATIC_BUILTIN_TYPES);

/* We use 1-based indexing so 0 can mean "not initialized". */
set_static_builtin_index(self, interp->types.num_builtins_initialized);
static_builtin_index_set(self, interp->types.num_builtins_initialized);

/* Now we initialize the type's per-interpreter state. */
static_builtin_type_state *state = _PyStaticType_GetState(self);
Expand All @@ -6738,16 +6755,6 @@ _PyStaticType_InitBuiltin(PyTypeObject *self)
return PyType_Ready(self);
}

static_builtin_type_state *
_PyStaticType_GetState(PyTypeObject *self)
{
if (get_static_builtin_index(self) == 0) {
return NULL;
}
PyInterpreterState *interp = _PyInterpreterState_GET();
return &(interp->types.builtins[get_static_builtin_index(self) - 1]);
}


static int
add_subclass(PyTypeObject *base, PyTypeObject *type)
Expand Down
0