Commit c1e4f9dd authored by Raymond Hettinger's avatar Raymond Hettinger

Use a new macro, PySequence_Fast_ITEMS to factor out code common to

three recent optimizations.  Aside from reducing code volume, it
increases readability.
parent 989ddc07
...@@ -830,6 +830,13 @@ determination. ...@@ -830,6 +830,13 @@ determination.
and that \var{i} is within bounds. and that \var{i} is within bounds.
\end{cfuncdesc} \end{cfuncdesc}
\begin{cfuncdesc}{PyObject**}{PySequence_Fast_ITEMS}{PyObject *o}
Return the underlying array of PyObject pointers. Assumes that
\var{o} was returned by \cfunction{PySequence_Fast()} and
\var{o} is not \NULL.
\versionadded{2.4}
\end{cfuncdesc}
\begin{cfuncdesc}{PyObject*}{PySequence_ITEM}{PyObject *o, int i} \begin{cfuncdesc}{PyObject*}{PySequence_ITEM}{PyObject *o, int i}
Return the \var{i}th element of \var{o} or \NULL{} on failure. Return the \var{i}th element of \var{o} or \NULL{} on failure.
Macro form of \cfunction{PySequence_GetItem()} but without checking Macro form of \cfunction{PySequence_GetItem()} but without checking
......
...@@ -1016,6 +1016,12 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/ ...@@ -1016,6 +1016,12 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
need to be corrected for a negative index need to be corrected for a negative index
*/ */
#define _PySequence_Fast_ITEMS(sf) \
(PyList_Check(sf) ? ((PyListObject *)(sf))->ob_item \
: ((PyTupleObject *)(sf))->ob_item)
/* Return a pointer to the underlying item array for
an object retured by PySequence_Fast */
PyAPI_FUNC(int) PySequence_Count(PyObject *o, PyObject *value); PyAPI_FUNC(int) PySequence_Count(PyObject *o, PyObject *value);
/* /*
......
...@@ -412,6 +412,9 @@ Build ...@@ -412,6 +412,9 @@ Build
C API C API
----- -----
- Added a new macro, PySequence_Fast_ITEMS, which retrieves a fast sequence's
underlying array of PyObject pointers. Useful for high speed looping.
- Created a new method flag, METH_COEXIST, which causes a method to be loaded - Created a new method flag, METH_COEXIST, which causes a method to be loaded
even if already defined by a slot wrapper. This allows a __contains__ even if already defined by a slot wrapper. This allows a __contains__
method, for example, to co-exist with a defined sq_contains slot. This method, for example, to co-exist with a defined sq_contains slot. This
......
...@@ -491,12 +491,7 @@ list_ass_slice(PyListObject *a, int ilow, int ihigh, PyObject *v) ...@@ -491,12 +491,7 @@ list_ass_slice(PyListObject *a, int ilow, int ihigh, PyObject *v)
if(v_as_SF == NULL) if(v_as_SF == NULL)
return -1; return -1;
n = PySequence_Fast_GET_SIZE(v_as_SF); n = PySequence_Fast_GET_SIZE(v_as_SF);
if (PyList_Check(v_as_SF)) vitem = _PySequence_Fast_ITEMS(v_as_SF);
vitem = ((PyListObject *)v_as_SF)->ob_item;
else {
assert (PyTuple_Check(v_as_SF));
vitem = ((PyTupleObject *)v_as_SF)->ob_item;
}
} }
if (ilow < 0) if (ilow < 0)
ilow = 0; ilow = 0;
...@@ -691,12 +686,7 @@ listextend_internal(PyListObject *self, PyObject *b) ...@@ -691,12 +686,7 @@ listextend_internal(PyListObject *self, PyObject *b)
} }
/* populate the end of self with b's items */ /* populate the end of self with b's items */
if (PyList_Check(b)) src = _PySequence_Fast_ITEMS(b);
src = ((PyListObject *)b)->ob_item;
else {
assert (PyTuple_Check(b));
src = ((PyTupleObject *)b)->ob_item;
}
dest = self->ob_item + selflen; dest = self->ob_item + selflen;
for (i = 0; i < blen; i++) { for (i = 0; i < blen; i++) {
PyObject *o = src[i]; PyObject *o = src[i];
...@@ -2571,10 +2561,7 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value) ...@@ -2571,10 +2561,7 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
PyMem_MALLOC(slicelength*sizeof(PyObject*)); PyMem_MALLOC(slicelength*sizeof(PyObject*));
selfitems = self->ob_item; selfitems = self->ob_item;
if (PyList_Check(seq)) seqitems = _PySequence_Fast_ITEMS(seq);
seqitems = ((PyListObject *)seq)->ob_item;
else
seqitems = ((PyTupleObject *)seq)->ob_item;
for (cur = start, i = 0; i < slicelength; for (cur = start, i = 0; i < slicelength;
cur += step, i++) { cur += step, i++) {
garbage[i] = selfitems[cur]; garbage[i] = selfitems[cur];
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment