opt fixes for tasks and heapq

This commit is contained in:
Joe Shochet 2004-09-04 00:43:19 +00:00
parent 4386afd134
commit 454f8fd782
3 changed files with 25 additions and 30 deletions

View File

@ -5,8 +5,6 @@
// under Windows). // under Windows).
#define DIR_TYPE metalib #define DIR_TYPE metalib
//#define BUILDING_DLL BUILDING_DIRECT
#define OTHER_LIBS \ #define OTHER_LIBS \
pandaexpress:m \ pandaexpress:m \

View File

@ -4,6 +4,9 @@
reported to be about 20x faster. In 2.4 they reimplemented heapq in C so reported to be about 20x faster. In 2.4 they reimplemented heapq in C so
it should be comparable to this. At this time though, Python 2.4 is it should be comparable to this. At this time though, Python 2.4 is
still in alpha. still in alpha.
Note: This code has been bastardized to only work on Tasks temporarily.
*/ */
#include <Python.h> #include <Python.h>
@ -122,7 +125,7 @@ heapify(PyObject *self, PyObject *args) {
static int static int
_siftdown(PyObject *list, int startpos, int pos) { _siftdown(PyObject *list, int startpos, int pos) {
PyObject *newitem, *parent; PyObject *newitem, *parent;
int parentpos, cmp; int parentpos;
newitem = PySequence_GetItem(list,pos); newitem = PySequence_GetItem(list,pos);
@ -150,11 +153,8 @@ _siftdown(PyObject *list, int startpos, int pos) {
if (parentCTask->get_wake_time() <= newitemCTask->get_wake_time()) { if (parentCTask->get_wake_time() <= newitemCTask->get_wake_time()) {
break; break;
} else {
return -1;
} }
Py_INCREF(parent); Py_INCREF(parent);
PyList_SetItem(list,pos,parent); PyList_SetItem(list,pos,parent);
pos = parentpos; pos = parentpos;
@ -168,7 +168,6 @@ _siftup(PyObject *list, int pos) {
PyObject *newitem, *right, *child; PyObject *newitem, *right, *child;
int endpos, rightpos, childpos; int endpos, rightpos, childpos;
int startpos = pos; int startpos = pos;
int cmp;
endpos = PyList_Size(list); endpos = PyList_Size(list);
newitem = PySequence_GetItem(list,pos); newitem = PySequence_GetItem(list,pos);
@ -202,8 +201,6 @@ _siftup(PyObject *list, int pos) {
if (rightCTask->get_wake_time() <= childCTask->get_wake_time()) { if (rightCTask->get_wake_time() <= childCTask->get_wake_time()) {
childpos = rightpos; childpos = rightpos;
} else {
return -1;
} }
} }
child = PySequence_GetItem(list,childpos); child = PySequence_GetItem(list,childpos);

View File

@ -86,28 +86,28 @@ class Task(CTask):
self.pstats = None self.pstats = None
self.extraArgs = None self.extraArgs = None
# Used for putting into the doLaterList # # Used for putting into the doLaterList
# the heapq calls __cmp__ via the rich compare function # # the heapq calls __cmp__ via the rich compare function
def __cmp__(self, other): # def __cmp__(self, other):
if isinstance(other, Task): # if isinstance(other, Task):
if self.getWakeTime() < other.getWakeTime(): # if self.getWakeTime() < other.getWakeTime():
return -1 # return -1
elif self.getWakeTime() > other.getWakeTime(): # elif self.getWakeTime() > other.getWakeTime():
return 1 # return 1
# If the wakeTimes happen to be the same, just # # If the wakeTimes happen to be the same, just
# sort them based on id # # sort them based on id
else: # else:
return cmp(id(self), id(other)) # return cmp(id(self), id(other))
# This is important for people doing a (task != None) and such. # # This is important for people doing a (task != None) and such.
else: # else:
return cmp(id(self), id(other)) # return cmp(id(self), id(other))
# According to the Python manual (3.3.1), if you define a cmp operator # # According to the Python manual (3.3.1), if you define a cmp operator
# you should also define a hash operator or your objects will not be # # you should also define a hash operator or your objects will not be
# usable in dictionaries. Since no two task objects are unique, we can # # usable in dictionaries. Since no two task objects are unique, we can
# just return the unique id. # # just return the unique id.
def __hash__(self): # def __hash__(self):
return self.id # return self.id
def remove(self): def remove(self):
if not self.__removed: if not self.__removed: