diff options
author | antirez <antirez@gmail.com> | 2011-09-14 15:10:28 +0200 |
---|---|---|
committer | antirez <antirez@gmail.com> | 2011-09-14 15:17:09 +0200 |
commit | 9e087a298dc798b5664654d0934d27762e76092e (patch) | |
tree | e3018ca33b322fd0e900d58bda6f0d022d37bbc1 | |
parent | 8ac3c866645ec19a1f21bcf49e6a39950bc6993b (diff) | |
download | redis-9e087a298dc798b5664654d0934d27762e76092e.tar.gz |
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.
-rw-r--r-- | src/t_list.c | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/src/t_list.c b/src/t_list.c index 64e917a41..1d046f403 100644 --- a/src/t_list.c +++ b/src/t_list.c @@ -514,7 +514,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); |