Sujet : Re: More Funny Stuff From Python
De : ldo (at) *nospam* nz.invalid (Lawrence D'Oliveiro)
Groupes : comp.os.linux.advocacyDate : 16. Jun 2024, 03:02:05
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v4ldic$3n6db$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
User-Agent : Pan/0.158 (Avdiivka; )
On Fri, 14 Jun 2024 14:14:33 -0400, DFS wrote:
Here's how python checks array bound every time the
array is accessed:
int PyList_SetItem(PyObject *op, Py_ssize_t i,
PyObject *newitem)
{
PyObject **p;
if (!PyList_Check(op)) {
Py_XDECREF(newitem); PyErr_BadInternalCall();
return -1;
}
if (!valid_index(i, Py_SIZE(op))) {
Py_XDECREF(newitem); PyErr_SetString(PyExc_IndexError,
"list assignment index out of range");
return -1;
}
p = ((PyListObject *)op) -> ob_item + i;
Py_XSETREF(*p, newitem);
return 0;
}
Python-3.11.0.tgz.2\Python-3.11.0\Objects\listobject.c line 257
Which is another thing that contributes to its slowness vs C.
From the same source file:
static inline int
valid_index(Py_ssize_t i, Py_ssize_t limit)
{
/* The cast to size_t lets us use just a single comparison
to check whether i is in the range: 0 <= i < limit.
See: Section 14.2 "Bounds Checking" in the Agner Fog
optimization manual found at:
https://www.agner.org/optimize/optimizing_cpp.pdf */
return (size_t) i < (size_t) limit;
}
Somehow I don’t think that’s the bottleneck in PyList_SetItem.
Also have a look at the list_extend routine, since that is commonly used
for appending lots of items.