From 4a30a26f8fbb54854f5b29b77fd3c744e8be4290 Mon Sep 17 00:00:00 2001 From: filipecosta90 Date: Sun, 15 Sep 2019 21:16:30 +0100 Subject: [PATCH 1/3] [add] improved performance of RM_ReplyWithSimpleString and RM_ReplyWithError by making usage addReplyProto instead of addReplySds --- src/module.c | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/module.c b/src/module.c index ab614c529..ad7e6864c 100644 --- a/src/module.c +++ b/src/module.c @@ -1120,19 +1120,6 @@ int RM_ReplyWithLongLong(RedisModuleCtx *ctx, long long ll) { return REDISMODULE_OK; } -/* Reply with an error or simple string (status message). Used to implement - * ReplyWithSimpleString() and ReplyWithError(). - * The function always returns REDISMODULE_OK. */ -int replyWithStatus(RedisModuleCtx *ctx, const char *msg, char *prefix) { - client *c = moduleGetReplyClient(ctx); - if (c == NULL) return REDISMODULE_OK; - sds strmsg = sdsnewlen(prefix,1); - strmsg = sdscat(strmsg,msg); - strmsg = sdscatlen(strmsg,"\r\n",2); - addReplySds(c,strmsg); - return REDISMODULE_OK; -} - /* Reply with the error 'err'. * * Note that 'err' must contain all the error, including @@ -1148,7 +1135,13 @@ int replyWithStatus(RedisModuleCtx *ctx, const char *msg, char *prefix) { * The function always returns REDISMODULE_OK. */ int RM_ReplyWithError(RedisModuleCtx *ctx, const char *err) { - return replyWithStatus(ctx,err,"-"); + client *c = moduleGetReplyClient(ctx); + if (c == NULL) return REDISMODULE_OK; + const size_t len = strlen(err); + addReplyProto(c,"-",1); + addReplyProto(c,err,len); + addReplyProto(c,"\r\n",2); + return REDISMODULE_OK; } /* Reply with a simple string (+... \r\n in RESP protocol). This replies @@ -1157,7 +1150,13 @@ int RM_ReplyWithError(RedisModuleCtx *ctx, const char *err) { * * The function always returns REDISMODULE_OK. */ int RM_ReplyWithSimpleString(RedisModuleCtx *ctx, const char *msg) { - return replyWithStatus(ctx,msg,"+"); + client *c = moduleGetReplyClient(ctx); + if (c == NULL) return REDISMODULE_OK; + const size_t len = strlen(msg); + addReplyProto(c,"+",1); + addReplyProto(c,msg,len); + addReplyProto(c,"\r\n",2); + return REDISMODULE_OK; } /* Reply with an array type of 'len' elements. However 'len' other calls From 733280d9cb1122dd15440429137610a4237869d4 Mon Sep 17 00:00:00 2001 From: filipecosta90 Date: Mon, 23 Sep 2019 23:45:31 +0100 Subject: [PATCH 2/3] [fix] un-refactor the code. [perf] replyWithStatus now makes usage of addReplyProto --- src/module.c | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/module.c b/src/module.c index ad7e6864c..067a69928 100644 --- a/src/module.c +++ b/src/module.c @@ -1120,6 +1120,19 @@ int RM_ReplyWithLongLong(RedisModuleCtx *ctx, long long ll) { return REDISMODULE_OK; } +/* Reply with an error or simple string (status message). Used to implement + * ReplyWithSimpleString() and ReplyWithError(). + * The function always returns REDISMODULE_OK. */ +int replyWithStatus(RedisModuleCtx *ctx, const char *msg, char *prefix) { + client *c = moduleGetReplyClient(ctx); + if (c == NULL) return REDISMODULE_OK; + const size_t len = strlen(msg); + addReplyProto(c,"-",1); + addReplyProto(c,msg,len); + addReplyProto(c,"\r\n",2); + return REDISMODULE_OK; +} + /* Reply with the error 'err'. * * Note that 'err' must contain all the error, including @@ -1135,13 +1148,7 @@ int RM_ReplyWithLongLong(RedisModuleCtx *ctx, long long ll) { * The function always returns REDISMODULE_OK. */ int RM_ReplyWithError(RedisModuleCtx *ctx, const char *err) { - client *c = moduleGetReplyClient(ctx); - if (c == NULL) return REDISMODULE_OK; - const size_t len = strlen(err); - addReplyProto(c,"-",1); - addReplyProto(c,err,len); - addReplyProto(c,"\r\n",2); - return REDISMODULE_OK; + return replyWithStatus(ctx,err,"-"); } /* Reply with a simple string (+... \r\n in RESP protocol). This replies @@ -1150,13 +1157,7 @@ int RM_ReplyWithError(RedisModuleCtx *ctx, const char *err) { * * The function always returns REDISMODULE_OK. */ int RM_ReplyWithSimpleString(RedisModuleCtx *ctx, const char *msg) { - client *c = moduleGetReplyClient(ctx); - if (c == NULL) return REDISMODULE_OK; - const size_t len = strlen(msg); - addReplyProto(c,"+",1); - addReplyProto(c,msg,len); - addReplyProto(c,"\r\n",2); - return REDISMODULE_OK; + return replyWithStatus(ctx,msg,"+"); } /* Reply with an array type of 'len' elements. However 'len' other calls From af15b285faa2a9cece8f5a76e05221836b6abae5 Mon Sep 17 00:00:00 2001 From: filipecosta90 Date: Wed, 25 Sep 2019 17:28:42 +0100 Subject: [PATCH 3/3] [fix] fixed the un-refactor bug. --- src/module.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/module.c b/src/module.c index 067a69928..d02e2708c 100644 --- a/src/module.c +++ b/src/module.c @@ -1126,9 +1126,10 @@ int RM_ReplyWithLongLong(RedisModuleCtx *ctx, long long ll) { int replyWithStatus(RedisModuleCtx *ctx, const char *msg, char *prefix) { client *c = moduleGetReplyClient(ctx); if (c == NULL) return REDISMODULE_OK; - const size_t len = strlen(msg); - addReplyProto(c,"-",1); - addReplyProto(c,msg,len); + const size_t msgLen = strlen(msg); + const size_t prefixLen = strlen(prefix); + addReplyProto(c,prefix,prefixLen); + addReplyProto(c,msg,msgLen); addReplyProto(c,"\r\n",2); return REDISMODULE_OK; }