Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c1e4f9d

Browse files
committed
Use a new macro, PySequence_Fast_ITEMS to factor out code common to
three recent optimizations. Aside from reducing code volume, it increases readability.
1 parent 989ddc0 commit c1e4f9d

4 files changed

Lines changed: 19 additions & 16 deletions

File tree

Doc/api/abstract.tex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,13 @@ \section{Sequence Protocol \label{sequence}}
830830
and that \var{i} is within bounds.
831831
\end{cfuncdesc}
832832

833+
\begin{cfuncdesc}{PyObject**}{PySequence_Fast_ITEMS}{PyObject *o}
834+
Return the underlying array of PyObject pointers. Assumes that
835+
\var{o} was returned by \cfunction{PySequence_Fast()} and
836+
\var{o} is not \NULL.
837+
\versionadded{2.4}
838+
\end{cfuncdesc}
839+
833840
\begin{cfuncdesc}{PyObject*}{PySequence_ITEM}{PyObject *o, int i}
834841
Return the \var{i}th element of \var{o} or \NULL{} on failure.
835842
Macro form of \cfunction{PySequence_GetItem()} but without checking

Include/abstract.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,12 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
10161016
need to be corrected for a negative index
10171017
*/
10181018

1019+
#define _PySequence_Fast_ITEMS(sf) \
1020+
(PyList_Check(sf) ? ((PyListObject *)(sf))->ob_item \
1021+
: ((PyTupleObject *)(sf))->ob_item)
1022+
/* Return a pointer to the underlying item array for
1023+
an object retured by PySequence_Fast */
1024+
10191025
PyAPI_FUNC(int) PySequence_Count(PyObject *o, PyObject *value);
10201026

10211027
/*

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,9 @@ Build
412412
C API
413413
-----
414414

415+
- Added a new macro, PySequence_Fast_ITEMS, which retrieves a fast sequence's
416+
underlying array of PyObject pointers. Useful for high speed looping.
417+
415418
- Created a new method flag, METH_COEXIST, which causes a method to be loaded
416419
even if already defined by a slot wrapper. This allows a __contains__
417420
method, for example, to co-exist with a defined sq_contains slot. This

Objects/listobject.c

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -491,12 +491,7 @@ list_ass_slice(PyListObject *a, int ilow, int ihigh, PyObject *v)
491491
if(v_as_SF == NULL)
492492
return -1;
493493
n = PySequence_Fast_GET_SIZE(v_as_SF);
494-
if (PyList_Check(v_as_SF))
495-
vitem = ((PyListObject *)v_as_SF)->ob_item;
496-
else {
497-
assert (PyTuple_Check(v_as_SF));
498-
vitem = ((PyTupleObject *)v_as_SF)->ob_item;
499-
}
494+
vitem = _PySequence_Fast_ITEMS(v_as_SF);
500495
}
501496
if (ilow < 0)
502497
ilow = 0;
@@ -691,12 +686,7 @@ listextend_internal(PyListObject *self, PyObject *b)
691686
}
692687

693688
/* populate the end of self with b's items */
694-
if (PyList_Check(b))
695-
src = ((PyListObject *)b)->ob_item;
696-
else {
697-
assert (PyTuple_Check(b));
698-
src = ((PyTupleObject *)b)->ob_item;
699-
}
689+
src = _PySequence_Fast_ITEMS(b);
700690
dest = self->ob_item + selflen;
701691
for (i = 0; i < blen; i++) {
702692
PyObject *o = src[i];
@@ -2571,10 +2561,7 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
25712561
PyMem_MALLOC(slicelength*sizeof(PyObject*));
25722562

25732563
selfitems = self->ob_item;
2574-
if (PyList_Check(seq))
2575-
seqitems = ((PyListObject *)seq)->ob_item;
2576-
else
2577-
seqitems = ((PyTupleObject *)seq)->ob_item;
2564+
seqitems = _PySequence_Fast_ITEMS(seq);
25782565
for (cur = start, i = 0; i < slicelength;
25792566
cur += step, i++) {
25802567
garbage[i] = selfitems[cur];

0 commit comments

Comments
 (0)