mirror of
https://github.com/python/cpython.git
synced 2025-09-27 02:39:58 +00:00
Small optimizations for list_slice() and list_extend_internal().
* Using addition instead of substraction on array indices allows the compiler to use a fast addressing mode. Saves about 10%. * Using PyTuple_GET_ITEM and PyList_SET_ITEM is about 7% faster than PySequenceFast_GET_ITEM which has to make a list check on every pass.
This commit is contained in:
parent
ebedb2f773
commit
99842b6534
1 changed files with 20 additions and 9 deletions
|
@ -342,7 +342,7 @@ static PyObject *
|
||||||
list_slice(PyListObject *a, int ilow, int ihigh)
|
list_slice(PyListObject *a, int ilow, int ihigh)
|
||||||
{
|
{
|
||||||
PyListObject *np;
|
PyListObject *np;
|
||||||
int i;
|
int i, len;
|
||||||
if (ilow < 0)
|
if (ilow < 0)
|
||||||
ilow = 0;
|
ilow = 0;
|
||||||
else if (ilow > a->ob_size)
|
else if (ilow > a->ob_size)
|
||||||
|
@ -351,13 +351,15 @@ list_slice(PyListObject *a, int ilow, int ihigh)
|
||||||
ihigh = ilow;
|
ihigh = ilow;
|
||||||
else if (ihigh > a->ob_size)
|
else if (ihigh > a->ob_size)
|
||||||
ihigh = a->ob_size;
|
ihigh = a->ob_size;
|
||||||
np = (PyListObject *) PyList_New(ihigh - ilow);
|
len = ihigh - ilow;
|
||||||
|
np = (PyListObject *) PyList_New(len);
|
||||||
if (np == NULL)
|
if (np == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
for (i = ilow; i < ihigh; i++) {
|
|
||||||
PyObject *v = a->ob_item[i];
|
for (i = 0; i < len; i++) {
|
||||||
|
PyObject *v = a->ob_item[i+ilow];
|
||||||
Py_INCREF(v);
|
Py_INCREF(v);
|
||||||
np->ob_item[i - ilow] = v;
|
np->ob_item[i] = v;
|
||||||
}
|
}
|
||||||
return (PyObject *)np;
|
return (PyObject *)np;
|
||||||
}
|
}
|
||||||
|
@ -676,10 +678,19 @@ listextend_internal(PyListObject *self, PyObject *b)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* populate the end of self with b's items */
|
/* populate the end of self with b's items */
|
||||||
for (i = 0; i < blen; i++) {
|
if (PyList_Check(b)) {
|
||||||
PyObject *o = PySequence_Fast_GET_ITEM(b, i);
|
for (i = 0; i < blen; i++) {
|
||||||
Py_INCREF(o);
|
PyObject *o = PyList_GET_ITEM(b, i);
|
||||||
PyList_SET_ITEM(self, i+selflen, o);
|
Py_INCREF(o);
|
||||||
|
PyList_SET_ITEM(self, i+selflen, o);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
assert (PyTuple_Check(b));
|
||||||
|
for (i = 0; i < blen; i++) {
|
||||||
|
PyObject *o = PyTuple_GET_ITEM(b, i);
|
||||||
|
Py_INCREF(o);
|
||||||
|
PyList_SET_ITEM(self, i+selflen, o);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Py_DECREF(b);
|
Py_DECREF(b);
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue