Skip to content

Commit 8fae6ec

Browse files
committed
Add maybe_enable_deferred_ref_count().
If we are specializing to LOAD_GLOBAL_MODULE, set deferred reference counting for the value, if it meets criteria. For now, it's only done for frozenset objects.
1 parent 27a2e49 commit 8fae6ec

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Python/specialize.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,6 +1264,24 @@ specialize_attr_loadclassattr(PyObject *owner, _Py_CODEUNIT *instr,
12641264
return 1;
12651265
}
12661266

1267+
#ifdef Py_GIL_DISABLED
1268+
static void
1269+
maybe_enable_deferred_ref_count(PyObject *globals, PyObject *name)
1270+
{
1271+
PyObject *value;
1272+
if (PyDict_GetItemRef(globals, name, &value) != 1) {
1273+
return;
1274+
}
1275+
if (!PyType_IS_GC(Py_TYPE(value)) || _PyObject_HasDeferredRefcount(value)) {
1276+
Py_DECREF(value);
1277+
return;
1278+
}
1279+
if (PyFrozenSet_Check(value)) {
1280+
PyUnstable_Object_EnableDeferredRefcount(value);
1281+
}
1282+
Py_DECREF(value);
1283+
}
1284+
#endif
12671285

12681286
static void
12691287
specialize_load_global_lock_held(
@@ -1305,6 +1323,9 @@ specialize_load_global_lock_held(
13051323
SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_OUT_OF_RANGE);
13061324
goto fail;
13071325
}
1326+
#ifdef Py_GIL_DISABLED
1327+
maybe_enable_deferred_ref_count(globals, name);
1328+
#endif
13081329
cache->index = (uint16_t)index;
13091330
cache->module_keys_version = (uint16_t)keys_version;
13101331
specialize(instr, LOAD_GLOBAL_MODULE);

0 commit comments

Comments
 (0)