Skip to content

Commit b6a5ee6

Browse files
committed
gh-162752: Skip stop-the-world when a type slot update changes nothing
Assigning a special method to a class stopped the world on every assignment, even when no slot changed (e.g. replacing one Python __getitem__ function with another). With other threads contending, each pause could cost about 1 ms. Before stopping the world, compute the new slot values with TYPE_LOCK held and skip the pause if none would change.
1 parent cb9d78f commit b6a5ee6

2 files changed

Lines changed: 61 additions & 13 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix a performance regression in the :term:`free-threaded build` when
2+
assigning a special method to a class, such as ``C.__getitem__ = f``, while
3+
other threads are running. The world is no longer stopped if no type slot
4+
would change.

‎Objects/typeobject.c‎

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3888,12 +3888,14 @@ solid_base(PyTypeObject *type)
38883888
/// data passed to update_slots_callback()
38893889
typedef struct {
38903890
pytype_slotdef **defs;
3891+
// If not NULL, only check if any slot would change, setting *changed.
3892+
bool *changed;
38913893
} update_callback_data_t;
38923894

38933895
static void object_dealloc(PyObject *);
38943896
static PyObject *object_new(PyTypeObject *, PyObject *, PyObject *);
38953897
static int object_init(PyObject *, PyObject *, PyObject *);
3896-
static int update_slot(PyTypeObject *, PyObject *);
3898+
static int update_slot(PyTypeObject *, PyObject *, bool *);
38973899
static void fixup_slot_dispatchers(PyTypeObject *);
38983900
static int type_ready(PyTypeObject *, int, int);
38993901
static int type_ready_publish(PyTypeObject *, int);
@@ -6720,8 +6722,22 @@ type_update_dict(PyTypeObject *type, PyDictObject *dict, PyObject *name,
67206722
static int
67216723
update_slot_after_setattr(PyTypeObject *type, PyObject *name)
67226724
{
6725+
#ifdef Py_GIL_DISABLED
6726+
// Check if we actually need to update the slot. If not, we can skip the
6727+
// STW, which can be expensive. This is pretty common since assigning a new
6728+
// function to an existing slot name, like "C.__getitem__ = f", usually
6729+
// changes no slot.
6730+
ASSERT_TYPE_LOCK_HELD();
6731+
bool changed = false;
6732+
if (update_slot(type, name, &changed) < 0) {
6733+
return -1;
6734+
}
6735+
if (!changed) {
6736+
return 0;
6737+
}
6738+
#endif
67236739
types_stop_world();
6724-
int res = update_slot(type, name);
6740+
int res = update_slot(type, name, NULL);
67256741
types_start_world();
67266742
#ifdef Py_GIL_DISABLED
67276743
// Invalidate again, after the slots are updated and while TYPE_LOCK is
@@ -12122,11 +12138,14 @@ collect_slot_descrs(PyTypeObject *type, PyObject **descrs)
1212212138
* If descrs is not NULL, it holds the descriptors found by
1212312139
* collect_slot_descrs() and no lookups are done.
1212412140
*
12141+
* If changed is not NULL, the type is not modified. Instead, *changed is
12142+
* set to true if the slot (or tp_flags) would be changed.
12143+
*
1212512144
* Exceptions that occur internally are ignored, such as when looking up
1212612145
* descriptors. */
1212712146
static void
1212812147
update_one_slot(PyTypeObject *type, pytype_slotdef *p, pytype_slotdef **next_p,
12129-
PyObject **descrs)
12148+
PyObject **descrs, bool *changed)
1213012149
{
1213112150
ASSERT_NEW_TYPE_OR_LOCKED(type);
1213212151

@@ -12264,7 +12283,14 @@ update_one_slot(PyTypeObject *type, pytype_slotdef *p, pytype_slotdef **next_p,
1226412283
}
1226512284
if (p->function == slot_tp_call) {
1226612285
/* A generic __call__ is incompatible with vectorcall */
12267-
type_clear_flags(type, Py_TPFLAGS_HAVE_VECTORCALL);
12286+
if (changed != NULL) {
12287+
if (type->tp_flags & Py_TPFLAGS_HAVE_VECTORCALL) {
12288+
*changed = true;
12289+
}
12290+
}
12291+
else {
12292+
type_clear_flags(type, Py_TPFLAGS_HAVE_VECTORCALL);
12293+
}
1226812294
}
1226912295
}
1227012296
PyStackRef_CLOSE(descr_ref);
@@ -12277,8 +12303,15 @@ update_one_slot(PyTypeObject *type, pytype_slotdef *p, pytype_slotdef **next_p,
1227712303
slot_value = generic;
1227812304
}
1227912305

12280-
ASSERT_WORLD_STOPPED_OR_NEW_TYPE(type);
12281-
*ptr = slot_value;
12306+
if (changed != NULL) {
12307+
if (*ptr != slot_value) {
12308+
*changed = true;
12309+
}
12310+
}
12311+
else {
12312+
ASSERT_WORLD_STOPPED_OR_NEW_TYPE(type);
12313+
*ptr = slot_value;
12314+
}
1228212315

1228312316
if (next_p != NULL) {
1228412317
*next_p = p;
@@ -12293,17 +12326,23 @@ update_slots_callback(PyTypeObject *type, void *data)
1229312326
ASSERT_NEW_TYPE_OR_LOCKED(type);
1229412327

1229512328
update_callback_data_t *update_data = (update_callback_data_t *)data;
12329+
bool *changed = update_data->changed;
1229612330
pytype_slotdef **pp = update_data->defs;
1229712331
for (; *pp; pp++) {
12298-
update_one_slot(type, *pp, NULL, NULL);
12332+
if (changed != NULL && *changed) {
12333+
break; // already known that an update is needed
12334+
}
12335+
update_one_slot(type, *pp, NULL, NULL, changed);
1229912336
}
1230012337
return 0;
1230112338
}
1230212339

1230312340
/* Update the slots after assignment to a class (type) attribute. In the
12304-
free-threaded build, this is called with the world stopped. */
12341+
free-threaded build, this is called with the world stopped. If changed is
12342+
not NULL, nothing is updated and *changed is set to true if any slot of the
12343+
type or its subclasses would change. */
1230512344
static int
12306-
update_slot(PyTypeObject *type, PyObject *name)
12345+
update_slot(PyTypeObject *type, PyObject *name, bool *changed)
1230712346
{
1230812347
pytype_slotdef *ptrs[MAX_EQUIV];
1230912348
pytype_slotdef *p;
@@ -12332,6 +12371,7 @@ update_slot(PyTypeObject *type, PyObject *name)
1233212371

1233312372
update_callback_data_t callback_data;
1233412373
callback_data.defs = ptrs;
12374+
callback_data.changed = changed;
1233512375
return update_subclasses(type, name,
1233612376
update_slots_callback, (void *)&callback_data);
1233712377
}
@@ -12352,7 +12392,7 @@ fixup_slot_dispatchers(PyTypeObject *type)
1235212392
descrs = NULL;
1235312393
}
1235412394
for (pytype_slotdef *p = slotdefs; p->name; ) {
12355-
update_one_slot(type, p, &p, descrs);
12395+
update_one_slot(type, p, &p, descrs, NULL);
1235612396
}
1235712397
if (descrs != NULL) {
1235812398
clear_slot_descrs(descrs);
@@ -12367,7 +12407,7 @@ update_all_slots(PyTypeObject* type)
1236712407
types_stop_world();
1236812408
for (pytype_slotdef *p = slotdefs; p->name; p++) {
1236912409
/* update_slot returns int but can't actually fail in this case*/
12370-
update_slot(type, p->name_strobj);
12410+
update_slot(type, p->name_strobj, NULL);
1237112411
}
1237212412
types_start_world();
1237312413
// Invalidate the version tags after the slots are updated, while
@@ -12535,8 +12575,12 @@ recurse_down_subclasses(PyTypeObject *type, PyObject *attr_name,
1253512575
// With the world stopped, use borrowed references to the subclasses,
1253612576
// so that dropping a reference cannot run type_dealloc(). Look up
1253712577
// attr_name without calling __eq__ of non-str keys, consistent with
12538-
// find_slot_name_in_mro().
12539-
assert(types_world_is_stopped());
12578+
// find_slot_name_in_mro(). This is also done with only TYPE_LOCK held
12579+
// when checking for slot changes (see update_slot()). That is safe
12580+
// too: type_dealloc() removes the type from tp_subclasses, which
12581+
// requires TYPE_LOCK, before it tears down the type, and the type
12582+
// dicts are only modified with TYPE_LOCK held.
12583+
ASSERT_TYPE_LOCK_HELD();
1254012584
if (_PyWeakref_IS_DEAD(ref)) {
1254112585
continue;
1254212586
}

0 commit comments

Comments
 (0)