@@ -2378,6 +2378,24 @@ def testfunc(n):
23782378 self .assertIn ("_BINARY_OP_SUBSCR_DICT_KNOWN_HASH" , uops )
23792379 self .assertNotIn ("_BINARY_OP_SUBSCR_DICT" , uops )
23802380
2381+ def test_binary_op_subscr_defaultdict_known_hash (self ):
2382+ # str, int, bytes, float, complex, tuple and any python object which has generic hash
2383+ import collections
2384+
2385+ def testfunc (n ):
2386+ x = 0
2387+ d = collections .defaultdict (lambda : 1 )
2388+ for _ in range (n ):
2389+ x += d ['a' ] + d [1 ] + d [b'b' ] + d [(1 , 2 )] + d [_GENERIC_KEY ] + d [1.5 ] + d [1 + 2j ]
2390+ return x
2391+
2392+ res , ex = self ._run_with_optimizer (testfunc , TIER2_THRESHOLD )
2393+ self .assertEqual (res , 7 * TIER2_THRESHOLD )
2394+ self .assertIsNotNone (ex )
2395+ uops = get_opnames (ex )
2396+ self .assertIn ("_BINARY_OP_SUBSCR_DICT_KNOWN_HASH" , uops )
2397+ self .assertNotIn ("_BINARY_OP_SUBSCR_DICT" , uops )
2398+
23812399 def test_store_subscr_dict_known_hash (self ):
23822400 # str, int, bytes, float, complex, tuple and any python object which has generic hash
23832401 def testfunc (n ):
@@ -2399,6 +2417,28 @@ def testfunc(n):
23992417 self .assertIn ("_STORE_SUBSCR_DICT_KNOWN_HASH" , uops )
24002418 self .assertNotIn ("_STORE_SUBSCR_DICT" , uops )
24012419
2420+ def test_store_subscr_defaultdict_known_hash (self ):
2421+ import collections
2422+
2423+ def testfunc (n ):
2424+ d = collections .defaultdict (lambda : 0 )
2425+ for _ in range (n ):
2426+ d ['a' ] += 1
2427+ d [1 ] += 2
2428+ d [b'b' ] += 3
2429+ d [(1 , 2 )] += 4
2430+ d [_GENERIC_KEY ] += 5
2431+ d [1.5 ] += 6
2432+ d [1 + 2j ] += 7
2433+ return d ['a' ] + d [1 ] + d [b'b' ] + d [(1 , 2 )] + d [_GENERIC_KEY ] + d [1.5 ] + d [1 + 2j ]
2434+
2435+ res , ex = self ._run_with_optimizer (testfunc , TIER2_THRESHOLD )
2436+ self .assertEqual (res , 28 * TIER2_THRESHOLD )
2437+ self .assertIsNotNone (ex )
2438+ uops = get_opnames (ex )
2439+ self .assertIn ("_STORE_SUBSCR_DICT_KNOWN_HASH" , uops )
2440+ self .assertNotIn ("_STORE_SUBSCR_DICT" , uops )
2441+
24022442 def test_contains_op (self ):
24032443 def testfunc (n ):
24042444 x = 0
0 commit comments