Skip to content

Commit 81cd4f4

Browse files
committed
gh-134584: Eliminate redundant refcounting from _CALL_STR_1
Signed-off-by: Manjusaka <me@manjusaka.me>
1 parent 35ecaf9 commit 81cd4f4

File tree

7 files changed

+40
-22
lines changed

7 files changed

+40
-22
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.

Lib/test/test_capi/test_opt.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1858,6 +1858,20 @@ def testfunc(n):
18581858
self.assertNotIn("_GUARD_NOS_NULL", uops)
18591859
self.assertNotIn("_GUARD_CALLABLE_STR_1", uops)
18601860

1861+
def test_call_str_1_pop_top(self):
1862+
def testfunc(n):
1863+
x = 0
1864+
for _ in range(n):
1865+
t = str("")
1866+
x += 1 if len(t) == 0 else 0
1867+
return x
1868+
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
1869+
self.assertEqual(res, TIER2_THRESHOLD)
1870+
self.assertIsNotNone(ex)
1871+
uops = get_opnames(ex)
1872+
self.assertIn("_CALL_STR_1", uops)
1873+
self.assertIn("_POP_TOP_NOP", uops)
1874+
18611875
def test_call_str_1_result_is_str(self):
18621876
def testfunc(n):
18631877
x = 0
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Eliminate redundant refcounting from ``_CALL_STR_1``.

Python/bytecodes.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4055,17 +4055,14 @@ dummy_func(
40554055
DEOPT_IF(callable_o != (PyObject *)&PyUnicode_Type);
40564056
}
40574057

4058-
op(_CALL_STR_1, (callable, null, arg -- res)) {
4058+
op(_CALL_STR_1, (callable, null, arg -- res, a)) {
40594059
PyObject *arg_o = PyStackRef_AsPyObjectBorrow(arg);
40604060

40614061
assert(oparg == 1);
40624062
STAT_INC(CALL, hit);
4063+
INPUTS_DEAD();
40634064
PyObject *res_o = PyObject_Str(arg_o);
4064-
DEAD(null);
4065-
DEAD(callable);
4066-
(void)callable; // Silence compiler warnings about unused variables
4067-
(void)null;
4068-
PyStackRef_CLOSE(arg);
4065+
a = arg;
40694066
ERROR_IF(res_o == NULL);
40704067
res = PyStackRef_FromPyObjectSteal(res_o);
40714068
}
@@ -4076,6 +4073,7 @@ dummy_func(
40764073
_GUARD_NOS_NULL +
40774074
_GUARD_CALLABLE_STR_1 +
40784075
_CALL_STR_1 +
4076+
POP_TOP +
40794077
_CHECK_PERIODIC;
40804078

40814079
op(_GUARD_CALLABLE_TUPLE_1, (callable, unused, unused -- callable, unused, unused)) {

Python/generated_cases.c.h

Lines changed: 14 additions & 13 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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -936,14 +936,15 @@ dummy_func(void) {
936936
}
937937
}
938938

939-
op(_CALL_STR_1, (unused, unused, arg -- res)) {
939+
op(_CALL_STR_1, (unused, unused, arg -- res, a)) {
940940
if (sym_matches_type(arg, &PyUnicode_Type)) {
941941
// e.g. str('foo') or str(foo) where foo is known to be a string
942942
res = arg;
943943
}
944944
else {
945945
res = sym_new_type(ctx, &PyUnicode_Type);
946946
}
947+
a = arg;
947948
}
948949

949950
op(_CALL_ISINSTANCE, (unused, unused, instance, cls -- res)) {

Python/optimizer_cases.c.h

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

0 commit comments

Comments
 (0)