Skip to content

Commit

Permalink
Optimize LRANGE to scan the list starting from the head or the tail i…
Browse files Browse the repository at this point in the history
…n order to traverse the minimal number of elements. Thanks to Didier Spezia for noticing the problem and providing a patch.
  • Loading branch information
antirez committed Sep 14, 2011
1 parent 6468a6f commit 7cfeb8c
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/t_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 7cfeb8c

Please sign in to comment.