Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1790,3 +1790,4 @@ Jelle Zijlstra
Gennadiy Zlobin
Doug Zongker
Peter Åstrand
Jeethu Rao
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Faster list.insert for lists with 32 or more elements in them. Patch by Jeethu Rao.
17 changes: 13 additions & 4 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,12 @@ PyList_SetItem(PyObject *op, Py_ssize_t i,
return 0;
}


/* Threshold at which to switch to using
* memmove instead of a for loop in ins1
*/
#define INS1_MEMMOVE_THRESHOLD 32

static int
ins1(PyListObject *self, Py_ssize_t where, PyObject *v)
{
Expand All @@ -256,12 +262,15 @@ ins1(PyListObject *self, Py_ssize_t where, PyObject *v)
where += n;
if (where < 0)
where = 0;
}
if (where > n)
} else if (where > n)
where = n;
items = self->ob_item;
for (i = n; --i >= where; )
items[i+1] = items[i];
if (n <= INS1_MEMMOVE_THRESHOLD)
for (i = n; --i >= where; )
items[i+1] = items[i];
else
memmove(&items[where+1], &items[where],
sizeof(PyObject*) * (n - where));
Py_INCREF(v);
items[where] = v;
return 0;
Expand Down