Skip to content

Commit fc1f6b1

Browse files
gh-GH-131798: optimize jit attribute loads on immutable types (#146449)
1 parent b5a27bd commit fc1f6b1

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

Lib/test/test_capi/test_opt.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2808,9 +2808,6 @@ def testfunc(n):
28082808
self.assertEqual(res, sum(range(TIER2_THRESHOLD)))
28092809
uops = get_opnames(ex)
28102810
self.assertIn("_CALL_LIST_APPEND", uops)
2811-
# We should remove these in the future
2812-
self.assertIn("_GUARD_NOS_LIST", uops)
2813-
self.assertIn("_GUARD_CALLABLE_LIST_APPEND", uops)
28142811

28152812
def test_call_list_append_pop_top(self):
28162813
def testfunc(n):
@@ -3046,6 +3043,8 @@ def f(n):
30463043
self.assertEqual(res, TIER2_THRESHOLD)
30473044
uops = get_opnames(ex)
30483045
self.assertNotIn("_LOAD_ATTR_METHOD_NO_DICT", uops)
3046+
self.assertNotIn("_LOAD_CONST_UNDER_INLINE", uops)
3047+
self.assertIn("_LOAD_CONST_UNDER_INLINE_BORROW", uops)
30493048

30503049
def test_store_fast_refcount_elimination(self):
30513050
def foo(x):

Python/optimizer_analysis.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,11 @@ lookup_attr(JitOptContext *ctx, _PyBloomFilter *dependencies, _PyUOpInstruction
367367
if (type && PyType_Check(type)) {
368368
PyObject *lookup = _PyType_Lookup(type, name);
369369
if (lookup) {
370-
int opcode = _Py_IsImmortal(lookup) ? immortal : mortal;
370+
int opcode = mortal;
371+
// if the object is immortal or the type is immutable, borrowing is safe
372+
if (_Py_IsImmortal(lookup) || (type->tp_flags & Py_TPFLAGS_IMMUTABLETYPE)) {
373+
opcode = immortal;
374+
}
371375
ADD_OP(opcode, 0, (uintptr_t)lookup);
372376
PyType_Watch(TYPE_WATCHER_ID, (PyObject *)type);
373377
_Py_BloomFilter_Add(dependencies, type);

0 commit comments

Comments
 (0)