summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDvir Volk <dvirsky@gmail.com>2016-05-19 13:51:55 +0300
committerDvir Volk <dvirsky@gmail.com>2016-05-19 13:51:55 +0300
commit137fd86a6133b4a83c2a95215241c9906ec6877f (patch)
tree453d8cc3f59aeff8a7db6a748ff8bf3320d608fc
parent46b07cbb5c52a6a9321ab8c2134d3c6be9ddae86 (diff)
downloadredis-137fd86a6133b4a83c2a95215241c9906ec6877f.tar.gz
optimized amFree even further
-rw-r--r--src/module.c13
1 files changed, 9 insertions, 4 deletions
diff --git a/src/module.c b/src/module.c
index bedda7fd5..f79b6bda5 100644
--- a/src/module.c
+++ b/src/module.c
@@ -585,12 +585,17 @@ void autoMemoryFreed(RedisModuleCtx *ctx, int type, void *ptr) {
ctx->amqueue[j].ptr == ptr)
{
ctx->amqueue[j].type = REDISMODULE_AM_FREED;
- /* Optimization: if this is the last element, we can
- * reuse it. */
- if (j == ctx->amqueue_used-1) ctx->amqueue_used--;
+
+ /* Switch the freed element and the top element, to avoid growing
+ * the queue unnecessarily if we allocate/free in a loop */
+ if (j != ctx->amqueue_used-1) {
+ ctx->amqueue[j] = ctx->amqueue[ctx->amqueue_used-1];
+ }
+ /* Reduce the size of the queue because we either moved the top
+ * element elsewhere or freed it */
+ ctx->amqueue_used--;
break;
-
}
}
}