File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -2880,6 +2880,29 @@ def testfunc(n):
28802880 self .assertIn ("_POP_TOP_NOP" , uops )
28812881 self .assertLessEqual (count_ops (ex , "_POP_TOP" ), 2 )
28822882
2883+ def test_binary_op_refcount_elimination (self ):
2884+ class CustomAdder :
2885+ def __init__ (self , val ):
2886+ self .val = val
2887+ def __add__ (self , other ):
2888+ return CustomAdder (self .val + other .val )
2889+
2890+ def testfunc (n ):
2891+ a = CustomAdder (1 )
2892+ b = CustomAdder (2 )
2893+ res = None
2894+ for _ in range (n ):
2895+ res = a + b
2896+ return res .val if res else 0
2897+
2898+ res , ex = self ._run_with_optimizer (testfunc , TIER2_THRESHOLD )
2899+ self .assertEqual (res , 3 )
2900+ self .assertIsNotNone (ex )
2901+ uops = get_opnames (ex )
2902+ self .assertIn ("_BINARY_OP" , uops )
2903+ self .assertIn ("_POP_TOP_NOP" , uops )
2904+ self .assertLessEqual (count_ops (ex , "_POP_TOP" ), 2 )
2905+
28832906 def test_remove_guard_for_slice_list (self ):
28842907 def f (n ):
28852908 for i in range (n ):
You can’t perform that action at this time.
0 commit comments