]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-134584: Eliminate redundant refcounting from `_STORE_ATTR_INSTANCE_VALUE` (GH...
authorNadeshiko Manju <me@manjusaka.me>
Mon, 15 Dec 2025 20:17:12 +0000 (04:17 +0800)
committerGitHub <noreply@github.com>
Mon, 15 Dec 2025 20:17:12 +0000 (20:17 +0000)
Signed-off-by: Manjusaka <me@manjusaka.me>
Include/internal/pycore_opcode_metadata.h
Include/internal/pycore_uop_ids.h
Include/internal/pycore_uop_metadata.h
Lib/test/test_capi/test_opt.py
Misc/NEWS.d/next/Core_and_Builtins/2025-12-16-01-17-21.gh-issue-134584.tsxYYw.rst [new file with mode: 0644]
Python/bytecodes.c
Python/executor_cases.c.h
Python/generated_cases.c.h
Python/optimizer_bytecodes.c
Python/optimizer_cases.c.h

index f658a4f62fc406dab4051fd91cd4ec02788b41f5..15e35fd53605e32709c45815ef0ecbf507c279dc 100644 (file)
@@ -1482,7 +1482,7 @@ _PyOpcode_macro_expansion[256] = {
     [SET_FUNCTION_ATTRIBUTE] = { .nuops = 1, .uops = { { _SET_FUNCTION_ATTRIBUTE, OPARG_SIMPLE, 0 } } },
     [SET_UPDATE] = { .nuops = 1, .uops = { { _SET_UPDATE, OPARG_SIMPLE, 0 } } },
     [STORE_ATTR] = { .nuops = 1, .uops = { { _STORE_ATTR, OPARG_SIMPLE, 3 } } },
-    [STORE_ATTR_INSTANCE_VALUE] = { .nuops = 3, .uops = { { _GUARD_TYPE_VERSION_AND_LOCK, 2, 1 }, { _GUARD_DORV_NO_DICT, OPARG_SIMPLE, 3 }, { _STORE_ATTR_INSTANCE_VALUE, 1, 3 } } },
+    [STORE_ATTR_INSTANCE_VALUE] = { .nuops = 4, .uops = { { _GUARD_TYPE_VERSION_AND_LOCK, 2, 1 }, { _GUARD_DORV_NO_DICT, OPARG_SIMPLE, 3 }, { _STORE_ATTR_INSTANCE_VALUE, 1, 3 }, { _POP_TOP, OPARG_SIMPLE, 4 } } },
     [STORE_ATTR_SLOT] = { .nuops = 2, .uops = { { _GUARD_TYPE_VERSION, 2, 1 }, { _STORE_ATTR_SLOT, 1, 3 } } },
     [STORE_ATTR_WITH_HINT] = { .nuops = 2, .uops = { { _GUARD_TYPE_VERSION, 2, 1 }, { _STORE_ATTR_WITH_HINT, 1, 3 } } },
     [STORE_DEREF] = { .nuops = 1, .uops = { { _STORE_DEREF, OPARG_SIMPLE, 0 } } },
index 7a3a735d1e63ac231d15158caebb357846937556..f2c92668e079a99b95073f404c5a5f74b384c69f 100644 (file)
@@ -1037,7 +1037,7 @@ extern "C" {
 #define _SPILL_OR_RELOAD_r32 1230
 #define _START_EXECUTOR_r00 1231
 #define _STORE_ATTR_r20 1232
-#define _STORE_ATTR_INSTANCE_VALUE_r20 1233
+#define _STORE_ATTR_INSTANCE_VALUE_r21 1233
 #define _STORE_ATTR_SLOT_r20 1234
 #define _STORE_ATTR_WITH_HINT_r20 1235
 #define _STORE_DEREF_r10 1236
index c5fae89cd176c85f7abee83f03ce48b6332d7415..97e7478ab3f0b48979bc3ad9ae5253105a68ee27 100644 (file)
@@ -1828,7 +1828,7 @@ const _PyUopCachingInfo _PyUop_Caching[MAX_UOP_ID+1] = {
         .entries = {
             { -1, -1, -1 },
             { -1, -1, -1 },
-            { 0, 2, _STORE_ATTR_INSTANCE_VALUE_r20 },
+            { 1, 2, _STORE_ATTR_INSTANCE_VALUE_r21 },
             { -1, -1, -1 },
         },
     },
@@ -3546,7 +3546,7 @@ const uint16_t _PyUop_Uncached[MAX_UOP_REGS_ID+1] = {
     [_GUARD_DORV_NO_DICT_r11] = _GUARD_DORV_NO_DICT,
     [_GUARD_DORV_NO_DICT_r22] = _GUARD_DORV_NO_DICT,
     [_GUARD_DORV_NO_DICT_r33] = _GUARD_DORV_NO_DICT,
-    [_STORE_ATTR_INSTANCE_VALUE_r20] = _STORE_ATTR_INSTANCE_VALUE,
+    [_STORE_ATTR_INSTANCE_VALUE_r21] = _STORE_ATTR_INSTANCE_VALUE,
     [_STORE_ATTR_WITH_HINT_r20] = _STORE_ATTR_WITH_HINT,
     [_STORE_ATTR_SLOT_r20] = _STORE_ATTR_SLOT,
     [_COMPARE_OP_r21] = _COMPARE_OP,
@@ -4784,7 +4784,7 @@ const char *const _PyOpcode_uop_name[MAX_UOP_REGS_ID+1] = {
     [_STORE_ATTR] = "_STORE_ATTR",
     [_STORE_ATTR_r20] = "_STORE_ATTR_r20",
     [_STORE_ATTR_INSTANCE_VALUE] = "_STORE_ATTR_INSTANCE_VALUE",
-    [_STORE_ATTR_INSTANCE_VALUE_r20] = "_STORE_ATTR_INSTANCE_VALUE_r20",
+    [_STORE_ATTR_INSTANCE_VALUE_r21] = "_STORE_ATTR_INSTANCE_VALUE_r21",
     [_STORE_ATTR_SLOT] = "_STORE_ATTR_SLOT",
     [_STORE_ATTR_SLOT_r20] = "_STORE_ATTR_SLOT_r20",
     [_STORE_ATTR_WITH_HINT] = "_STORE_ATTR_WITH_HINT",
index 69a2a0e0edba8c28370ffec2fc591cdc602175d5..cfb5d6ef91cb5bded4d052e726d76e0df7a1088a 100644 (file)
@@ -2511,6 +2511,23 @@ class TestUopsOptimization(unittest.TestCase):
         self.assertNotIn("_GUARD_TOS_INT", uops)
         self.assertNotIn("_GUARD_NOS_INT", uops)
 
+    def test_store_attr_instance_value(self):
+        def testfunc(n):
+            class C:
+                pass
+            c = C()
+            for i in range(n):
+                c.a = i
+            return c.a
+        res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
+        self.assertEqual(res, TIER2_THRESHOLD - 1)
+        self.assertIsNotNone(ex)
+        uops = get_opnames(ex)
+
+        self.assertIn("_STORE_ATTR_INSTANCE_VALUE", uops)
+        self.assertNotIn("_POP_TOP", uops)
+        self.assertIn("_POP_TOP_NOP", uops)
+
     def test_store_subscr_int(self):
         def testfunc(n):
             l = [0, 0, 0, 0]
diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-12-16-01-17-21.gh-issue-134584.tsxYYw.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-12-16-01-17-21.gh-issue-134584.tsxYYw.rst
new file mode 100644 (file)
index 0000000..66bafbf
--- /dev/null
@@ -0,0 +1 @@
+Eliminate redundant refcounting from ``_STORE_ATTR_INSTANCE_VALUE``.
index ee28c69b57b1d50532d68516dfe5563868e1e53f..cf5bf0b6769e2a374f3818ba61aa131de5137882 100644 (file)
@@ -2575,7 +2575,7 @@ dummy_func(
             }
         }
 
-        op(_STORE_ATTR_INSTANCE_VALUE, (offset/1, value, owner --)) {
+        op(_STORE_ATTR_INSTANCE_VALUE, (offset/1, value, owner -- o)) {
             PyObject *owner_o = PyStackRef_AsPyObjectBorrow(owner);
 
             STAT_INC(STORE_ATTR, hit);
@@ -2589,7 +2589,8 @@ dummy_func(
                 _PyDictValues_AddToInsertionOrder(values, index);
             }
             UNLOCK_OBJECT(owner_o);
-            PyStackRef_CLOSE(owner);
+            o = owner;
+            DEAD(owner);
             Py_XDECREF(old_value);
         }
 
@@ -2597,7 +2598,8 @@ dummy_func(
             unused/1 +
             _GUARD_TYPE_VERSION_AND_LOCK +
             _GUARD_DORV_NO_DICT +
-            _STORE_ATTR_INSTANCE_VALUE;
+            _STORE_ATTR_INSTANCE_VALUE +
+            POP_TOP;
 
         op(_STORE_ATTR_WITH_HINT, (hint/1, value, owner --)) {
             PyObject *owner_o = PyStackRef_AsPyObjectBorrow(owner);
index b879da7a1714a520e5786cc396d62869c9cf2447..d8f33bc979710d251042f08fe9198645cc32e97c 100644 (file)
             break;
         }
 
-        case _STORE_ATTR_INSTANCE_VALUE_r20: {
+        case _STORE_ATTR_INSTANCE_VALUE_r21: {
             CHECK_CURRENT_CACHED_VALUES(2);
             assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
             _PyStackRef owner;
             _PyStackRef value;
+            _PyStackRef o;
             _PyStackRef _stack_item_0 = _tos_cache0;
             _PyStackRef _stack_item_1 = _tos_cache1;
             owner = _stack_item_1;
                 _PyDictValues_AddToInsertionOrder(values, index);
             }
             UNLOCK_OBJECT(owner_o);
+            o = owner;
+            stack_pointer[0] = o;
+            stack_pointer += 1;
+            ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
             _PyFrame_SetStackPointer(frame, stack_pointer);
-            PyStackRef_CLOSE(owner);
             Py_XDECREF(old_value);
             stack_pointer = _PyFrame_GetStackPointer(frame);
-            _tos_cache0 = PyStackRef_ZERO_BITS;
+            _tos_cache0 = o;
             _tos_cache1 = PyStackRef_ZERO_BITS;
             _tos_cache2 = PyStackRef_ZERO_BITS;
-            SET_CURRENT_CACHED_VALUES(0);
+            SET_CURRENT_CACHED_VALUES(1);
+            stack_pointer += -1;
+            ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
             assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
             break;
         }
index 40d02dd12a0130c386380b51edf50c8c72eae707..1e7778c7e752bbe111717db7f1d20a3c1223bbf3 100644 (file)
             static_assert(INLINE_CACHE_ENTRIES_STORE_ATTR == 4, "incorrect cache size");
             _PyStackRef owner;
             _PyStackRef value;
+            _PyStackRef o;
             /* Skip 1 cache entry */
             // _GUARD_TYPE_VERSION_AND_LOCK
             {
                     _PyDictValues_AddToInsertionOrder(values, index);
                 }
                 UNLOCK_OBJECT(owner_o);
-                stack_pointer += -2;
+                o = owner;
+                stack_pointer[-2] = o;
+                stack_pointer += -1;
                 ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
                 _PyFrame_SetStackPointer(frame, stack_pointer);
-                PyStackRef_CLOSE(owner);
                 Py_XDECREF(old_value);
                 stack_pointer = _PyFrame_GetStackPointer(frame);
             }
+            // _POP_TOP
+            {
+                value = o;
+                stack_pointer += -1;
+                ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
+                _PyFrame_SetStackPointer(frame, stack_pointer);
+                PyStackRef_XCLOSE(value);
+                stack_pointer = _PyFrame_GetStackPointer(frame);
+            }
             DISPATCH();
         }
 
index deb28f0e367b0e4389f4e037c246f46a39e696ae..9e9af6186c4df5142fd5525e696ecb78cd05610e 100644 (file)
@@ -99,6 +99,11 @@ dummy_func(void) {
         GETLOCAL(oparg) = temp;
     }
 
+    op(_STORE_ATTR_INSTANCE_VALUE, (offset/1, value, owner -- o)) {
+        (void)value;
+        o = owner;
+    }
+
     op(_STORE_FAST, (value --)) {
         GETLOCAL(oparg) = value;
     }
index 4c5a98a1fa17ab4ce325e37080cc16a2e532e328..b79e9125d6bf2ea737ad4786fd0ccc658666e664 100644 (file)
         }
 
         case _STORE_ATTR_INSTANCE_VALUE: {
-            CHECK_STACK_BOUNDS(-2);
-            stack_pointer += -2;
+            JitOptRef owner;
+            JitOptRef value;
+            JitOptRef o;
+            owner = stack_pointer[-1];
+            value = stack_pointer[-2];
+            uint16_t offset = (uint16_t)this_instr->operand0;
+            (void)value;
+            o = owner;
+            CHECK_STACK_BOUNDS(-1);
+            stack_pointer[-2] = o;
+            stack_pointer += -1;
             ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
             break;
         }