From afd9e3ed3ff9ead46a233d11b45780b7585b00a3 Mon Sep 17 00:00:00 2001 From: judeng Date: Sun, 22 Jan 2023 15:16:17 +0800 Subject: [PATCH] Optimize the performance of sdscatrepr in printable characters (#11725) sdscatrepr is not the hot path in redis, but it's still useful to have make it less wasteful. --- src/sds.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/sds.c b/src/sds.c index 4e934d7ad..e0e726dbb 100644 --- a/src/sds.c +++ b/src/sds.c @@ -997,6 +997,7 @@ void sdsfreesplitres(sds *tokens, int count) { * After the call, the modified sds string is no longer valid and all the * references must be substituted with the new pointer returned by the call. */ sds sdscatrepr(sds s, const char *p, size_t len) { + s = sdsMakeRoomFor(s, len + 2); s = sdscatlen(s,"\"",1); while(len--) { switch(*p) { @@ -1011,7 +1012,7 @@ sds sdscatrepr(sds s, const char *p, size_t len) { case '\b': s = sdscatlen(s,"\\b",2); break; default: if (isprint(*p)) - s = sdscatprintf(s,"%c",*p); + s = sdscatlen(s, p, 1); else s = sdscatprintf(s,"\\x%02x",(unsigned char)*p); break;