From edb519581a9627ed18f309667c5d53b610a666fb Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Thu, 4 Mar 2010 14:23:59 +0100 Subject: [PATCH] first check if starting point is trivial (head or tail) before applying log(N) search --- redis.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) 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++) {