Add fast path in count_elements (gh-120983)

This commit is contained in:
Raymond Hettinger 2024-06-25 03:10:00 -05:00 committed by GitHub
parent bb057ea107
commit 9b32b89074
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 5 additions and 1 deletions

View File

@ -2575,7 +2575,11 @@ _collections__count_elements_impl(PyObject *module, PyObject *mapping,
oldval = PyObject_CallFunctionObjArgs(bound_get, key, zero, NULL);
if (oldval == NULL)
break;
newval = PyNumber_Add(oldval, one);
if (oldval == zero) {
newval = Py_NewRef(one);
} else {
newval = PyNumber_Add(oldval, one);
}
Py_DECREF(oldval);
if (newval == NULL)
break;