Since the index is always non-negative, use faster unsigned division and modulo.

This commit is contained in:
Raymond Hettinger 2015-02-27 12:42:54 -08:00
parent b44ed82b81
commit da2850f932

View file

@ -142,7 +142,7 @@ typedef struct {
static PyTypeObject deque_type; static PyTypeObject deque_type;
/* XXX Todo: /* XXX Todo:
If aligned memory allocations become available, make the If aligned memory allocations become available, make the
deque object 64 byte aligned so that all of the fields deque object 64 byte aligned so that all of the fields
can be retrieved or updated in a single cache line. can be retrieved or updated in a single cache line.
@ -780,7 +780,9 @@ deque_item(dequeobject *deque, Py_ssize_t i)
b = deque->rightblock; b = deque->rightblock;
} else { } else {
i += deque->leftindex; i += deque->leftindex;
n = i / BLOCKLEN; assert(i >= 0);
n = (Py_ssize_t)((unsigned) i / BLOCKLEN);
i = (Py_ssize_t)((unsigned) i % BLOCKLEN);
i %= BLOCKLEN; i %= BLOCKLEN;
if (index < (Py_SIZE(deque) >> 1)) { if (index < (Py_SIZE(deque) >> 1)) {
b = deque->leftblock; b = deque->leftblock;
@ -1848,7 +1850,7 @@ _count_elements(PyObject *self, PyObject *args)
(hash = ((PyASCIIObject *) key)->hash) == -1) (hash = ((PyASCIIObject *) key)->hash) == -1)
{ {
hash = PyObject_Hash(key); hash = PyObject_Hash(key);
if (hash == -1) if (hash == -1)
goto done; goto done;
} }