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,
...@@ -180,37 +180,43 @@ static PyObject * ...@@ -180,37 +180,43 @@ 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); COPY_KEY_FROM_ARG(key, keyarg, copied);
UNLESS (copied) return NULL; UNLESS (copied) return NULL;
PER_USE_OR_RETURN(self, NULL); PER_USE_OR_RETURN(self, NULL);
if (self->len == 0) {
BTREE_SEARCH(min, self, key, goto Error); /* empty BTree */
if (self->len)
{
if (SameType_Check(self, self->data[min].child))
r = _BTree_get(BTREE(self->data[min].child), keyarg,
has_key ? has_key + 1: 0);
else
r = _bucket_get(BUCKET(self->data[min].child), keyarg,
has_key ? has_key + 1: 0);
}
else
{ /* No data */
if (has_key) if (has_key)
r = PyInt_FromLong(0); result = PyInt_FromLong(0);
else else
PyErr_SetObject(PyExc_KeyError, keyarg); PyErr_SetObject(PyExc_KeyError, keyarg);
} }
else {
for (;;) {
int i;
Sized *child;
Error: BTREE_SEARCH(i, self, key, goto Done);
PER_ALLOW_DEACTIVATION(self); child = self->data[i].child;
PER_ACCESSED(self); has_key += has_key != 0; /* bump depth counter, maybe */
return r; 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;
}
}
}
Done:
PER_UNUSE(self);
return result;
} }
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