From 1222b6087385aa431a412a9e9b59cf29ce55210e Mon Sep 17 00:00:00 2001 From: judeng Date: Sun, 16 Apr 2023 18:02:47 +0800 Subject: [PATCH] improve performance of keys command by using static objects (#12036) Improve performance by avoiding redundancy memory malloc/free --- src/db.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/db.c b/src/db.c index ddfb04ebb..31c8f69bd 100644 --- a/src/db.c +++ b/src/db.c @@ -774,17 +774,16 @@ void keysCommand(client *c) { di = dictGetSafeIterator(c->db->dict); allkeys = (pattern[0] == '*' && plen == 1); + robj keyobj; while((de = dictNext(di)) != NULL) { sds key = dictGetKey(de); - robj *keyobj; if (allkeys || stringmatchlen(pattern,plen,key,sdslen(key),0)) { - keyobj = createStringObject(key,sdslen(key)); - if (!keyIsExpired(c->db,keyobj)) { - addReplyBulk(c,keyobj); + initStaticStringObject(keyobj, key); + if (!keyIsExpired(c->db, &keyobj)) { + addReplyBulkCBuffer(c, key, sdslen(key)); numkeys++; } - decrRefCount(keyobj); } if (c->flags & CLIENT_CLOSE_ASAP) break;