This is an automated email from the ASF dual-hosted git repository.

twice pushed a commit to branch unstable
in repository https://gitbox.apache.org/repos/asf/kvrocks.git


The following commit(s) were added to refs/heads/unstable by this push:
     new 3a518696 feat(resp): optimize simple string "OK" construction (#2627)
3a518696 is described below

commit 3a51869677161092ec9c21dc3744debf70ea4698
Author: Rivers <[email protected]>
AuthorDate: Thu Oct 31 13:32:49 2024 +0800

    feat(resp): optimize simple string "OK" construction (#2627)
---
 src/cluster/replication.cc          |  2 +-
 src/cluster/sync_migrate_context.cc |  2 +-
 src/commands/cmd_bloom_filter.cc    |  2 +-
 src/commands/cmd_cluster.cc         | 18 ++++++-------
 src/commands/cmd_function.cc        |  2 +-
 src/commands/cmd_hash.cc            |  2 +-
 src/commands/cmd_hll.cc             |  2 +-
 src/commands/cmd_json.cc            |  6 ++---
 src/commands/cmd_key.cc             |  2 +-
 src/commands/cmd_list.cc            |  4 +--
 src/commands/cmd_replication.cc     |  2 +-
 src/commands/cmd_script.cc          |  2 +-
 src/commands/cmd_search.cc          |  4 +--
 src/commands/cmd_server.cc          | 52 ++++++++++++++++++-------------------
 src/commands/cmd_stream.cc          |  6 ++---
 src/commands/cmd_string.cc          |  8 +++---
 src/commands/cmd_txn.cc             | 10 +++----
 src/server/redis_reply.cc           |  9 ++++++-
 src/server/redis_reply.h            |  5 +++-
 19 files changed, 75 insertions(+), 65 deletions(-)

diff --git a/src/cluster/replication.cc b/src/cluster/replication.cc
index 14575703..dff2d3d7 100644
--- a/src/cluster/replication.cc
+++ b/src/cluster/replication.cc
@@ -62,7 +62,7 @@ Status FeedSlaveThread::Start() {
     sigaddset(&mask, SIGHUP);
     sigaddset(&mask, SIGPIPE);
     pthread_sigmask(SIG_BLOCK, &mask, &omask);
-    auto s = util::SockSend(conn_->GetFD(), redis::SimpleString("OK"), 
conn_->GetBufferEvent());
+    auto s = util::SockSend(conn_->GetFD(), redis::RESP_OK, 
conn_->GetBufferEvent());
     if (!s.IsOK()) {
       LOG(ERROR) << "failed to send OK response to the replica: " << s.Msg();
       return;
diff --git a/src/cluster/sync_migrate_context.cc 
b/src/cluster/sync_migrate_context.cc
index a7caf73c..7b51c982 100644
--- a/src/cluster/sync_migrate_context.cc
+++ b/src/cluster/sync_migrate_context.cc
@@ -66,7 +66,7 @@ void SyncMigrateContext::TimerCB(int, [[maybe_unused]] 
int16_t events) {
 
 void SyncMigrateContext::OnWrite(bufferevent *bev) {
   if (migrate_result_) {
-    conn_->Reply(redis::SimpleString("OK"));
+    conn_->Reply(redis::RESP_OK);
   } else {
     conn_->Reply(redis::Error(migrate_result_));
   }
diff --git a/src/commands/cmd_bloom_filter.cc b/src/commands/cmd_bloom_filter.cc
index 6fd422ff..d58a1a46 100644
--- a/src/commands/cmd_bloom_filter.cc
+++ b/src/commands/cmd_bloom_filter.cc
@@ -94,7 +94,7 @@ class CommandBFReserve : public Commander {
     auto s = bloomfilter_db.Reserve(ctx, args_[1], capacity_, error_rate_, 
expansion_);
     if (!s.ok()) return {Status::RedisExecErr, s.ToString()};
 
-    *output = redis::SimpleString("OK");
+    *output = redis::RESP_OK;
     return Status::OK();
   }
 
diff --git a/src/commands/cmd_cluster.cc b/src/commands/cmd_cluster.cc
index ab23f86d..4a6d5dbe 100644
--- a/src/commands/cmd_cluster.cc
+++ b/src/commands/cmd_cluster.cc
@@ -116,14 +116,14 @@ class CommandCluster : public Commander {
       // TODO: support multiple slot ranges
       Status s = srv->cluster->ImportSlotRange(conn, slot_ranges_[0], state_);
       if (s.IsOK()) {
-        *output = redis::SimpleString("OK");
+        *output = redis::RESP_OK;
       } else {
         return s;
       }
     } else if (subcommand_ == "reset") {
       Status s = srv->cluster->Reset();
       if (s.IsOK()) {
-        *output = redis::SimpleString("OK");
+        *output = redis::RESP_OK;
       } else {
         return s;
       }
@@ -258,7 +258,7 @@ class CommandClusterX : public Commander {
       Status s = srv->cluster->SetClusterNodes(nodes_str_, set_version_, 
force_);
       if (s.IsOK()) {
         need_persist_nodes_info = true;
-        *output = redis::SimpleString("OK");
+        *output = redis::RESP_OK;
       } else {
         return s;
       }
@@ -266,7 +266,7 @@ class CommandClusterX : public Commander {
       Status s = srv->cluster->SetNodeId(args_[2]);
       if (s.IsOK()) {
         need_persist_nodes_info = true;
-        *output = redis::SimpleString("OK");
+        *output = redis::RESP_OK;
       } else {
         return s;
       }
@@ -274,7 +274,7 @@ class CommandClusterX : public Commander {
       Status s = srv->cluster->SetSlotRanges(slot_ranges_, args_[4], 
set_version_);
       if (s.IsOK()) {
         need_persist_nodes_info = true;
-        *output = redis::SimpleString("OK");
+        *output = redis::RESP_OK;
       } else {
         return s;
       }
@@ -293,7 +293,7 @@ class CommandClusterX : public Commander {
         if (sync_migrate_) {
           return {Status::BlockingCmd};
         }
-        *output = redis::SimpleString("OK");
+        *output = redis::RESP_OK;
       } else {
         return s;
       }
@@ -331,7 +331,7 @@ class CommandReadOnly : public Commander {
  public:
   Status Execute([[maybe_unused]] engine::Context &ctx, [[maybe_unused]] 
Server *srv, Connection *conn,
                  std::string *output) override {
-    *output = redis::SimpleString("OK");
+    *output = redis::RESP_OK;
     conn->EnableFlag(redis::Connection::kReadOnly);
     return Status::OK();
   }
@@ -341,7 +341,7 @@ class CommandReadWrite : public Commander {
  public:
   Status Execute([[maybe_unused]] engine::Context &ctx, [[maybe_unused]] 
Server *srv, Connection *conn,
                  std::string *output) override {
-    *output = redis::SimpleString("OK");
+    *output = redis::RESP_OK;
     conn->DisableFlag(redis::Connection::kReadOnly);
     return Status::OK();
   }
@@ -352,7 +352,7 @@ class CommandAsking : public Commander {
   Status Execute([[maybe_unused]] engine::Context &ctx, [[maybe_unused]] 
Server *srv, Connection *conn,
                  std::string *output) override {
     conn->EnableFlag(redis::Connection::kAsking);
-    *output = redis::SimpleString("OK");
+    *output = redis::RESP_OK;
     return Status::OK();
   }
 };
diff --git a/src/commands/cmd_function.cc b/src/commands/cmd_function.cc
index ee5a580d..ff41257a 100644
--- a/src/commands/cmd_function.cc
+++ b/src/commands/cmd_function.cc
@@ -75,7 +75,7 @@ struct CommandFunction : Commander {
       auto s = lua::FunctionDelete(ctx, srv, libname);
       if (!s) return s;
 
-      *output = SimpleString("OK");
+      *output = RESP_OK;
       return Status::OK();
     } else {
       return {Status::NotOK, "no such subcommand"};
diff --git a/src/commands/cmd_hash.cc b/src/commands/cmd_hash.cc
index 9b9847fb..30d6eb28 100644
--- a/src/commands/cmd_hash.cc
+++ b/src/commands/cmd_hash.cc
@@ -249,7 +249,7 @@ class CommandHMSet : public Commander {
     if (GetAttributes()->name == "hset") {
       *output = redis::Integer(ret);
     } else {
-      *output = redis::SimpleString("OK");
+      *output = redis::RESP_OK;
     }
     return Status::OK();
   }
diff --git a/src/commands/cmd_hll.cc b/src/commands/cmd_hll.cc
index 35f6889d..3af558cd 100644
--- a/src/commands/cmd_hll.cc
+++ b/src/commands/cmd_hll.cc
@@ -93,7 +93,7 @@ class CommandPfMerge final : public Commander {
     if (!s.ok() && !s.IsNotFound()) {
       return {Status::RedisExecErr, s.ToString()};
     }
-    *output = redis::SimpleString("OK");
+    *output = redis::RESP_OK;
     return Status::OK();
   }
 };
diff --git a/src/commands/cmd_json.cc b/src/commands/cmd_json.cc
index 7f9de307..a18cf903 100644
--- a/src/commands/cmd_json.cc
+++ b/src/commands/cmd_json.cc
@@ -61,7 +61,7 @@ class CommandJsonSet : public Commander {
     auto s = json.Set(ctx, args_[1], args_[2], args_[3]);
     if (!s.ok()) return {Status::RedisExecErr, s.ToString()};
 
-    *output = redis::SimpleString("OK");
+    *output = redis::RESP_OK;
     return Status::OK();
   }
 };
@@ -337,7 +337,7 @@ class CommandJsonMerge : public Commander {
     if (!result) {
       *output = conn->NilString();
     } else {
-      *output = redis::SimpleString("OK");
+      *output = redis::RESP_OK;
     }
 
     return Status::OK();
@@ -648,7 +648,7 @@ class CommandJsonMSet : public Commander {
 
     if (auto s = json.MSet(ctx, user_keys, paths, values); !s.ok()) return 
{Status::RedisExecErr, s.ToString()};
 
-    *output = redis::SimpleString("OK");
+    *output = redis::RESP_OK;
     return Status::OK();
   }
 };
diff --git a/src/commands/cmd_key.cc b/src/commands/cmd_key.cc
index 023db7ca..827f6f52 100644
--- a/src/commands/cmd_key.cc
+++ b/src/commands/cmd_key.cc
@@ -367,7 +367,7 @@ class CommandRename : public Commander {
     auto s = redis.Copy(ctx, ns_key, new_ns_key, false, true, &res);
     if (!s.ok()) return {Status::RedisExecErr, s.ToString()};
     if (res == Database::CopyResult::KEY_NOT_EXIST) return 
{Status::RedisExecErr, "no such key"};
-    *output = redis::SimpleString("OK");
+    *output = redis::RESP_OK;
     return Status::OK();
   }
 };
diff --git a/src/commands/cmd_list.cc b/src/commands/cmd_list.cc
index 0cc2f121..367d8db6 100644
--- a/src/commands/cmd_list.cc
+++ b/src/commands/cmd_list.cc
@@ -625,7 +625,7 @@ class CommandLSet : public Commander {
       return {Status::RedisExecErr, errNoSuchKey};
     }
 
-    *output = redis::SimpleString("OK");
+    *output = redis::RESP_OK;
     return Status::OK();
   }
 
@@ -656,7 +656,7 @@ class CommandLTrim : public Commander {
       return {Status::RedisExecErr, s.ToString()};
     }
 
-    *output = redis::SimpleString("OK");
+    *output = redis::RESP_OK;
     return Status::OK();
   }
 
diff --git a/src/commands/cmd_replication.cc b/src/commands/cmd_replication.cc
index 71fd7fb6..e649516e 100644
--- a/src/commands/cmd_replication.cc
+++ b/src/commands/cmd_replication.cc
@@ -194,7 +194,7 @@ class CommandReplConf : public Commander {
     if (!ip_address_.empty()) {
       conn->SetAnnounceIP(ip_address_);
     }
-    *output = redis::SimpleString("OK");
+    *output = redis::RESP_OK;
     return Status::OK();
   }
 
diff --git a/src/commands/cmd_script.cc b/src/commands/cmd_script.cc
index 40c19a17..3098d005 100644
--- a/src/commands/cmd_script.cc
+++ b/src/commands/cmd_script.cc
@@ -83,7 +83,7 @@ class CommandScript : public Commander {
         LOG(ERROR) << "Failed to propagate script command: " << s.Msg();
         return s;
       }
-      *output = redis::SimpleString("OK");
+      *output = redis::RESP_OK;
     } else if (args_.size() >= 3 && subcommand_ == "exists") {
       *output = redis::MultiLen(args_.size() - 2);
       for (size_t j = 2; j < args_.size(); j++) {
diff --git a/src/commands/cmd_search.cc b/src/commands/cmd_search.cc
index f6d5ab05..bf4e527f 100644
--- a/src/commands/cmd_search.cc
+++ b/src/commands/cmd_search.cc
@@ -185,7 +185,7 @@ class CommandFTCreate : public Commander {
 
     GET_OR_RET(srv->index_mgr.Create(ctx, std::move(index_info_)));
 
-    output->append(redis::SimpleString("OK"));
+    output->append(redis::RESP_OK);
     return Status::OK();
   };
 
@@ -488,7 +488,7 @@ class CommandFTDrop : public Commander {
 
     GET_OR_RET(srv->index_mgr.Drop(ctx, index_name, conn->GetNamespace()));
 
-    output->append(SimpleString("OK"));
+    output->append(redis::RESP_OK);
 
     return Status::OK();
   };
diff --git a/src/commands/cmd_server.cc b/src/commands/cmd_server.cc
index 600f23c0..16fea925 100644
--- a/src/commands/cmd_server.cc
+++ b/src/commands/cmd_server.cc
@@ -54,7 +54,7 @@ class CommandAuth : public Commander {
         break;
     }
     conn->SetNamespace(ns);
-    *output = redis::SimpleString("OK");
+    *output = redis::RESP_OK;
     return Status::OK();
   }
 };
@@ -92,17 +92,17 @@ class CommandNamespace : public Commander {
       }
     } else if (args_.size() == 4 && sub_command == "set") {
       Status s = srv->GetNamespace()->Set(args_[2], args_[3]);
-      *output = s.IsOK() ? redis::SimpleString("OK") : redis::Error(s);
+      *output = s.IsOK() ? redis::RESP_OK : redis::Error(s);
       LOG(WARNING) << "Updated namespace: " << args_[2] << " with token: " << 
args_[3] << ", addr: " << conn->GetAddr()
                    << ", result: " << s.Msg();
     } else if (args_.size() == 4 && sub_command == "add") {
       Status s = srv->GetNamespace()->Add(args_[2], args_[3]);
-      *output = s.IsOK() ? redis::SimpleString("OK") : redis::Error(s);
+      *output = s.IsOK() ? redis::RESP_OK : redis::Error(s);
       LOG(WARNING) << "New namespace: " << args_[2] << " with token: " << 
args_[3] << ", addr: " << conn->GetAddr()
                    << ", result: " << s.Msg();
     } else if (args_.size() == 3 && sub_command == "del") {
       Status s = srv->GetNamespace()->Del(args_[2]);
-      *output = s.IsOK() ? redis::SimpleString("OK") : redis::Error(s);
+      *output = s.IsOK() ? redis::RESP_OK : redis::Error(s);
       LOG(WARNING) << "Deleted namespace: " << args_[2] << ", addr: " << 
conn->GetAddr() << ", result: " << s.Msg();
     } else {
       return {Status::RedisExecErr, "NAMESPACE subcommand must be one of GET, 
SET, DEL, ADD"};
@@ -145,7 +145,7 @@ class CommandFlushDB : public Commander {
     auto s = redis.FlushDB(ctx);
     LOG(WARNING) << "DB keys in namespace: " << conn->GetNamespace() << " was 
flushed, addr: " << conn->GetAddr();
     if (s.ok()) {
-      *output = redis::SimpleString("OK");
+      *output = redis::RESP_OK;
       return Status::OK();
     }
 
@@ -172,7 +172,7 @@ class CommandFlushAll : public Commander {
     auto s = redis.FlushAll(ctx);
     if (s.ok()) {
       LOG(WARNING) << "All DB keys was flushed, addr: " << conn->GetAddr();
-      *output = redis::SimpleString("OK");
+      *output = redis::RESP_OK;
       return Status::OK();
     }
 
@@ -199,7 +199,7 @@ class CommandSelect : public Commander {
  public:
   Status Execute([[maybe_unused]] engine::Context &ctx, [[maybe_unused]] 
Server *srv, [[maybe_unused]] Connection *conn,
                  std::string *output) override {
-    *output = redis::SimpleString("OK");
+    *output = redis::RESP_OK;
     return Status::OK();
   }
 };
@@ -222,7 +222,7 @@ class CommandConfig : public Commander {
       Status s = config->Rewrite(srv->GetNamespace()->List());
       if (!s.IsOK()) return s;
 
-      *output = redis::SimpleString("OK");
+      *output = redis::RESP_OK;
       LOG(INFO) << "# CONFIG REWRITE executed with success";
     } else if (args_.size() == 3 && sub_command == "get") {
       std::vector<std::string> values;
@@ -233,7 +233,7 @@ class CommandConfig : public Commander {
       if (!s.IsOK()) {
         return {Status::RedisExecErr, "CONFIG SET '" + args_[2] + "' error: " 
+ s.Msg()};
       } else {
-        *output = redis::SimpleString("OK");
+        *output = redis::RESP_OK;
       }
     } else {
       return {Status::RedisExecErr, "CONFIG subcommand must be one of GET, 
SET, REWRITE"};
@@ -311,7 +311,7 @@ class CommandDBSize : public Commander {
     } else if (args_.size() == 2 && util::EqualICase(args_[1], "scan")) {
       Status s = srv->AsyncScanDBSize(ns);
       if (s.IsOK()) {
-        *output = redis::SimpleString("OK");
+        *output = redis::RESP_OK;
       } else {
         return s;
       }
@@ -348,7 +348,7 @@ class CommandPerfLog : public Commander {
       *output = redis::Integer(static_cast<int64_t>(perf_log->Size()));
     } else if (subcommand_ == "reset") {
       perf_log->Reset();
-      *output = redis::SimpleString("OK");
+      *output = redis::RESP_OK;
     } else if (subcommand_ == "get") {
       *output = perf_log->GetLatestEntries(cnt_);
     }
@@ -384,7 +384,7 @@ class CommandSlowlog : public Commander {
     auto slowlog = srv->GetSlowLog();
     if (subcommand_ == "reset") {
       slowlog->Reset();
-      *output = redis::SimpleString("OK");
+      *output = redis::RESP_OK;
       return Status::OK();
     } else if (subcommand_ == "len") {
       *output = redis::Integer(static_cast<int64_t>(slowlog->Size()));
@@ -489,7 +489,7 @@ class CommandClient : public Commander {
       return Status::OK();
     } else if (subcommand_ == "setname") {
       conn->SetName(conn_name_);
-      *output = redis::SimpleString("OK");
+      *output = redis::RESP_OK;
       return Status::OK();
     } else if (subcommand_ == "getname") {
       std::string name = conn->GetName();
@@ -507,7 +507,7 @@ class CommandClient : public Commander {
         if (killed == 0)
           return {Status::RedisExecErr, "No such client"};
         else
-          *output = redis::SimpleString("OK");
+          *output = redis::RESP_OK;
       }
       return Status::OK();
     }
@@ -530,7 +530,7 @@ class CommandMonitor : public Commander {
   Status Execute([[maybe_unused]] engine::Context &ctx, [[maybe_unused]] 
Server *srv, Connection *conn,
                  std::string *output) override {
     conn->Owner()->BecomeMonitorConn(conn);
-    *output = redis::SimpleString("OK");
+    *output = redis::RESP_OK;
     return Status::OK();
   }
 };
@@ -556,7 +556,7 @@ class CommandQuit : public Commander {
   Status Execute([[maybe_unused]] engine::Context &ctx, [[maybe_unused]] 
Server *srv, Connection *conn,
                  std::string *output) override {
     conn->EnableFlag(redis::Connection::kCloseAfterReply);
-    *output = redis::SimpleString("OK");
+    *output = redis::RESP_OK;
     return Status::OK();
   }
 };
@@ -591,7 +591,7 @@ class CommandDebug : public Commander {
   Status Execute([[maybe_unused]] engine::Context &ctx, Server *srv, 
Connection *conn, std::string *output) override {
     if (subcommand_ == "sleep") {
       usleep(microsecond_);
-      *output = redis::SimpleString("OK");
+      *output = redis::RESP_OK;
     } else if (subcommand_ == "protocol") {  // protocol type
       if (protocol_type_ == "string") {
         *output = redis::BulkString("Hello World");
@@ -639,7 +639,7 @@ class CommandDebug : public Commander {
       }
     } else if (subcommand_ == "dbsize-limit") {
       srv->storage->SetDBSizeLimit(dbsize_limit_);
-      *output = redis::SimpleString("OK");
+      *output = redis::RESP_OK;
     } else {
       return {Status::RedisInvalidCmd, "Unknown subcommand, should be SLEEP, 
PROTOCOL or DBSIZE-LIMIT"};
     }
@@ -885,7 +885,7 @@ class CommandCompact : public Commander {
     Status s = srv->AsyncCompactDB(begin_key, end_key);
     if (!s.IsOK()) return s;
 
-    *output = redis::SimpleString("OK");
+    *output = redis::RESP_OK;
     LOG(INFO) << "Compact was triggered by manual with executed success";
     return Status::OK();
   }
@@ -901,7 +901,7 @@ class CommandBGSave : public Commander {
     Status s = srv->AsyncBgSaveDB();
     if (!s.IsOK()) return s;
 
-    *output = redis::SimpleString("OK");
+    *output = redis::RESP_OK;
     LOG(INFO) << "BGSave was triggered by manual with executed success";
     return Status::OK();
   }
@@ -917,7 +917,7 @@ class CommandFlushBackup : public Commander {
     Status s = srv->AsyncPurgeOldBackups(0, 0);
     if (!s.IsOK()) return s;
 
-    *output = redis::SimpleString("OK");
+    *output = redis::RESP_OK;
     LOG(INFO) << "flushbackup was triggered by manual with executed success";
     return Status::OK();
   }
@@ -979,7 +979,7 @@ class CommandSlaveOf : public Commander {
         return s.Prefixed("failed to remove master");
       }
 
-      *output = redis::SimpleString("OK");
+      *output = redis::RESP_OK;
       LOG(WARNING) << "MASTER MODE enabled (user request from '" << 
conn->GetAddr() << "')";
       if (srv->GetConfig()->cluster_enabled) {
         srv->slot_migrator->SetStopMigrationFlag(false);
@@ -993,7 +993,7 @@ class CommandSlaveOf : public Commander {
     if (!s.IsOK()) return s;
     s = srv->AddMaster(host_, port_, false);
     if (s.IsOK()) {
-      *output = redis::SimpleString("OK");
+      *output = redis::RESP_OK;
       LOG(WARNING) << "SLAVE OF " << host_ << ":" << port_ << " enabled (user 
request from '" << conn->GetAddr()
                    << "')";
       if (srv->GetConfig()->cluster_enabled) {
@@ -1096,7 +1096,7 @@ class CommandRestore : public Commander {
       auto now = util::GetTimeStampMS();
       if (ttl_ms_ <= now) {
         // return ok if the ttl is already expired
-        *output = redis::SimpleString("OK");
+        *output = redis::RESP_OK;
         return Status::OK();
       }
       ttl_ms_ -= now;
@@ -1106,7 +1106,7 @@ class CommandRestore : public Commander {
     RDB rdb(srv->storage, conn->GetNamespace(), std::move(stream_ptr));
     auto s = rdb.Restore(ctx, args_[1], args_[3], ttl_ms_);
     if (!s.IsOK()) return s;
-    *output = redis::SimpleString("OK");
+    *output = redis::RESP_OK;
     return Status::OK();
   }
 
@@ -1154,7 +1154,7 @@ class CommandRdb : public Commander {
     RDB rdb(srv->storage, conn->GetNamespace(), std::move(stream_ptr));
     GET_OR_RET(rdb.LoadRdb(ctx, db_index_, overwrite_exist_key_));
 
-    *output = redis::SimpleString("OK");
+    *output = redis::RESP_OK;
     return Status::OK();
   }
 
diff --git a/src/commands/cmd_stream.cc b/src/commands/cmd_stream.cc
index 903b0031..a7aa9802 100644
--- a/src/commands/cmd_stream.cc
+++ b/src/commands/cmd_stream.cc
@@ -547,7 +547,7 @@ class CommandXGroup : public Commander {
         return {Status::RedisExecErr, s.ToString()};
       }
 
-      *output = redis::SimpleString("OK");
+      *output = redis::RESP_OK;
     }
 
     if (subcommand_ == "destroy") {
@@ -599,7 +599,7 @@ class CommandXGroup : public Commander {
         return {Status::RedisExecErr, s.ToString()};
       }
 
-      *output = redis::SimpleString("OK");
+      *output = redis::RESP_OK;
     }
 
     return Status::OK();
@@ -1865,7 +1865,7 @@ class CommandXSetId : public Commander {
       return {Status::RedisExecErr, s.ToString()};
     }
 
-    *output = redis::SimpleString("OK");
+    *output = redis::RESP_OK;
 
     return Status::OK();
   }
diff --git a/src/commands/cmd_string.cc b/src/commands/cmd_string.cc
index 1322c74b..cfeb10ef 100644
--- a/src/commands/cmd_string.cc
+++ b/src/commands/cmd_string.cc
@@ -322,7 +322,7 @@ class CommandSet : public Commander {
       }
     } else {
       if (ret.has_value()) {
-        *output = redis::SimpleString("OK");
+        *output = redis::RESP_OK;
       } else {
         *output = conn->NilString();
       }
@@ -356,7 +356,7 @@ class CommandSetEX : public Commander {
     redis::String string_db(srv->storage, conn->GetNamespace());
 
     auto s = string_db.SetEX(ctx, args_[1], args_[3], expire_);
-    *output = redis::SimpleString("OK");
+    *output = redis::RESP_OK;
     return Status::OK();
   }
 
@@ -383,7 +383,7 @@ class CommandPSetEX : public Commander {
     redis::String string_db(srv->storage, conn->GetNamespace());
 
     auto s = string_db.SetEX(ctx, args_[1], args_[3], expire_);
-    *output = redis::SimpleString("OK");
+    *output = redis::RESP_OK;
     return Status::OK();
   }
 
@@ -413,7 +413,7 @@ class CommandMSet : public Commander {
       return {Status::RedisExecErr, s.ToString()};
     }
 
-    *output = redis::SimpleString("OK");
+    *output = redis::RESP_OK;
     return Status::OK();
   }
 };
diff --git a/src/commands/cmd_txn.cc b/src/commands/cmd_txn.cc
index 92113fee..4f3cb3a4 100644
--- a/src/commands/cmd_txn.cc
+++ b/src/commands/cmd_txn.cc
@@ -37,7 +37,7 @@ class CommandMulti : public Commander {
     conn->ResetMultiExec();
     // Client starts into MULTI-EXEC
     conn->EnableFlag(Connection::kMultiExec);
-    *output = redis::SimpleString("OK");
+    *output = redis::RESP_OK;
     return Status::OK();
   }
 };
@@ -52,7 +52,7 @@ class CommandDiscard : public Commander {
     auto reset_watch = MakeScopeExit([srv, conn] { 
srv->ResetWatchedKeys(conn); });
     conn->ResetMultiExec();
 
-    *output = redis::SimpleString("OK");
+    *output = redis::RESP_OK;
 
     return Status::OK();
   }
@@ -100,12 +100,12 @@ class CommandWatch : public Commander {
 
     // If a conn is already marked as watched_keys_modified, we can skip the 
watch.
     if (srv->IsWatchedKeysModified(conn)) {
-      *output = redis::SimpleString("OK");
+      *output = redis::RESP_OK;
       return Status::OK();
     }
 
     srv->WatchKey(conn, std::vector<std::string>(args_.begin() + 1, 
args_.end()));
-    *output = redis::SimpleString("OK");
+    *output = redis::RESP_OK;
     return Status::OK();
   }
 };
@@ -114,7 +114,7 @@ class CommandUnwatch : public Commander {
  public:
   Status Execute([[maybe_unused]] engine::Context &ctx, Server *srv, 
Connection *conn, std::string *output) override {
     srv->ResetWatchedKeys(conn);
-    *output = redis::SimpleString("OK");
+    *output = redis::RESP_OK;
     return Status::OK();
   }
 };
diff --git a/src/server/redis_reply.cc b/src/server/redis_reply.cc
index d01c823a..11a7eb12 100644
--- a/src/server/redis_reply.cc
+++ b/src/server/redis_reply.cc
@@ -37,7 +37,14 @@ 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; }
 
diff --git a/src/server/redis_reply.h b/src/server/redis_reply.h
index 9442e8c2..4a80124b 100644
--- a/src/server/redis_reply.h
+++ b/src/server/redis_reply.h
@@ -24,6 +24,7 @@
 
 #include <map>
 #include <string>
+#include <string_view>
 #include <vector>
 
 #include "rocksdb/status.h"
@@ -39,7 +40,9 @@ namespace redis {
 enum class RESP { v2, v3 };
 
 void Reply(evbuffer *output, const std::string &data);
-std::string SimpleString(const std::string &data);
+std::string SimpleString(std::string_view data);
+
+static const inline std::string RESP_OK = RESP_PREFIX_SIMPLE_STRING "OK" CRLF;
 
 std::string Error(const Status &s);
 std::string StatusToRedisErrorMsg(const Status &s);

Reply via email to