Commit 9e087a29 authored by antirez's avatar antirez
Browse files

Optimize LRANGE to scan the list starting from the head or the tail in order...

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.
parent 8ac3c866
...@@ -514,7 +514,12 @@ void lrangeCommand(redisClient *c) { ...@@ -514,7 +514,12 @@ void lrangeCommand(redisClient *c) {
p = ziplistNext(o->ptr,p); p = ziplistNext(o->ptr,p);
} }
} else if (o->encoding == REDIS_ENCODING_LINKEDLIST) { } 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--) { while(rangelen--) {
addReplyBulk(c,ln->value); addReplyBulk(c,ln->value);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment