Skip to content

Commit 671a27d

Browse files
committed
gh-149217: Avoid adding dependencies on immutable, immortal classes in the JIT
1 parent bb911a2 commit 671a27d

3 files changed

Lines changed: 24 additions & 28 deletions

File tree

Python/optimizer_analysis.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,16 @@ type_watcher_callback(PyTypeObject* type)
156156
return 0;
157157
}
158158

159+
static void
160+
watch_type(PyTypeObject *type, _PyBloomFilter *filter)
161+
{
162+
if (_Py_IsImmortal(type) && (type->tp_flags & Py_TPFLAGS_IMMUTABLETYPE)) {
163+
return;
164+
}
165+
PyType_Watch(TYPE_WATCHER_ID, (PyObject *)type);
166+
_Py_BloomFilter_Add(filter, type);
167+
}
168+
159169
static PyObject *
160170
convert_global_to_const(_PyUOpInstruction *inst, PyObject *obj)
161171
{
@@ -367,8 +377,7 @@ optimize_dict_known_hash(
367377
// for user-defined objects which don't override tp_hash
368378
Py_hash_t hash = PyObject_Hash(sub);
369379
ADD_OP(opcode, 0, hash);
370-
PyType_Watch(TYPE_WATCHER_ID, (PyObject *)Py_TYPE(sub));
371-
_Py_BloomFilter_Add(dependencies, Py_TYPE(sub));
380+
watch_type(Py_TYPE(sub), dependencies);
372381
}
373382
}
374383

@@ -401,8 +410,7 @@ lookup_attr(JitOptContext *ctx, _PyBloomFilter *dependencies, _PyUOpInstruction
401410
ADD_OP(suffix, 2, 0);
402411
}
403412
if ((type->tp_flags & Py_TPFLAGS_IMMUTABLETYPE) == 0) {
404-
PyType_Watch(TYPE_WATCHER_ID, (PyObject *)type);
405-
_Py_BloomFilter_Add(dependencies, type);
413+
watch_type(type, dependencies);
406414
}
407415
return sym_new_const(ctx, lookup);
408416
}
@@ -473,10 +481,8 @@ lookup_super_attr(JitOptContext *ctx, _PyBloomFilter *dependencies,
473481
}
474482
// if obj_type is immutable, then all its superclasses are immutable
475483
if ((obj_type->tp_flags & Py_TPFLAGS_IMMUTABLETYPE) == 0) {
476-
PyType_Watch(TYPE_WATCHER_ID, (PyObject *)su_type);
477-
_Py_BloomFilter_Add(dependencies, su_type);
478-
PyType_Watch(TYPE_WATCHER_ID, (PyObject *)obj_type);
479-
_Py_BloomFilter_Add(dependencies, obj_type);
484+
watch_type(su_type, dependencies);
485+
watch_type(obj_type, dependencies);
480486
}
481487
return sym_new_const_steal(ctx, lookup);
482488
}

Python/optimizer_bytecodes.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,7 @@ dummy_func(void) {
148148
sym_set_type(owner, probable_type);
149149
sym_set_type_version(owner, type_version);
150150
if ((probable_type->tp_flags & Py_TPFLAGS_IMMUTABLETYPE) == 0) {
151-
PyType_Watch(TYPE_WATCHER_ID, (PyObject *)probable_type);
152-
_Py_BloomFilter_Add(dependencies, probable_type);
151+
watch_type(probable_type, dependencies);
153152
}
154153
}
155154
else {
@@ -237,8 +236,7 @@ dummy_func(void) {
237236
else {
238237
sym_set_const(owner, type);
239238
if ((((PyTypeObject *)type)->tp_flags & Py_TPFLAGS_IMMUTABLETYPE) == 0) {
240-
PyType_Watch(TYPE_WATCHER_ID, type);
241-
_Py_BloomFilter_Add(dependencies, type);
239+
watch_type((PyTypeObject *)type, dependencies);
242240
}
243241
}
244242
}
@@ -256,8 +254,7 @@ dummy_func(void) {
256254
probable_type->tp_version_tag == type_version) {
257255
sym_set_type(owner, probable_type);
258256
sym_set_type_version(owner, type_version);
259-
PyType_Watch(TYPE_WATCHER_ID, (PyObject *)probable_type);
260-
_Py_BloomFilter_Add(dependencies, probable_type);
257+
watch_type(probable_type, dependencies);
261258
}
262259
else {
263260
ctx->contradiction = true;
@@ -1318,8 +1315,7 @@ dummy_func(void) {
13181315
assert(init != NULL);
13191316
assert(PyFunction_Check(init));
13201317
callable = sym_new_const(ctx, init);
1321-
PyType_Watch(TYPE_WATCHER_ID, callable_o);
1322-
_Py_BloomFilter_Add(dependencies, callable_o);;
1318+
watch_type((PyTypeObject *)callable_o, dependencies);
13231319
}
13241320
else {
13251321
callable = sym_new_not_null(ctx);
@@ -2026,8 +2022,7 @@ dummy_func(void) {
20262022
ADD_OP(_SWAP, 3, 0);
20272023
optimize_pop_top(ctx, this_instr, method_and_self[0]);
20282024
if ((type->tp_flags & Py_TPFLAGS_IMMUTABLETYPE) == 0) {
2029-
PyType_Watch(TYPE_WATCHER_ID, (PyObject *)type);
2030-
_Py_BloomFilter_Add(dependencies, type);
2025+
watch_type(type, dependencies);
20312026
}
20322027
method_and_self[0] = sym_new_const(ctx, descr);
20332028
optimized = true;

Python/optimizer_cases.c.h

Lines changed: 5 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)