This is an automated email from the ASF dual-hosted git repository.
chenBright pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brpc.git
The following commit(s) were added to refs/heads/master by this push:
new d3164512b Use StringPiece when hashing Redis Cluster keys (#3781)
d3164512b is described below
commit d3164512bd36da617f725e8e27f618f4a370fde2
Author: nkysg <[email protected]>
AuthorDate: Sun Nov 29 08:32:33 2026 +0800
Use StringPiece when hashing Redis Cluster keys (#3781)
---
src/brpc/redis_cluster.cpp | 6 +++---
src/brpc/redis_cluster.h | 4 +++-
2 files changed, 6 insertions(+), 4 deletions(-)
diff --git a/src/brpc/redis_cluster.cpp b/src/brpc/redis_cluster.cpp
index a06cc5979..bf55dbbca 100644
--- a/src/brpc/redis_cluster.cpp
+++ b/src/brpc/redis_cluster.cpp
@@ -992,7 +992,7 @@ Channel* RedisClusterChannel::GetOrCreateChannel(const
std::string& endpoint) {
}
uint16_t RedisClusterChannel::HashSlot(const std::string& key) {
- const std::string hashed = ExtractHashtag(key);
+ const butil::StringPiece hashed = ExtractHashtag(key);
uint16_t crc = 0;
for (size_t i = 0; i < hashed.size(); ++i) {
const uint8_t idx = static_cast<uint8_t>((crc >> 8) ^
@@ -1002,7 +1002,7 @@ uint16_t RedisClusterChannel::HashSlot(const std::string&
key) {
return crc & (kRedisClusterSlotCount - 1);
}
-std::string RedisClusterChannel::ExtractHashtag(const std::string& key) {
+butil::StringPiece RedisClusterChannel::ExtractHashtag(const std::string& key)
{
const size_t begin = key.find('{');
if (begin == std::string::npos) {
return key;
@@ -1011,7 +1011,7 @@ std::string RedisClusterChannel::ExtractHashtag(const
std::string& key) {
if (end == std::string::npos || end == begin + 1) {
return key;
}
- return key.substr(begin + 1, end - begin - 1);
+ return butil::StringPiece(key.data() + begin + 1, end - begin - 1);
}
bool RedisClusterChannel::ParseRedirectReply(const SingleCommandResult& result,
diff --git a/src/brpc/redis_cluster.h b/src/brpc/redis_cluster.h
index d8b99701a..99038a0fd 100644
--- a/src/brpc/redis_cluster.h
+++ b/src/brpc/redis_cluster.h
@@ -28,6 +28,7 @@
#include "bthread/bthread.h"
#include "butil/containers/doubly_buffered_data.h"
+#include "butil/strings/string_piece.h"
#include "butil/synchronization/lock.h"
#include "brpc/channel.h"
#include "brpc/channel_base.h"
@@ -148,7 +149,8 @@ private:
Channel* GetOrCreateChannel(const std::string& endpoint);
static uint16_t HashSlot(const std::string& key);
- static std::string ExtractHashtag(const std::string& key);
+ // Returns a view into `key` that is valid while `key` remains alive.
+ static butil::StringPiece ExtractHashtag(const std::string& key);
static bool ParseRedirectReply(const SingleCommandResult& result,
RedirectInfo* redirect);
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]