summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2020-10-27 21:24:33 (GMT)
committerGitHub <noreply@github.com>2020-10-27 21:24:33 (GMT)
commit35b95aaf21534e4a8e3370dfd6f7482265316c9e (patch)
tree1f0bb3bbeb1c8389b1cd43408f17e0d17c748b7d
parentc310185c081110741fae914c06c7aaf673ad3d0d (diff)
downloadcpython-35b95aaf21534e4a8e3370dfd6f7482265316c9e.zip
cpython-35b95aaf21534e4a8e3370dfd6f7482265316c9e.tar.gz
cpython-35b95aaf21534e4a8e3370dfd6f7482265316c9e.tar.bz2
bpo-42161: Micro-optimize _collections._count_elements() (GH-23008)
Move the _PyLong_GetOne() call outside the fast-path loop.
-rw-r--r--Modules/_collectionsmodule.c9
1 files changed, 5 insertions, 4 deletions
diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c
index 8990071..1578750 100644
--- a/Modules/_collectionsmodule.c
+++ b/Modules/_collectionsmodule.c
@@ -2278,6 +2278,7 @@ _collections__count_elements_impl(PyObject *module, PyObject *mapping,
PyObject *dict_get;
PyObject *mapping_setitem;
PyObject *dict_setitem;
+ PyObject *one = _PyLong_GetOne(); // borrowed reference
it = PyObject_GetIter(iterable);
if (it == NULL)
@@ -2324,10 +2325,10 @@ _collections__count_elements_impl(PyObject *module, PyObject *mapping,
if (oldval == NULL) {
if (PyErr_Occurred())
goto done;
- if (_PyDict_SetItem_KnownHash(mapping, key, _PyLong_GetOne(), hash) < 0)
+ if (_PyDict_SetItem_KnownHash(mapping, key, one, hash) < 0)
goto done;
} else {
- newval = PyNumber_Add(oldval, _PyLong_GetOne());
+ newval = PyNumber_Add(oldval, one);
if (newval == NULL)
goto done;
if (_PyDict_SetItem_KnownHash(mapping, key, newval, hash) < 0)
@@ -2336,13 +2337,13 @@ _collections__count_elements_impl(PyObject *module, PyObject *mapping,
}
Py_DECREF(key);
}
- } else {
+ }
+ else {
bound_get = _PyObject_GetAttrId(mapping, &PyId_get);
if (bound_get == NULL)
goto done;
PyObject *zero = _PyLong_GetZero(); // borrowed reference
- PyObject *one = _PyLong_GetOne(); // borrowed reference
while (1) {
key = PyIter_Next(it);
if (key == NULL)