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 <tekknolagi@gmail.com>
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

Lines changed: 12 additions & 0 deletions
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
Lines changed: 1 addition & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy