From 51829ed3f09bb4381f075900521bcaafee8819bb Mon Sep 17 00:00:00 2001 From: Aman Gupta Date: Sun, 17 May 2009 12:25:05 -0700 Subject: [PATCH 1/3] Optimize SDIFF to return as soon as the result set is empty --- redis.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/redis.c b/redis.c index 6be82b4b..a3341f6d 100644 --- a/redis.c +++ b/redis.c @@ -3112,6 +3112,7 @@ static void sunionDiffGenericCommand(redisClient *c, robj **setskeys, int setsnu /* Iterate all the elements of all the sets, add every element a single * time to the result set */ for (j = 0; j < setsnum; j++) { + if (op == REDIS_OP_DIFF && j == 0 && !dv[j]) break; /* result set is empty */ if (!dv[j]) continue; /* non existing keys are like empty sets */ di = dictGetIterator(dv[j]); @@ -3134,6 +3135,8 @@ static void sunionDiffGenericCommand(redisClient *c, robj **setskeys, int setsnu } } dictReleaseIterator(di); + + if (op == REDIS_OP_DIFF && cardinality == 0) break; /* result set is empty */ } /* Output the content of the resulting set, if not in STORE mode */ From 83cdfe182f4e24e0785d13b358c84fe5f195df32 Mon Sep 17 00:00:00 2001 From: Aman Gupta Date: Sun, 17 May 2009 14:16:24 -0700 Subject: [PATCH 2/3] Fix SINTER/UNIONSTORE to allow for &=/|= style operations (i.e. SINTERSTORE set1 set1 set2) --- redis.c | 31 ++++++++++++++----------------- test-redis.tcl | 10 ++++++++++ 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/redis.c b/redis.c index a3341f6d..9587dcdc 100644 --- a/redis.c +++ b/redis.c @@ -3013,9 +3013,6 @@ static void sinterGenericCommand(redisClient *c, robj **setskeys, int setsnum, r /* If we have a target key where to store the resulting set * create this key with an empty set inside */ dstset = createSetObject(); - deleteKey(c->db,dstkey); - dictAdd(c->db->dict,dstkey,dstset); - incrRefCount(dstkey); } /* Iterate all the elements of the first (smallest) set, and test @@ -3044,6 +3041,13 @@ static void sinterGenericCommand(redisClient *c, robj **setskeys, int setsnum, r } dictReleaseIterator(di); + if (dstkey) { + /* Store the resulting set into the target */ + deleteKey(c->db,dstkey); + dictAdd(c->db->dict,dstkey,dstset); + incrRefCount(dstkey); + } + if (!dstkey) { lenobj->ptr = sdscatprintf(sdsempty(),"*%d\r\n",cardinality); } else { @@ -3095,20 +3099,6 @@ static void sunionDiffGenericCommand(redisClient *c, robj **setskeys, int setsnu * this set object will be the resulting object to set into the target key*/ dstset = createSetObject(); - /* The first thing we should output is the total number of elements... - * since this is a multi-bulk write, but at this stage we don't know - * the intersection set size, so we use a trick, append an empty object - * to the output list and save the pointer to later modify it with the - * right length */ - if (dstkey) { - /* If we have a target key where to store the resulting set - * create this key with an empty set inside */ - deleteKey(c->db,dstkey); - dictAdd(c->db->dict,dstkey,dstset); - incrRefCount(dstkey); - server.dirty++; - } - /* Iterate all the elements of all the sets, add every element a single * time to the result set */ for (j = 0; j < setsnum; j++) { @@ -3154,6 +3144,13 @@ static void sunionDiffGenericCommand(redisClient *c, robj **setskeys, int setsnu addReply(c,shared.crlf); } dictReleaseIterator(di); + } else { + /* If we have a target key where to store the resulting set + * create this key with the result set inside */ + deleteKey(c->db,dstkey); + dictAdd(c->db->dict,dstkey,dstset); + incrRefCount(dstkey); + server.dirty++; } /* Cleanup */ diff --git a/test-redis.tcl b/test-redis.tcl index bd58cb2e..16723903 100644 --- a/test-redis.tcl +++ b/test-redis.tcl @@ -481,6 +481,11 @@ proc main {server port} { lsort [$r smembers setres] } [lsort -uniq "[$r smembers set1] [$r smembers set2]"] + test {SUNIONSTORE with same src and dest} { + $r sunionstore set1 set1 set1 + $r scard set1 + } {1000} + test {SINTER against three sets} { $r sadd set3 999 $r sadd set3 995 @@ -494,6 +499,11 @@ proc main {server port} { lsort [$r smembers setres] } {995 999} + test {SINTERSTORE with same src and dest} { + $r sinterstore set1 set1 set1 + $r scard set1 + } {1000} + test {SUNION with non existing keys} { lsort [$r sunion nokey1 set1 set2 nokey2] } [lsort -uniq "[$r smembers set1] [$r smembers set2]"] From 0150db364d939a5a69244aca6a66ad05903979fe Mon Sep 17 00:00:00 2001 From: Aman Gupta Date: Tue, 19 May 2009 22:36:11 -0700 Subject: [PATCH 3/3] Allow timeout=0 config to disable client timeouts --- redis.c | 4 ++-- redis.conf | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/redis.c b/redis.c index 9587dcdc..f4e15bb4 100644 --- a/redis.c +++ b/redis.c @@ -720,7 +720,7 @@ int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientData) { } /* Close connections of timedout clients */ - if (!(loops % 10)) + if (server.maxidletime && !(loops % 10)) closeTimedoutClients(); /* Check if a background saving in progress terminated */ @@ -959,7 +959,7 @@ static void loadServerConfig(char *filename) { /* Execute config directives */ if (!strcasecmp(argv[0],"timeout") && argc == 2) { server.maxidletime = atoi(argv[1]); - if (server.maxidletime < 1) { + if (server.maxidletime < 0) { err = "Invalid timeout value"; goto loaderr; } } else if (!strcasecmp(argv[0],"port") && argc == 2) { diff --git a/redis.conf b/redis.conf index 38592b02..e8c6cd82 100644 --- a/redis.conf +++ b/redis.conf @@ -16,7 +16,7 @@ port 6379 # # bind 127.0.0.1 -# Close the connection after a client is idle for N seconds +# Close the connection after a client is idle for N seconds (0 to disable) timeout 300 # Save the DB on disk: