summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorantirez <antirez@gmail.com>2011-09-14 15:10:28 +0200
committerantirez <antirez@gmail.com>2011-09-14 15:17:04 +0200
commit67f594f9b5f62c76206332cb96b588a55c5f5ecf (patch)
tree5ead9ba540015b44c34ca9ee40b7855dc8276fe3
parentb7bf29059e2954566af95dd18c8554f68479ed2d (diff)
downloadredis-67f594f9b5f62c76206332cb96b588a55c5f5ecf.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.c7
1 files changed, 6 insertions, 1 deletions
diff --git a/src/t_list.c b/src/t_list.c
index 59455d893..5fdaaa8ac 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);