Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8d490b3

Browse files
authored
GH-131798: Narrow the return type of isinstance for some known arguments in the JIT (GH-133172)
1 parent9859791 commit8d490b3

File tree

4 files changed

+152
-1
lines changed

4 files changed

+152
-1
lines changed

‎Lib/test/test_capi/test_opt.py

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1959,6 +1959,121 @@ def testfunc(n):
19591959
self.assertNotIn("_GUARD_THIRD_NULL",uops)
19601960
self.assertNotIn("_GUARD_CALLABLE_ISINSTANCE",uops)
19611961

1962+
deftest_call_isinstance_is_true(self):
1963+
deftestfunc(n):
1964+
x=0
1965+
for_inrange(n):
1966+
y=isinstance(42,int)
1967+
ify:
1968+
x+=1
1969+
returnx
1970+
1971+
res,ex=self._run_with_optimizer(testfunc,TIER2_THRESHOLD)
1972+
self.assertEqual(res,TIER2_THRESHOLD)
1973+
self.assertIsNotNone(ex)
1974+
uops=get_opnames(ex)
1975+
self.assertIn("_CALL_ISINSTANCE",uops)
1976+
self.assertNotIn("_TO_BOOL_BOOL",uops)
1977+
self.assertNotIn("_GUARD_IS_TRUE_POP",uops)
1978+
1979+
deftest_call_isinstance_is_false(self):
1980+
deftestfunc(n):
1981+
x=0
1982+
for_inrange(n):
1983+
y=isinstance(42,str)
1984+
ifnoty:
1985+
x+=1
1986+
returnx
1987+
1988+
res,ex=self._run_with_optimizer(testfunc,TIER2_THRESHOLD)
1989+
self.assertEqual(res,TIER2_THRESHOLD)
1990+
self.assertIsNotNone(ex)
1991+
uops=get_opnames(ex)
1992+
self.assertIn("_CALL_ISINSTANCE",uops)
1993+
self.assertNotIn("_TO_BOOL_BOOL",uops)
1994+
self.assertNotIn("_GUARD_IS_FALSE_POP",uops)
1995+
1996+
deftest_call_isinstance_subclass(self):
1997+
deftestfunc(n):
1998+
x=0
1999+
for_inrange(n):
2000+
y=isinstance(True,int)
2001+
ify:
2002+
x+=1
2003+
returnx
2004+
2005+
res,ex=self._run_with_optimizer(testfunc,TIER2_THRESHOLD)
2006+
self.assertEqual(res,TIER2_THRESHOLD)
2007+
self.assertIsNotNone(ex)
2008+
uops=get_opnames(ex)
2009+
self.assertIn("_CALL_ISINSTANCE",uops)
2010+
self.assertNotIn("_TO_BOOL_BOOL",uops)
2011+
self.assertNotIn("_GUARD_IS_TRUE_POP",uops)
2012+
2013+
deftest_call_isinstance_unknown_object(self):
2014+
deftestfunc(n):
2015+
x=0
2016+
for_inrange(n):
2017+
# The optimizer doesn't know the return type here:
2018+
bar=eval("42")
2019+
# This will only narrow to bool:
2020+
y=isinstance(bar,int)
2021+
ify:
2022+
x+=1
2023+
returnx
2024+
2025+
res,ex=self._run_with_optimizer(testfunc,TIER2_THRESHOLD)
2026+
self.assertEqual(res,TIER2_THRESHOLD)
2027+
self.assertIsNotNone(ex)
2028+
uops=get_opnames(ex)
2029+
self.assertIn("_CALL_ISINSTANCE",uops)
2030+
self.assertNotIn("_TO_BOOL_BOOL",uops)
2031+
self.assertIn("_GUARD_IS_TRUE_POP",uops)
2032+
2033+
deftest_call_isinstance_tuple_of_classes(self):
2034+
deftestfunc(n):
2035+
x=0
2036+
for_inrange(n):
2037+
# A tuple of classes is currently not optimized,
2038+
# so this is only narrowed to bool:
2039+
y=isinstance(42, (int,str))
2040+
ify:
2041+
x+=1
2042+
returnx
2043+
2044+
res,ex=self._run_with_optimizer(testfunc,TIER2_THRESHOLD)
2045+
self.assertEqual(res,TIER2_THRESHOLD)
2046+
self.assertIsNotNone(ex)
2047+
uops=get_opnames(ex)
2048+
self.assertIn("_CALL_ISINSTANCE",uops)
2049+
self.assertNotIn("_TO_BOOL_BOOL",uops)
2050+
self.assertIn("_GUARD_IS_TRUE_POP",uops)
2051+
2052+
deftest_call_isinstance_metaclass(self):
2053+
classEvenNumberMeta(type):
2054+
def__instancecheck__(self,number):
2055+
returnnumber%2==0
2056+
2057+
classEvenNumber(metaclass=EvenNumberMeta):
2058+
pass
2059+
2060+
deftestfunc(n):
2061+
x=0
2062+
for_inrange(n):
2063+
# Only narrowed to bool
2064+
y=isinstance(42,EvenNumber)
2065+
ify:
2066+
x+=1
2067+
returnx
2068+
2069+
res,ex=self._run_with_optimizer(testfunc,TIER2_THRESHOLD)
2070+
self.assertEqual(res,TIER2_THRESHOLD)
2071+
self.assertIsNotNone(ex)
2072+
uops=get_opnames(ex)
2073+
self.assertIn("_CALL_ISINSTANCE",uops)
2074+
self.assertNotIn("_TO_BOOL_BOOL",uops)
2075+
self.assertIn("_GUARD_IS_TRUE_POP",uops)
2076+
19622077

19632078
defglobal_identity(x):
19642079
returnx
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Narrow the return type and constant-evaluate ``CALL_ISINSTANCE`` for a
2+
subset of known values in the JIT. Patch by Tomas Roun

‎Python/optimizer_bytecodes.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,26 @@ dummy_func(void) {
890890
}
891891
}
892892

893+
op(_CALL_ISINSTANCE, (unused,unused,instance,cls--res)) {
894+
// the result is always a bool, but sometimes we can
895+
// narrow it down to True or False
896+
res=sym_new_type(ctx,&PyBool_Type);
897+
PyTypeObject*inst_type=sym_get_type(instance);
898+
PyTypeObject*cls_o= (PyTypeObject*)sym_get_const(ctx,cls);
899+
if (inst_type&&cls_o&&sym_matches_type(cls,&PyType_Type)) {
900+
// isinstance(inst, cls) where both inst and cls have
901+
// known types, meaning we can deduce either True or False
902+
903+
// The below check is equivalent to PyObject_TypeCheck(inst, cls)
904+
if (inst_type==cls_o||PyType_IsSubtype(inst_type,cls_o)) {
905+
sym_set_const(res,Py_True);
906+
}
907+
else {
908+
sym_set_const(res,Py_False);
909+
}
910+
}
911+
}
912+
893913
op(_GUARD_IS_TRUE_POP, (flag-- )) {
894914
if (sym_is_const(ctx,flag)) {
895915
PyObject*value=sym_get_const(ctx,flag);

‎Python/optimizer_cases.c.h

Lines changed: 15 additions & 1 deletion
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp