Skip to content

Commit b51bdcd

Browse files
committed
gh-131798: JIT: Optimize _CHECK_IS_NOT_PY_CALLABLE_KW and _CHECK_IS_NOT_PY_CALLABLE_EX
1 parent 0274d83 commit b51bdcd

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

Lib/test/test_capi/test_opt.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2845,6 +2845,35 @@ def testfunc(n):
28452845
uops = get_opnames(ex)
28462846
self.assertNotIn("_CHECK_IS_NOT_PY_CALLABLE", uops)
28472847

2848+
def test_check_is_not_py_callable_ex(self):
2849+
def testfunc(n):
2850+
total = 0
2851+
xs = (1, 2, 3)
2852+
args = (xs,)
2853+
for _ in range(n):
2854+
total += len(*args)
2855+
return total
2856+
2857+
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
2858+
self.assertEqual(res, 3 * TIER2_THRESHOLD)
2859+
self.assertIsNotNone(ex)
2860+
uops = get_opnames(ex)
2861+
self.assertNotIn("_CHECK_IS_NOT_PY_CALLABLE_EX", uops)
2862+
2863+
def test_check_is_not_py_callable_kw(self):
2864+
def testfunc(n):
2865+
total = 0
2866+
xs = (3, 1, 2)
2867+
for _ in range(n):
2868+
total += sorted(xs, reverse=False)[0]
2869+
return total
2870+
2871+
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
2872+
self.assertEqual(res, TIER2_THRESHOLD)
2873+
self.assertIsNotNone(ex)
2874+
uops = get_opnames(ex)
2875+
self.assertNotIn("_CHECK_IS_NOT_PY_CALLABLE_KW", uops)
2876+
28482877
def test_call_len_string(self):
28492878
def testfunc(n):
28502879
for _ in range(n):

Python/optimizer_bytecodes.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,20 @@ dummy_func(void) {
12731273
}
12741274
}
12751275

1276+
op(_CHECK_IS_NOT_PY_CALLABLE_EX, (func_st, unused, unused, unused -- func_st, unused, unused, unused)) {
1277+
PyTypeObject *type = sym_get_type(func_st);
1278+
if (type && type != &PyFunction_Type) {
1279+
ADD_OP(_NOP, 0, 0);
1280+
}
1281+
}
1282+
1283+
op(_CHECK_IS_NOT_PY_CALLABLE_KW, (callable, unused, unused[oparg], unused -- callable, unused, unused[oparg], unused)) {
1284+
PyTypeObject *type = sym_get_type(callable);
1285+
if (type && type != &PyFunction_Type && type != &PyMethod_Type) {
1286+
ADD_OP(_NOP, 0, 0);
1287+
}
1288+
}
1289+
12761290
op(_PUSH_FRAME, (new_frame -- )) {
12771291
SYNC_SP();
12781292
if (!CURRENT_FRAME_IS_INIT_SHIM()) {

0 commit comments

Comments
 (0)