Skip to content

Commit 4c20f46

Browse files
gh-131798: JIT: Narrow the return type of _CALL_LEN to int (#132940)
Reduce unnecessary guards whenever `len()` is called and used after. Co-authored-by: Max Bernstein <[email protected]>
1 parent f0485de commit 4c20f46

File tree

4 files changed

+18
-1
lines changed

4 files changed

+18
-1
lines changed

Lib/test/test_capi/test_opt.py

+12
Original file line numberDiff line numberDiff line change
@@ -1911,6 +1911,18 @@ def testfunc(n):
19111911
self.assertNotIn("_COMPARE_OP_INT", uops)
19121912
self.assertNotIn("_GUARD_IS_TRUE_POP", uops)
19131913

1914+
def test_call_len(self):
1915+
def testfunc(n):
1916+
a = [1, 2, 3, 4]
1917+
for _ in range(n):
1918+
_ = len(a) - 1
1919+
1920+
_, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
1921+
uops = get_opnames(ex)
1922+
self.assertNotIn("_GUARD_NOS_INT", uops)
1923+
self.assertNotIn("_GUARD_TOS_INT", uops)
1924+
self.assertIn("_CALL_LEN", uops)
1925+
19141926

19151927
def global_identity(x):
19161928
return x
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Allow the JIT to remove int guards after ``_CALL_LEN`` by setting the return type to int. Patch by Diego Russo

Python/optimizer_bytecodes.c

+4
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,10 @@ dummy_func(void) {
10551055
sym_set_const(callable, (PyObject *)&PyUnicode_Type);
10561056
}
10571057

1058+
op(_CALL_LEN, (callable[1], self_or_null[1], args[oparg] -- res)) {
1059+
res = sym_new_type(ctx, &PyLong_Type);
1060+
}
1061+
10581062
// END BYTECODES //
10591063

10601064
}

Python/optimizer_cases.c.h

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)