first check if starting point is trivial (head or tail) before applying log(N) search
authorPieter Noordhuis <pcnoordhuis@gmail.com>
Thu, 4 Mar 2010 13:23:59 +0000 (14:23 +0100)
committerPieter Noordhuis <pcnoordhuis@gmail.com>
Thu, 4 Mar 2010 13:23:59 +0000 (14:23 +0100)
redis.c

diff --git a/redis.c b/redis.c
index d5e47e9f0cd8e7eda9f548e781dc0a781a6b9199..bff1cdfc548adf64050f4540822b548fcabc23c2 100644 (file)
--- 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++) {