From 9b588f3820edd35d4cd44b69e96476095858fc06 Mon Sep 17 00:00:00 2001 From: judeng Date: Tue, 25 Apr 2023 15:17:21 +0800 Subject: minor optimization for slowlog get (#12103) We can always know the array length of the response, so there is no need to use addReplyDeferredLen which may introduce some additional overheads. --- src/slowlog.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'src/slowlog.c') diff --git a/src/slowlog.c b/src/slowlog.c index 320f334a8..4c31917bb 100644 --- a/src/slowlog.c +++ b/src/slowlog.c @@ -162,9 +162,8 @@ NULL } else if ((c->argc == 2 || c->argc == 3) && !strcasecmp(c->argv[1]->ptr,"get")) { - long count = 10, sent = 0; + long count = 10; listIter li; - void *totentries; listNode *ln; slowlogEntry *se; @@ -181,11 +180,15 @@ NULL } } - listRewind(server.slowlog,&li); - totentries = addReplyDeferredLen(c); - while(count-- && (ln = listNext(&li))) { + if (count > (long)listLength(server.slowlog)) { + count = listLength(server.slowlog); + } + addReplyArrayLen(c, count); + listRewind(server.slowlog, &li); + while (count--) { int j; + ln = listNext(&li); se = ln->value; addReplyArrayLen(c,6); addReplyLongLong(c,se->id); @@ -196,9 +199,7 @@ NULL addReplyBulk(c,se->argv[j]); addReplyBulkCBuffer(c,se->peerid,sdslen(se->peerid)); addReplyBulkCBuffer(c,se->cname,sdslen(se->cname)); - sent++; } - setDeferredArrayLen(c,totentries,sent); } else { addReplySubcommandSyntaxError(c); } -- cgit v1.2.1