Skip to content

Commit a2a400a

Browse files
gh-134584: Eliminate redundant refcounting from _CALL_BUILTION_O (GH-142695)
Co-authored-by: Ken Jin <kenjin4096@gmail.com>
1 parent 11aef21 commit a2a400a

File tree

9 files changed

+74
-38
lines changed

9 files changed

+74
-38
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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2133,6 +2133,21 @@ class C:
21332133
self.assertNotIn("_COMPARE_OP_INT", uops)
21342134
self.assertNotIn("_GUARD_IS_TRUE_POP", uops)
21352135

2136+
def test_call_builtin_o(self):
2137+
def testfunc(n):
2138+
x = 0
2139+
for _ in range(n):
2140+
y = abs(1)
2141+
x += y
2142+
return x
2143+
2144+
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
2145+
self.assertEqual(res, TIER2_THRESHOLD)
2146+
self.assertIsNotNone(ex)
2147+
uops = get_opnames(ex)
2148+
self.assertIn("_CALL_BUILTIN_O", uops)
2149+
self.assertIn("_POP_TOP", uops)
2150+
21362151
def test_get_len_with_const_tuple(self):
21372152
def testfunc(n):
21382153
x = 0.0
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Eliminate redundant refcounting from ``_CALL_BUILTIN_O``.

Python/bytecodes.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4186,7 +4186,7 @@ dummy_func(
41864186
_CALL_BUILTIN_CLASS +
41874187
_CHECK_PERIODIC_AT_END;
41884188

4189-
op(_CALL_BUILTIN_O, (callable, self_or_null, args[oparg] -- res)) {
4189+
op(_CALL_BUILTIN_O, (callable, self_or_null, args[oparg] -- res, a, c)) {
41904190
/* Builtin METH_O functions */
41914191
PyObject *callable_o = PyStackRef_AsPyObjectBorrow(callable);
41924192

@@ -4206,19 +4206,21 @@ dummy_func(
42064206
PyObject *res_o = _PyCFunction_TrampolineCall(cfunc, PyCFunction_GET_SELF(callable_o), PyStackRef_AsPyObjectBorrow(arg));
42074207
_Py_LeaveRecursiveCallTstate(tstate);
42084208
assert((res_o != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
4209-
4210-
PyStackRef_CLOSE(arg);
4211-
DEAD(args);
4212-
DEAD(self_or_null);
4213-
PyStackRef_CLOSE(callable);
4214-
ERROR_IF(res_o == NULL);
4209+
if (res_o == NULL) {
4210+
ERROR_NO_POP();
4211+
}
4212+
a = arg;
4213+
c = callable;
4214+
INPUTS_DEAD();
42154215
res = PyStackRef_FromPyObjectSteal(res_o);
42164216
}
42174217

42184218
macro(CALL_BUILTIN_O) =
42194219
unused/1 +
42204220
unused/2 +
42214221
_CALL_BUILTIN_O +
4222+
POP_TOP +
4223+
POP_TOP +
42224224
_CHECK_PERIODIC_AT_END;
42234225

42244226
op(_CALL_BUILTIN_FAST, (callable, self_or_null, args[oparg] -- res)) {

Python/executor_cases.c.h

Lines changed: 10 additions & 12 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: 24 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/optimizer_cases.c.h

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