Commit 529571b9 authored by Tim Peters's avatar Tim Peters

_BTree_get(): For a little speed boost in a frequent operation, removed

the recursion (iteration is quite natural here -- it was tail-recursive).
parent e2f5a9f7
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
****************************************************************************/ ****************************************************************************/
#define BTREETEMPLATE_C "$Id: BTreeTemplate.c,v 1.66 2002/06/21 05:43:56 tim_one Exp $\n" #define BTREETEMPLATE_C "$Id: BTreeTemplate.c,v 1.67 2002/06/21 18:06:26 tim_one Exp $\n"
/* Sanity-check a BTree. This is a private helper for BTree_check. Return: /* Sanity-check a BTree. This is a private helper for BTree_check. Return:
* -1 Error. If it's an internal inconsistency in the BTree, * -1 Error. If it's an internal inconsistency in the BTree,
...@@ -179,38 +179,44 @@ BTree_check(BTree *self, PyObject *args) ...@@ -179,38 +179,44 @@ BTree_check(BTree *self, PyObject *args)
static PyObject * static PyObject *
_BTree_get(BTree *self, PyObject *keyarg, int has_key) _BTree_get(BTree *self, PyObject *keyarg, int has_key)
{ {
KEY_TYPE key; KEY_TYPE key;
int min; /* index of child to search */ PyObject *result = NULL; /* guilty until proved innocent */
PyObject *r = NULL; /* result object */ int copied = 1;
int copied = 1;
COPY_KEY_FROM_ARG(key, keyarg, copied);
UNLESS (copied) return NULL;
PER_USE_OR_RETURN(self, NULL); COPY_KEY_FROM_ARG(key, keyarg, copied);
UNLESS (copied) return NULL;
BTREE_SEARCH(min, self, key, goto Error); PER_USE_OR_RETURN(self, NULL);
if (self->len) if (self->len == 0) {
{ /* empty BTree */
if (SameType_Check(self, self->data[min].child)) if (has_key)
r = _BTree_get(BTREE(self->data[min].child), keyarg, result = PyInt_FromLong(0);
has_key ? has_key + 1: 0); else
else PyErr_SetObject(PyExc_KeyError, keyarg);
r = _bucket_get(BUCKET(self->data[min].child), keyarg,
has_key ? has_key + 1: 0);
} }
else else {
{ /* No data */ for (;;) {
if (has_key) int i;
r = PyInt_FromLong(0); Sized *child;
else
PyErr_SetObject(PyExc_KeyError, keyarg); BTREE_SEARCH(i, self, key, goto Done);
child = self->data[i].child;
has_key += has_key != 0; /* bump depth counter, maybe */
if (SameType_Check(self, child)) {
PER_UNUSE(self);
self = BTREE(child);
PER_USE_OR_RETURN(self, NULL);
}
else {
result = _bucket_get(BUCKET(child), keyarg, has_key);
break;
}
}
} }
Error: Done:
PER_ALLOW_DEACTIVATION(self); PER_UNUSE(self);
PER_ACCESSED(self); return result;
return r;
} }
static PyObject * static PyObject *
......
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