Skip to content

Commit 488512b

Browse files
authored
A (very) slight speed improvement for iterating over bytes (#21705)
My mentee @xvxvxvxvxv noticed that iterating over array.array is slightly faster than iterating over bytes. Looking at the source I observed that arrayiter_next() calls `getitem(ao, it->index++)` wheras striter_next() uses the idiom (paraphrased) item = PyLong_FromLong(seq->ob_sval[it->it_index]); if (item != NULL) ++it->it_next; return item; I'm not 100% sure but I think that the second version has fewer opportunity for the CPU to overlap the `index++` operation with the rest of the code (which in both cases involves a call). So here I am optimistically incrementing the index -- if the PyLong_FromLong() call fails, this will leave the iterator pointing at the next byte, but honestly I doubt that anyone would seriously consider resuming use of the iterator after that kind of failure (it would have to be a MemoryError). And the author of arrayiter_next() made the same consideration (or never ever gave it a thought :-). With this, a loop like for _ in b: pass is now slightly *faster* than the same thing over an equivalent array, rather than slightly *slower* (in both cases a few percent).
1 parent c36dbac commit 488512b

File tree

1 file changed

+2
-6
lines changed

1 file changed

+2
-6
lines changed

Objects/bytesobject.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3139,7 +3139,6 @@ static PyObject *
31393139
striter_next(striterobject *it)
31403140
{
31413141
PyBytesObject *seq;
3142-
PyObject *item;
31433142

31443143
assert(it != NULL);
31453144
seq = it->it_seq;
@@ -3148,11 +3147,8 @@ striter_next(striterobject *it)
31483147
assert(PyBytes_Check(seq));
31493148

31503149
if (it->it_index < PyBytes_GET_SIZE(seq)) {
3151-
item = PyLong_FromLong(
3152-
(unsigned char)seq->ob_sval[it->it_index]);
3153-
if (item != NULL)
3154-
++it->it_index;
3155-
return item;
3150+
return PyLong_FromLong(
3151+
(unsigned char)seq->ob_sval[it->it_index++]);
31563152
}
31573153

31583154
it->it_seq = NULL;

0 commit comments

Comments
 (0)