Skip to content

Commit d1e3a2b

Browse files
committed
fix review idea
Signed-off-by: Manjusaka <[email protected]>
1 parent 69b929b commit d1e3a2b

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

Lib/test/test_capi/test_opt.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1942,33 +1942,49 @@ def testfunc(n):
19421942

19431943
def test_get_len_with_non_const_tuple(self):
19441944
def testfunc(n):
1945-
x = ""
1945+
x = 0.0
19461946
for _ in range(n):
19471947
match (object(), object()):
19481948
case [_, _]:
1949-
x += "1"
1949+
x += 1.0
19501950
return x
19511951
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
1952-
self.assertEqual(len(res), TIER2_THRESHOLD)
1952+
self.assertEqual(int(res), TIER2_THRESHOLD)
19531953
uops = get_opnames(ex)
19541954
self.assertNotIn("_GUARD_NOS_INT", uops)
19551955
self.assertIn("_GET_LEN", uops)
19561956
self.assertIn("_POP_TOP_LOAD_CONST_INLINE_BORROW", uops)
19571957

19581958
def test_get_len_with_non_tuple(self):
19591959
def testfunc(n):
1960-
x = ""
1960+
x = 0.0
19611961
for _ in range(n):
19621962
match [1, 2, 3, 4]:
19631963
case [_, _, _, _]:
1964-
x += "1"
1964+
x += 1.0
19651965
return x
19661966
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
1967-
self.assertEqual(len(res), TIER2_THRESHOLD)
1967+
self.assertEqual(int(res), TIER2_THRESHOLD)
19681968
uops = get_opnames(ex)
19691969
self.assertNotIn("_GUARD_NOS_INT", uops)
19701970
self.assertIn("_GET_LEN", uops)
1971-
self.assertNotIn("_POP_TOP_LOAD_CONST_INLINE_BORROW", uops)
1971+
1972+
def test_get_len_with_immortal_tuple(self):
1973+
def testfunc(n):
1974+
class TestObject(tuple):
1975+
pass
1976+
x = 0.0
1977+
for _ in range(n):
1978+
match TestObject.__mro__:
1979+
case (_, _, _,):
1980+
x += 1.0
1981+
return x
1982+
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
1983+
self.assertEqual(int(res), TIER2_THRESHOLD)
1984+
uops = get_opnames(ex)
1985+
self.assertIn("_GET_LEN", uops)
1986+
self.assertIn("_LOAD_CONST_INLINE_BORROW", uops)
1987+
19721988

19731989
def test_binary_op_subscr_tuple_int(self):
19741990
def testfunc(n):

Python/optimizer_bytecodes.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "pycore_uops.h"
44
#include "pycore_uop_ids.h"
55
#include "internal/pycore_moduleobject.h"
6+
#include "refcount.h"
67

78
#define op(name, ...) /* NAME is ignored */
89

@@ -1128,6 +1129,9 @@ dummy_func(void) {
11281129
goto error;
11291130
}
11301131
len = sym_new_const(ctx, temp);
1132+
if (_Py_IsImmortal(temp)) {
1133+
REPLACE_OP(this_instr, _LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)temp);
1134+
}
11311135
Py_DECREF(temp);
11321136
}
11331137
}

Python/optimizer_cases.c.h

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