Skip to content

Commit ea4ae79

Browse files
optimize floats and complex as well
1 parent 2d1a169 commit ea4ae79

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

Lib/test/test_capi/test_opt.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2321,35 +2321,37 @@ def testfunc(n):
23212321
self.assertLessEqual(count_ops(ex, "_POP_TOP"), 2)
23222322

23232323
def test_binary_op_subscr_dict_known_hash(self):
2324-
# str, int, bytes, tuple and any python object which has generic hash
2324+
# str, int, bytes, float, complex, tuple and any python object which has generic hash
23252325
def testfunc(n):
23262326
x = 0
2327-
d = {'a': 1, 1: 2, b'b': 3, (1, 2): 4, _GENERIC_KEY: 5}
2327+
d = {'a': 1, 1: 2, b'b': 3, (1, 2): 4, _GENERIC_KEY: 5, 1.5: 6, 1+2j: 7}
23282328
for _ in range(n):
2329-
x += d['a'] + d[1] + d[b'b'] + d[(1, 2)] + d[_GENERIC_KEY]
2329+
x += d['a'] + d[1] + d[b'b'] + d[(1, 2)] + d[_GENERIC_KEY] + d[1.5] + d[1+2j]
23302330
return x
23312331

23322332
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
2333-
self.assertEqual(res, 15 * TIER2_THRESHOLD)
2333+
self.assertEqual(res, 28 * TIER2_THRESHOLD)
23342334
self.assertIsNotNone(ex)
23352335
uops = get_opnames(ex)
23362336
self.assertIn("_BINARY_OP_SUBSCR_DICT_KNOWN_HASH", uops)
23372337
self.assertNotIn("_BINARY_OP_SUBSCR_DICT", uops)
23382338

23392339
def test_store_subscr_dict_known_hash(self):
2340-
# str, int, bytes, tuple and any python object which has generic hash
2340+
# str, int, bytes, float, complex, tuple and any python object which has generic hash
23412341
def testfunc(n):
2342-
d = {'a': 0, 1: 0, b'b': 0, (1, 2): 0, _GENERIC_KEY: 0}
2342+
d = {'a': 0, 1: 0, b'b': 0, (1, 2): 0, _GENERIC_KEY: 0, 1.5: 0, 1+2j: 0}
23432343
for _ in range(n):
23442344
d['a'] += 1
23452345
d[1] += 2
23462346
d[b'b'] += 3
23472347
d[(1, 2)] += 4
23482348
d[_GENERIC_KEY] += 5
2349-
return d['a'] + d[1] + d[b'b'] + d[(1, 2)] + d[_GENERIC_KEY]
2349+
d[1.5] += 6
2350+
d[1+2j] += 7
2351+
return d['a'] + d[1] + d[b'b'] + d[(1, 2)] + d[_GENERIC_KEY] + d[1.5] + d[1+2j]
23502352

23512353
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
2352-
self.assertEqual(res, 15 * TIER2_THRESHOLD)
2354+
self.assertEqual(res, 28 * TIER2_THRESHOLD)
23532355
self.assertIsNotNone(ex)
23542356
uops = get_opnames(ex)
23552357
self.assertIn("_STORE_SUBSCR_DICT_KNOWN_HASH", uops)

Python/optimizer_bytecodes.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ dummy_func(void) {
167167
op(_STORE_SUBSCR_DICT, (value, dict_st, sub -- st)) {
168168
PyObject *sub_o = sym_get_const(ctx, sub);
169169
if (sub_o != NULL) {
170-
if (PyUnicode_CheckExact(sub_o) || PyLong_CheckExact(sub_o) || PyBytes_CheckExact(sub_o)) {
170+
if (PyUnicode_CheckExact(sub_o) || PyLong_CheckExact(sub_o) || PyBytes_CheckExact(sub_o)
171+
|| PyFloat_CheckExact(sub_o) || PyComplex_CheckExact(sub_o)) {
171172
// PyObject_Hash can't fail on these types
172173
ADD_OP(_STORE_SUBSCR_DICT_KNOWN_HASH, 0, PyObject_Hash(sub_o));
173174
}
@@ -506,7 +507,8 @@ dummy_func(void) {
506507
op(_BINARY_OP_SUBSCR_DICT, (dict_st, sub_st -- res, ds, ss)) {
507508
PyObject *sub = sym_get_const(ctx, sub_st);
508509
if (sub != NULL) {
509-
if (PyUnicode_CheckExact(sub) || PyLong_CheckExact(sub) || PyBytes_CheckExact(sub)) {
510+
if (PyUnicode_CheckExact(sub) || PyLong_CheckExact(sub) || PyBytes_CheckExact(sub)
511+
|| PyFloat_CheckExact(sub) || PyComplex_CheckExact(sub)) {
510512
// PyObject_Hash can't fail on these types
511513
ADD_OP(_BINARY_OP_SUBSCR_DICT_KNOWN_HASH, 0, PyObject_Hash(sub));
512514
}

Python/optimizer_cases.c.h

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)