diff --git a/Include/listobject.h b/Include/listobject.h --- a/Include/listobject.h +++ b/Include/listobject.h @@ -72,6 +72,7 @@ #define PyList_GET_ITEM(op, i) (((PyListObject *)(op))->ob_item[i]) #define PyList_SET_ITEM(op, i, v) (((PyListObject *)(op))->ob_item[i] = (v)) #define PyList_GET_SIZE(op) Py_SIZE(op) +#define _PyList_ITEMS(op) (((PyListObject *)(op))->ob_item) #endif #ifdef __cplusplus diff --git a/Modules/_heapqmodule.c b/Modules/_heapqmodule.c --- a/Modules/_heapqmodule.c +++ b/Modules/_heapqmodule.c @@ -11,7 +11,7 @@ static int siftdown(PyListObject *heap, Py_ssize_t startpos, Py_ssize_t pos) { - PyObject *newitem, *parent; + PyObject *newitem, *parent, **arr; Py_ssize_t parentpos, size; int cmp; @@ -24,10 +24,11 @@ /* Follow the path to the root, moving parents down until finding a place newitem fits. */ - newitem = PyList_GET_ITEM(heap, pos); + arr = _PyList_ITEMS(heap); + newitem = arr[pos]; while (pos > startpos) { parentpos = (pos - 1) >> 1; - parent = PyList_GET_ITEM(heap, parentpos); + parent = arr[parentpos]; cmp = PyObject_RichCompareBool(newitem, parent, Py_LT); if (cmp == -1) return -1; @@ -38,10 +39,10 @@ } if (cmp == 0) break; - parent = PyList_GET_ITEM(heap, parentpos); - newitem = PyList_GET_ITEM(heap, pos); - PyList_SET_ITEM(heap, parentpos, newitem); - PyList_SET_ITEM(heap, pos, parent); + arr = _PyList_ITEMS(heap); + newitem = arr[pos]; + arr[pos] = arr[parentpos]; + arr[parentpos] = newitem; pos = parentpos; } return 0; @@ -51,7 +52,7 @@ siftup(PyListObject *heap, Py_ssize_t pos) { Py_ssize_t startpos, endpos, childpos, limit; - PyObject *tmp1, *tmp2; + PyObject *tmp, **arr; int cmp; assert(PyList_Check(heap)); @@ -63,15 +64,15 @@ } /* Bubble up the smaller child until hitting a leaf. */ + arr = _PyList_ITEMS(heap); limit = endpos / 2; /* smallest pos that has no child */ while (pos < limit) { /* Set childpos to index of smaller child. */ childpos = 2*pos + 1; /* leftmost child position */ if (childpos + 1 < endpos) { - cmp = PyObject_RichCompareBool( - PyList_GET_ITEM(heap, childpos), - PyList_GET_ITEM(heap, childpos + 1), - Py_LT); + cmp = PyObject_RichCompareBool(arr[childpos], + arr[childpos + 1], + Py_LT); if (cmp == -1) return -1; if (cmp == 0) @@ -83,10 +84,10 @@ } } /* Move the smaller child up. */ - tmp1 = PyList_GET_ITEM(heap, childpos); - tmp2 = PyList_GET_ITEM(heap, pos); - PyList_SET_ITEM(heap, childpos, tmp2); - PyList_SET_ITEM(heap, pos, tmp1); + arr = _PyList_ITEMS(heap); + tmp = arr[childpos]; + arr[childpos] = arr[pos]; + arr[pos] = tmp; pos = childpos; } /* Bubble it up to its final resting place (by sifting its parents down). */ @@ -362,7 +363,7 @@ static int siftdown_max(PyListObject *heap, Py_ssize_t startpos, Py_ssize_t pos) { - PyObject *newitem, *parent; + PyObject *newitem, *parent, **arr; Py_ssize_t parentpos, size; int cmp; @@ -375,10 +376,11 @@ /* Follow the path to the root, moving parents down until finding a place newitem fits. */ - newitem = PyList_GET_ITEM(heap, pos); + arr = _PyList_ITEMS(heap); + newitem = arr[pos]; while (pos > startpos) { parentpos = (pos - 1) >> 1; - parent = PyList_GET_ITEM(heap, parentpos); + parent = arr[parentpos]; cmp = PyObject_RichCompareBool(parent, newitem, Py_LT); if (cmp == -1) return -1; @@ -389,10 +391,10 @@ } if (cmp == 0) break; - parent = PyList_GET_ITEM(heap, parentpos); - newitem = PyList_GET_ITEM(heap, pos); - PyList_SET_ITEM(heap, parentpos, newitem); - PyList_SET_ITEM(heap, pos, parent); + arr = _PyList_ITEMS(heap); + newitem = arr[pos]; + arr[pos] = arr[parentpos]; + arr[parentpos] = newitem; pos = parentpos; } return 0; @@ -402,7 +404,7 @@ siftup_max(PyListObject *heap, Py_ssize_t pos) { Py_ssize_t startpos, endpos, childpos, limit; - PyObject *tmp1, *tmp2; + PyObject *tmp, **arr; int cmp; assert(PyList_Check(heap)); @@ -414,15 +416,15 @@ } /* Bubble up the smaller child until hitting a leaf. */ + arr = _PyList_ITEMS(heap); limit = endpos / 2; /* smallest pos that has no child */ while (pos < limit) { /* Set childpos to index of smaller child. */ childpos = 2*pos + 1; /* leftmost child position */ if (childpos + 1 < endpos) { - cmp = PyObject_RichCompareBool( - PyList_GET_ITEM(heap, childpos + 1), - PyList_GET_ITEM(heap, childpos), - Py_LT); + cmp = PyObject_RichCompareBool(arr[childpos + 1], + arr[childpos], + Py_LT); if (cmp == -1) return -1; if (cmp == 0) @@ -434,10 +436,10 @@ } } /* Move the smaller child up. */ - tmp1 = PyList_GET_ITEM(heap, childpos); - tmp2 = PyList_GET_ITEM(heap, pos); - PyList_SET_ITEM(heap, childpos, tmp2); - PyList_SET_ITEM(heap, pos, tmp1); + arr = _PyList_ITEMS(heap); + tmp = arr[childpos]; + arr[childpos] = arr[pos]; + arr[pos] = tmp; pos = childpos; } /* Bubble it up to its final resting place (by sifting its parents down). */