summaryrefslogtreecommitdiff
path: root/src/sort.c
diff options
context:
space:
mode:
authorantirez <antirez@gmail.com>2015-02-03 15:38:21 +0100
committerantirez <antirez@gmail.com>2015-02-03 15:38:21 +0100
commit96abf659008e7e8e544e446bbfac922c059a5650 (patch)
treec1b5f628d06e05a4911897c32068e5b6e478fb7c /src/sort.c
parent5fbb36f9e7cfc4012018c433b3ed682b06246f03 (diff)
downloadredis-96abf659008e7e8e544e446bbfac922c059a5650.tar.gz
Hopefully better sort.c optimization comments.
Related to #2346.
Diffstat (limited to 'src/sort.c')
-rw-r--r--src/sort.c24
1 files changed, 9 insertions, 15 deletions
diff --git a/src/sort.c b/src/sort.c
index e9a191eb2..97500df45 100644
--- a/src/sort.c
+++ b/src/sort.c
@@ -322,16 +322,16 @@ void sortCommand(redisClient *c) {
}
if (end >= vectorlen) end = vectorlen-1;
- /* Optimization:
+ /* Whenever possible, we load elements into the output array in a more
+ * direct way. This is possible if:
*
- * 1) if the object to sort is a sorted set.
+ * 1) The object to sort is a sorted set or a list (internally sorted).
* 2) There is nothing to sort as dontsort is true (BY <constant string>).
- * 3) We have a LIMIT option that actually reduces the number of elements
- * to fetch.
*
- * In this case to load all the objects in the vector is a huge waste of
- * resources. We just allocate a vector that is big enough for the selected
- * range length, and make sure to load just this part in the vector. */
+ * In this special case, if we have a LIMIT option that actually reduces
+ * the number of elements to fetch, we also optimize to just load the
+ * range we are interested in and allocating a vector that is big enough
+ * for the selected range length. */
if ((sortval->type == REDIS_ZSET || sortval->type == REDIS_LIST) &&
dontsort &&
(start != 0 || end != vectorlen-1))
@@ -364,10 +364,7 @@ void sortCommand(redisClient *c) {
j++;
}
listTypeReleaseIterator(li);
- /* The code producing the output does not know that in the case of
- * sorted set, 'dontsort', and LIMIT, we are able to get just the
- * range, already sorted, so we need to adjust "start" and "end"
- * to make sure start is set to 0. */
+ /* Fix start/end: output code is not aware of this optimization. */
end -= start;
start = 0;
}
@@ -427,10 +424,7 @@ void sortCommand(redisClient *c) {
j++;
ln = desc ? ln->backward : ln->level[0].forward;
}
- /* The code producing the output does not know that in the case of
- * sorted set, 'dontsort', and LIMIT, we are able to get just the
- * range, already sorted, so we need to adjust "start" and "end"
- * to make sure start is set to 0. */
+ /* Fix start/end: output code is not aware of this optimization. */
end -= start;
start = 0;
} else if (sortval->type == REDIS_ZSET) {