first check if starting point is trivial (head or tail) before applying log(N) search

This commit is contained in:
Pieter Noordhuis 2010-03-04 14:23:59 +01:00
parent e74825c24f
commit edb519581a

View File

@ -5300,13 +5300,15 @@ static void zrangeGenericCommand(redisClient *c, int reverse) {
if (end >= llen) end = llen-1; if (end >= llen) end = llen-1;
rangelen = (end-start)+1; rangelen = (end-start)+1;
/* Return the result in form of a multi-bulk reply */ /* check if starting point is trivial, before searching
* the element in log(N) time */
if (reverse) { if (reverse) {
ln = zslGetElementByRank(zsl, llen - start); ln = start == 0 ? zsl->tail : zslGetElementByRank(zsl, llen - start);
} else { } else {
ln = zslGetElementByRank(zsl, start + 1); ln = start == 0 ? zsl->header->forward[0] : zslGetElementByRank(zsl, start + 1);
} }
/* Return the result in form of a multi-bulk reply */
addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n", addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",
withscores ? (rangelen*2) : rangelen)); withscores ? (rangelen*2) : rangelen));
for (j = 0; j < rangelen; j++) { for (j = 0; j < rangelen; j++) {