Skip to content

Commit 059316a

Browse files
authored
gh-134584: Eliminate redundant refcounting from _STORE_ATTR_INSTANCE_VALUE (GH-142759)
Signed-off-by: Manjusaka <[email protected]>
1 parent 0ac4e6c commit 059316a

File tree

10 files changed

+67
-16
lines changed

10 files changed

+67
-16
lines changed

Include/internal/pycore_opcode_metadata.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_uop_ids.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_uop_metadata.h

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

Lib/test/test_capi/test_opt.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2511,6 +2511,23 @@ def testfunc(n):
25112511
self.assertNotIn("_GUARD_TOS_INT", uops)
25122512
self.assertNotIn("_GUARD_NOS_INT", uops)
25132513

2514+
def test_store_attr_instance_value(self):
2515+
def testfunc(n):
2516+
class C:
2517+
pass
2518+
c = C()
2519+
for i in range(n):
2520+
c.a = i
2521+
return c.a
2522+
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
2523+
self.assertEqual(res, TIER2_THRESHOLD - 1)
2524+
self.assertIsNotNone(ex)
2525+
uops = get_opnames(ex)
2526+
2527+
self.assertIn("_STORE_ATTR_INSTANCE_VALUE", uops)
2528+
self.assertNotIn("_POP_TOP", uops)
2529+
self.assertIn("_POP_TOP_NOP", uops)
2530+
25142531
def test_store_subscr_int(self):
25152532
def testfunc(n):
25162533
l = [0, 0, 0, 0]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Eliminate redundant refcounting from ``_STORE_ATTR_INSTANCE_VALUE``.

Python/bytecodes.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2575,7 +2575,7 @@ dummy_func(
25752575
}
25762576
}
25772577

2578-
op(_STORE_ATTR_INSTANCE_VALUE, (offset/1, value, owner --)) {
2578+
op(_STORE_ATTR_INSTANCE_VALUE, (offset/1, value, owner -- o)) {
25792579
PyObject *owner_o = PyStackRef_AsPyObjectBorrow(owner);
25802580

25812581
STAT_INC(STORE_ATTR, hit);
@@ -2589,15 +2589,17 @@ dummy_func(
25892589
_PyDictValues_AddToInsertionOrder(values, index);
25902590
}
25912591
UNLOCK_OBJECT(owner_o);
2592-
PyStackRef_CLOSE(owner);
2592+
o = owner;
2593+
DEAD(owner);
25932594
Py_XDECREF(old_value);
25942595
}
25952596

25962597
macro(STORE_ATTR_INSTANCE_VALUE) =
25972598
unused/1 +
25982599
_GUARD_TYPE_VERSION_AND_LOCK +
25992600
_GUARD_DORV_NO_DICT +
2600-
_STORE_ATTR_INSTANCE_VALUE;
2601+
_STORE_ATTR_INSTANCE_VALUE +
2602+
POP_TOP;
26012603

26022604
op(_STORE_ATTR_WITH_HINT, (hint/1, value, owner --)) {
26032605
PyObject *owner_o = PyStackRef_AsPyObjectBorrow(owner);

Python/executor_cases.c.h

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

Python/generated_cases.c.h

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

Python/optimizer_bytecodes.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ dummy_func(void) {
9999
GETLOCAL(oparg) = temp;
100100
}
101101

102+
op(_STORE_ATTR_INSTANCE_VALUE, (offset/1, value, owner -- o)) {
103+
(void)value;
104+
o = owner;
105+
}
106+
102107
op(_STORE_FAST, (value --)) {
103108
GETLOCAL(oparg) = value;
104109
}

Python/optimizer_cases.c.h

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

0 commit comments

Comments
 (0)