Kaydet (Commit) 4b0b1acc authored tarafından Raymond Hettinger's avatar Raymond Hettinger

Issue #21101: Eliminate double hashing in the C code for collections.Counter().

üst 4b74fba6
...@@ -73,6 +73,9 @@ Library ...@@ -73,6 +73,9 @@ Library
Decimal.quantize() method in the Python version. It had never been Decimal.quantize() method in the Python version. It had never been
present in the C version. present in the C version.
- Issue #21101: Eliminate double hashing in the C speed-up code for
collections.Counter().
- Issue #21321: itertools.islice() now releases the reference to the source - Issue #21321: itertools.islice() now releases the reference to the source
iterator when the slice is exhausted. Patch by Anton Afanasyev. iterator when the slice is exhausted. Patch by Anton Afanasyev.
......
...@@ -1831,18 +1831,29 @@ _count_elements(PyObject *self, PyObject *args) ...@@ -1831,18 +1831,29 @@ _count_elements(PyObject *self, PyObject *args)
if (mapping_get != NULL && mapping_get == dict_get && if (mapping_get != NULL && mapping_get == dict_get &&
mapping_setitem != NULL && mapping_setitem == dict_setitem) { mapping_setitem != NULL && mapping_setitem == dict_setitem) {
while (1) { while (1) {
Py_hash_t hash;
key = PyIter_Next(it); key = PyIter_Next(it);
if (key == NULL) if (key == NULL)
break; break;
oldval = PyDict_GetItem(mapping, key);
if (!PyUnicode_CheckExact(key) ||
(hash = ((PyASCIIObject *) key)->hash) == -1)
{
hash = PyObject_Hash(key);
if (hash == -1)
goto done;
}
oldval = _PyDict_GetItem_KnownHash(mapping, key, hash);
if (oldval == NULL) { if (oldval == NULL) {
if (PyDict_SetItem(mapping, key, one) == -1) if (_PyDict_SetItem_KnownHash(mapping, key, one, hash) == -1)
break; break;
} else { } else {
newval = PyNumber_Add(oldval, one); newval = PyNumber_Add(oldval, one);
if (newval == NULL) if (newval == NULL)
break; break;
if (PyDict_SetItem(mapping, key, newval) == -1) if (_PyDict_SetItem_KnownHash(mapping, key, newval, hash) == -1)
break; break;
Py_CLEAR(newval); Py_CLEAR(newval);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment