From 34bf5460352030cc369c47c0f08e7c0b10d5fd11 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Tue, 25 Jun 2024 02:24:58 -0500 Subject: [PATCH] Fast case for count from zero --- Modules/_collectionsmodule.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c index 644a90a8c71099..641d57a64c8357 100644 --- a/Modules/_collectionsmodule.c +++ b/Modules/_collectionsmodule.c @@ -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;