Skip to content

Commit a154c9e

Browse files
authored
gh-134584: Eliminate redundant refcounting from _CALL_STR_1 (GH-136070)
Signed-off-by: Manjusaka <[email protected]>
1 parent d3ef5ba commit a154c9e

File tree

10 files changed

+58
-43
lines changed

10 files changed

+58
-43
lines changed

Include/internal/pycore_opcode_metadata.h

Lines changed: 2 additions & 2 deletions
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: 4 additions & 4 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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,6 +1957,20 @@ def testfunc(n):
19571957
self.assertNotIn("_GUARD_NOS_NULL", uops)
19581958
self.assertNotIn("_GUARD_CALLABLE_STR_1", uops)
19591959

1960+
def test_call_str_1_pop_top(self):
1961+
def testfunc(n):
1962+
x = 0
1963+
for _ in range(n):
1964+
t = str("")
1965+
x += 1 if len(t) == 0 else 0
1966+
return x
1967+
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
1968+
self.assertEqual(res, TIER2_THRESHOLD)
1969+
self.assertIsNotNone(ex)
1970+
uops = get_opnames(ex)
1971+
self.assertIn("_CALL_STR_1", uops)
1972+
self.assertIn("_POP_TOP_NOP", uops)
1973+
19601974
def test_call_str_1_result_is_str(self):
19611975
def testfunc(n):
19621976
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: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4040,18 +4040,17 @@ dummy_func(
40404040
DEOPT_IF(callable_o != (PyObject *)&PyUnicode_Type);
40414041
}
40424042

4043-
op(_CALL_STR_1, (callable, null, arg -- res)) {
4043+
op(_CALL_STR_1, (callable, null, arg -- res, a)) {
40444044
PyObject *arg_o = PyStackRef_AsPyObjectBorrow(arg);
40454045

40464046
assert(oparg == 1);
40474047
STAT_INC(CALL, hit);
40484048
PyObject *res_o = PyObject_Str(arg_o);
4049-
DEAD(null);
4050-
DEAD(callable);
4051-
(void)callable; // Silence compiler warnings about unused variables
4052-
(void)null;
4053-
PyStackRef_CLOSE(arg);
4054-
ERROR_IF(res_o == NULL);
4049+
if (res_o == NULL) {
4050+
ERROR_NO_POP();
4051+
}
4052+
a = arg;
4053+
INPUTS_DEAD();
40554054
res = PyStackRef_FromPyObjectSteal(res_o);
40564055
}
40574056

@@ -4061,6 +4060,7 @@ dummy_func(
40614060
_GUARD_NOS_NULL +
40624061
_GUARD_CALLABLE_STR_1 +
40634062
_CALL_STR_1 +
4063+
POP_TOP +
40644064
_CHECK_PERIODIC_AT_END;
40654065

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

Python/executor_cases.c.h

Lines changed: 9 additions & 16 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 & 10 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
@@ -951,7 +951,7 @@ dummy_func(void) {
951951
}
952952
}
953953

954-
op(_CALL_STR_1, (unused, unused, arg -- res)) {
954+
op(_CALL_STR_1, (unused, unused, arg -- res, a)) {
955955
if (sym_matches_type(arg, &PyUnicode_Type)) {
956956
// e.g. str('foo') or str(foo) where foo is known to be a string
957957
// Note: we must strip the reference information because it goes
@@ -961,6 +961,7 @@ dummy_func(void) {
961961
else {
962962
res = sym_new_type(ctx, &PyUnicode_Type);
963963
}
964+
a = arg;
964965
}
965966

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

Python/optimizer_cases.c.h

Lines changed: 5 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)