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.
This commit is contained in:
judeng 2023-04-25 15:17:21 +08:00 committed by GitHub
parent bedecec786
commit 9b588f3820
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 10 deletions

View File

@ -162,9 +162,8 @@ NULL
} else if ((c->argc == 2 || c->argc == 3) && } else if ((c->argc == 2 || c->argc == 3) &&
!strcasecmp(c->argv[1]->ptr,"get")) !strcasecmp(c->argv[1]->ptr,"get"))
{ {
long count = 10, sent = 0; long count = 10;
listIter li; listIter li;
void *totentries;
listNode *ln; listNode *ln;
slowlogEntry *se; slowlogEntry *se;
@ -181,11 +180,15 @@ NULL
} }
} }
listRewind(server.slowlog,&li); if (count > (long)listLength(server.slowlog)) {
totentries = addReplyDeferredLen(c); count = listLength(server.slowlog);
while(count-- && (ln = listNext(&li))) { }
addReplyArrayLen(c, count);
listRewind(server.slowlog, &li);
while (count--) {
int j; int j;
ln = listNext(&li);
se = ln->value; se = ln->value;
addReplyArrayLen(c,6); addReplyArrayLen(c,6);
addReplyLongLong(c,se->id); addReplyLongLong(c,se->id);
@ -196,9 +199,7 @@ NULL
addReplyBulk(c,se->argv[j]); addReplyBulk(c,se->argv[j]);
addReplyBulkCBuffer(c,se->peerid,sdslen(se->peerid)); addReplyBulkCBuffer(c,se->peerid,sdslen(se->peerid));
addReplyBulkCBuffer(c,se->cname,sdslen(se->cname)); addReplyBulkCBuffer(c,se->cname,sdslen(se->cname));
sent++;
} }
setDeferredArrayLen(c,totentries,sent);
} else { } else {
addReplySubcommandSyntaxError(c); addReplySubcommandSyntaxError(c);
} }

View File

@ -24,8 +24,11 @@ start_server {tags {"slowlog"} overrides {slowlog-log-slower-than 1000000}} {
} {10} } {10}
test {SLOWLOG - GET optional argument to limit output len works} { test {SLOWLOG - GET optional argument to limit output len works} {
llength [r slowlog get 5]
} {5} assert_equal 5 [llength [r slowlog get 5]]
assert_equal 10 [llength [r slowlog get -1]]
assert_equal 10 [llength [r slowlog get 20]]
}
test {SLOWLOG - RESET subcommand works} { test {SLOWLOG - RESET subcommand works} {
r config set slowlog-log-slower-than 100000 r config set slowlog-log-slower-than 100000
@ -39,7 +42,7 @@ start_server {tags {"slowlog"} overrides {slowlog-log-slower-than 1000000}} {
set e [lindex [r slowlog get] 0] set e [lindex [r slowlog get] 0]
assert_equal [llength $e] 6 assert_equal [llength $e] 6
if {!$::external} { if {!$::external} {
assert_equal [lindex $e 0] 105 assert_equal [lindex $e 0] 107
} }
assert_equal [expr {[lindex $e 2] > 100000}] 1 assert_equal [expr {[lindex $e 2] > 100000}] 1
assert_equal [lindex $e 3] {debug sleep 0.2} assert_equal [lindex $e 3] {debug sleep 0.2}