diff options
author | Cheryl Sabella <cheryl.sabella@gmail.com> | 2019-04-05 10:08:43 (GMT) |
---|---|---|
committer | Inada Naoki <songofacandy@gmail.com> | 2019-04-05 10:08:43 (GMT) |
commit | f66e336f455b5a6bb0ca857d61c43be410d0df13 (patch) | |
tree | e95bc9d4f381c4772ca14e47646503ce00907b61 | |
parent | 6a8c3139ae9ada89d4a95985ec7cf8bb7d03bc01 (diff) | |
download | cpython-f66e336f455b5a6bb0ca857d61c43be410d0df13.zip cpython-f66e336f455b5a6bb0ca857d61c43be410d0df13.tar.gz cpython-f66e336f455b5a6bb0ca857d61c43be410d0df13.tar.bz2 |
bpo-29202: improve dict iteration (GH-11900)
Use fewer iterations instead of iterating over the whole entry table.
-rw-r--r-- | Objects/dictobject.c | 15 |
1 files changed, 6 insertions, 9 deletions
diff --git a/Objects/dictobject.c b/Objects/dictobject.c index bba27dd..c1187c2 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -2147,7 +2147,7 @@ dict_keys(PyDictObject *mp) PyObject *v; Py_ssize_t i, j; PyDictKeyEntry *ep; - Py_ssize_t size, n, offset; + Py_ssize_t n, offset; PyObject **value_ptr; again: @@ -2163,7 +2163,6 @@ dict_keys(PyDictObject *mp) goto again; } ep = DK_ENTRIES(mp->ma_keys); - size = mp->ma_keys->dk_nentries; if (mp->ma_values) { value_ptr = mp->ma_values; offset = sizeof(PyObject *); @@ -2172,7 +2171,7 @@ dict_keys(PyDictObject *mp) value_ptr = &ep[0].me_value; offset = sizeof(PyDictKeyEntry); } - for (i = 0, j = 0; i < size; i++) { + for (i = 0, j = 0; j < n; i++) { if (*value_ptr != NULL) { PyObject *key = ep[i].me_key; Py_INCREF(key); @@ -2191,7 +2190,7 @@ dict_values(PyDictObject *mp) PyObject *v; Py_ssize_t i, j; PyDictKeyEntry *ep; - Py_ssize_t size, n, offset; + Py_ssize_t n, offset; PyObject **value_ptr; again: @@ -2207,7 +2206,6 @@ dict_values(PyDictObject *mp) goto again; } ep = DK_ENTRIES(mp->ma_keys); - size = mp->ma_keys->dk_nentries; if (mp->ma_values) { value_ptr = mp->ma_values; offset = sizeof(PyObject *); @@ -2216,7 +2214,7 @@ dict_values(PyDictObject *mp) value_ptr = &ep[0].me_value; offset = sizeof(PyDictKeyEntry); } - for (i = 0, j = 0; i < size; i++) { + for (i = 0, j = 0; j < n; i++) { PyObject *value = *value_ptr; value_ptr = (PyObject **)(((char *)value_ptr) + offset); if (value != NULL) { @@ -2234,7 +2232,7 @@ dict_items(PyDictObject *mp) { PyObject *v; Py_ssize_t i, j, n; - Py_ssize_t size, offset; + Py_ssize_t offset; PyObject *item, *key; PyDictKeyEntry *ep; PyObject **value_ptr; @@ -2265,7 +2263,6 @@ dict_items(PyDictObject *mp) } /* Nothing we do below makes any function calls. */ ep = DK_ENTRIES(mp->ma_keys); - size = mp->ma_keys->dk_nentries; if (mp->ma_values) { value_ptr = mp->ma_values; offset = sizeof(PyObject *); @@ -2274,7 +2271,7 @@ dict_items(PyDictObject *mp) value_ptr = &ep[0].me_value; offset = sizeof(PyDictKeyEntry); } - for (i = 0, j = 0; i < size; i++) { + for (i = 0, j = 0; j < n; i++) { PyObject *value = *value_ptr; value_ptr = (PyObject **)(((char *)value_ptr) + offset); if (value != NULL) { |