Skip to content

Commit afaf58b

Browse files
authored
gh-131798: JIT: Optimize _CHECK_IS_NOT_PY_CALLABLE_EX and _CHECK_IS_NOT_PY_CALLABLE_KW (GH-148494)
1 parent 88e378c commit afaf58b

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-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
@@ -1320,6 +1320,20 @@ dummy_func(void) {
13201320
}
13211321
}
13221322

1323+
op(_CHECK_IS_NOT_PY_CALLABLE_EX, (func_st, unused, unused, unused -- func_st, unused, unused, unused)) {
1324+
PyTypeObject *type = sym_get_type(func_st);
1325+
if (type && type != &PyFunction_Type) {
1326+
ADD_OP(_NOP, 0, 0);
1327+
}
1328+
}
1329+
1330+
op(_CHECK_IS_NOT_PY_CALLABLE_KW, (callable, unused, unused[oparg], unused -- callable, unused, unused[oparg], unused)) {
1331+
PyTypeObject *type = sym_get_type(callable);
1332+
if (type && type != &PyFunction_Type && type != &PyMethod_Type) {
1333+
ADD_OP(_NOP, 0, 0);
1334+
}
1335+
}
1336+
13231337
op(_PUSH_FRAME, (new_frame -- )) {
13241338
SYNC_SP();
13251339
if (!CURRENT_FRAME_IS_INIT_SHIM()) {

Python/optimizer_cases.c.h

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)