From: Pieter Noordhuis Date: Thu, 4 Mar 2010 13:23:59 +0000 (+0100) Subject: first check if starting point is trivial (head or tail) before applying log(N) search X-Git-Url: https://git.saurik.com/redis.git/commitdiff_plain/edb519581a9627ed18f309667c5d53b610a666fb?ds=inline;hp=-c first check if starting point is trivial (head or tail) before applying log(N) search --- edb519581a9627ed18f309667c5d53b610a666fb diff --git a/redis.c b/redis.c index d5e47e9f..bff1cdfc 100644 --- a/redis.c +++ b/redis.c @@ -5300,13 +5300,15 @@ static void zrangeGenericCommand(redisClient *c, int reverse) { if (end >= llen) end = llen-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) { - ln = zslGetElementByRank(zsl, llen - start); + ln = start == 0 ? zsl->tail : zslGetElementByRank(zsl, llen - start); } 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", withscores ? (rangelen*2) : rangelen)); for (j = 0; j < rangelen; j++) {