From 2dab3fdc646ad77523d835d190b457622fd32e8d Mon Sep 17 00:00:00 2001 From: antirez Date: Wed, 14 Sep 2011 15:10:28 +0200 Subject: [PATCH] Optimize LRANGE to scan the list starting from the head or the tail in order to traverse the minimal number of elements. Thanks to Didier Spezia for noticing the problem and providing a patch. --- src/t_list.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/t_list.c b/src/t_list.c index 0690fd2fa..71436198d 100644 --- a/src/t_list.c +++ b/src/t_list.c @@ -519,7 +519,12 @@ void lrangeCommand(redisClient *c) { p = ziplistNext(o->ptr,p); } } else if (o->encoding == REDIS_ENCODING_LINKEDLIST) { - listNode *ln = listIndex(o->ptr,start); + listNode *ln; + + /* If we are nearest to the end of the list, reach the element + * starting from tail and going backward, as it is faster. */ + if (start > llen/2) start -= llen; + ln = listIndex(o->ptr,start); while(rangelen--) { addReplyBulk(c,ln->value);