]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Add fast path in count_elements (gh-120983)
authorRaymond Hettinger <rhettinger@users.noreply.github.com>
Tue, 25 Jun 2024 08:10:00 +0000 (03:10 -0500)
committerGitHub <noreply@github.com>
Tue, 25 Jun 2024 08:10:00 +0000 (03:10 -0500)
Modules/_collectionsmodule.c

index 644a90a8c71099d040f00a36a898ac0476d24dbe..641d57a64c8357942279ffa4773652925cc0659a 100644 (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;