Skip to content

Commit 27e5bc8

Browse files
committed
gh-134584: Eliminate redundant refcounting from _STORE_ATTR_WITH_HINT
Signed-off-by: Manjusaka <me@manjusaka.me>
1 parent 059316a commit 27e5bc8

File tree

10 files changed

+68
-17
lines changed

10 files changed

+68
-17
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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2528,6 +2528,25 @@ class C:
25282528
self.assertNotIn("_POP_TOP", uops)
25292529
self.assertIn("_POP_TOP_NOP", uops)
25302530

2531+
def test_store_attr_with_hint(self):
2532+
def testfunc(n):
2533+
class C:
2534+
pass
2535+
c = C()
2536+
for i in range(_testinternalcapi.SHARED_KEYS_MAX_SIZE - 1):
2537+
setattr(c, f"_{i}", None)
2538+
2539+
for i in range(n):
2540+
c.x = i
2541+
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
2542+
self.assertEqual(res, None)
2543+
self.assertIsNotNone(ex)
2544+
uops = get_opnames(ex)
2545+
2546+
self.assertIn("_STORE_ATTR_WITH_HINT", uops)
2547+
self.assertNotIn("_POP_TOP", uops)
2548+
self.assertIn("_POP_TOP_NOP", uops)
2549+
25312550
def test_store_subscr_int(self):
25322551
def testfunc(n):
25332552
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_WITH_HINT``.

Python/bytecodes.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2601,7 +2601,7 @@ dummy_func(
26012601
_STORE_ATTR_INSTANCE_VALUE +
26022602
POP_TOP;
26032603

2604-
op(_STORE_ATTR_WITH_HINT, (hint/1, value, owner --)) {
2604+
op(_STORE_ATTR_WITH_HINT, (hint/1, value, owner -- o)) {
26052605
PyObject *owner_o = PyStackRef_AsPyObjectBorrow(owner);
26062606
assert(Py_TYPE(owner_o)->tp_flags & Py_TPFLAGS_MANAGED_DICT);
26072607
PyDictObject *dict = _PyObject_GetManagedDict(owner_o);
@@ -2631,14 +2631,16 @@ dummy_func(
26312631
// old_value should be DECREFed after GC track checking is done, if not, it could raise a segmentation fault,
26322632
// when dict only holds the strong reference to value in ep->me_value.
26332633
STAT_INC(STORE_ATTR, hit);
2634-
PyStackRef_CLOSE(owner);
2634+
o = owner;
2635+
DEAD(owner);
26352636
Py_XDECREF(old_value);
26362637
}
26372638

26382639
macro(STORE_ATTR_WITH_HINT) =
26392640
unused/1 +
26402641
_GUARD_TYPE_VERSION +
2641-
_STORE_ATTR_WITH_HINT;
2642+
_STORE_ATTR_WITH_HINT +
2643+
POP_TOP;
26422644

26432645
op(_STORE_ATTR_SLOT, (index/1, value, owner --)) {
26442646
PyObject *owner_o = PyStackRef_AsPyObjectBorrow(owner);

Python/executor_cases.c.h

Lines changed: 9 additions & 5 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
@@ -104,6 +104,11 @@ dummy_func(void) {
104104
o = owner;
105105
}
106106

107+
op(_STORE_ATTR_WITH_HINT, (hint/1, value, owner -- o)) {
108+
(void)value;
109+
o = owner;
110+
}
111+
107112
op(_STORE_FAST, (value --)) {
108113
GETLOCAL(oparg) = value;
109114
}

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)