RiversJin commented on code in PR #2627:
URL: https://github.com/apache/kvrocks/pull/2627#discussion_r1820395683


##########
src/server/redis_reply.cc:
##########
@@ -37,9 +37,23 @@ namespace redis {
 
 void Reply(evbuffer *output, const std::string &data) { evbuffer_add(output, 
data.c_str(), data.length()); }
 
-std::string SimpleString(const std::string &data) { return "+" + data + CRLF; }
+std::string SimpleString(std::string_view data) {
+  std::string res;
+  res.reserve(data.size() + 3);  // 1 for '+', 2 for CRLF
+  res += RESP_PREFIX_SIMPLE_STRING;
+  res += data;
+  res += CRLF;
+  return res;
+}
 
-std::string Error(const Status &s) { return RESP_PREFIX_ERROR + 
StatusToRedisErrorMsg(s) + CRLF; }
+std::string Error(const Status &s) {
+  std::string res;
+  res.reserve(s.Msg().size() + 3);  // 1 for '-', 2 for CRLF

Review Comment:
   Yes, you're absolutely right; I overlooked the possibility that the text 
could come directly from redisErrorPrefixMapping. However, performing a hash 
lookup here to confirm the reserved length might be a bit heavy.
   
   Or, do you think it’s a good idea to directly use std::max(16, 
s.Msg().size() + 3)? In most cases, the minimum allocation unit by the memory 
allocator is often two pointer lengths, and for the contents within 
redisErrorPrefixMapping, 16 bytes should be sufficient.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to