Skip to content

Commit 5a37227

Browse files
authored
[ISSUE #1173]🔥Optimize use CheetahString replace Std String🎨 (#1174)
1 parent a3b4711 commit 5a37227

File tree

103 files changed

+1248
-999
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+1248
-999
lines changed

Cargo.lock

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,4 @@ uuid = { version = "1.11.0", features = ["v4", # Lets you generate random UUIDs
8181

8282
futures = "0.3"
8383

84-
cheetah-string = { version = "0.1.4", features = ["serde", "bytes"] }
84+
cheetah-string = { version = "0.1.5", features = ["serde", "bytes"] }

rocketmq-broker/src/broker_runtime.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ impl BrokerRuntime {
216216
);
217217
broker_member_group.broker_addrs.insert(
218218
broker_config.broker_identity.broker_id,
219-
broker_config.get_broker_addr(),
219+
broker_config.get_broker_addr().into(),
220220
);
221221
Self {
222222
broker_config: broker_config.clone(),

rocketmq-broker/src/client/client_channel_info.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,27 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
17+
use cheetah_string::CheetahString;
1818
use rocketmq_common::TimeUtils::get_current_millis;
1919
use rocketmq_remoting::net::channel::Channel;
2020
use rocketmq_remoting::protocol::LanguageCode;
2121

2222
#[derive(Debug, Clone, Hash, PartialEq)]
2323
pub struct ClientChannelInfo {
2424
channel: Channel,
25-
client_id: String,
25+
client_id: CheetahString,
2626
language: LanguageCode,
2727
version: i32,
2828
last_update_timestamp: u64,
2929
}
3030

3131
impl ClientChannelInfo {
32-
pub fn new(channel: Channel, client_id: String, language: LanguageCode, version: i32) -> Self {
32+
pub fn new(
33+
channel: Channel,
34+
client_id: CheetahString,
35+
language: LanguageCode,
36+
version: i32,
37+
) -> Self {
3338
Self {
3439
channel,
3540
client_id,
@@ -39,7 +44,7 @@ impl ClientChannelInfo {
3944
}
4045
}
4146

42-
pub fn client_id(&self) -> &String {
47+
pub fn client_id(&self) -> &CheetahString {
4348
&self.client_id
4449
}
4550

@@ -55,8 +60,8 @@ impl ClientChannelInfo {
5560
self.last_update_timestamp
5661
}
5762

58-
pub fn set_client_id(&mut self, client_id: String) {
59-
self.client_id = client_id;
63+
pub fn set_client_id(&mut self, client_id: impl Into<CheetahString>) {
64+
self.client_id = client_id.into();
6065
}
6166

6267
pub fn set_language(&mut self, language: LanguageCode) {

rocketmq-broker/src/client/consumer_group_info.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use std::collections::HashMap;
1818
use std::collections::HashSet;
1919
use std::sync::Arc;
2020

21+
use cheetah_string::CheetahString;
2122
use parking_lot::Mutex;
2223
use parking_lot::RwLock;
2324
use rocketmq_common::common::consumer::consume_from_where::ConsumeFromWhere;
@@ -34,8 +35,8 @@ use crate::client::client_channel_info::ClientChannelInfo;
3435

3536
#[derive(Debug, Clone)]
3637
pub struct ConsumerGroupInfo {
37-
group_name: String,
38-
subscription_table: Arc<RwLock<HashMap<String, SubscriptionData>>>,
38+
group_name: CheetahString,
39+
subscription_table: Arc<RwLock<HashMap<CheetahString, SubscriptionData>>>,
3940
channel_info_table: Arc<RwLock<HashMap<Channel, ClientChannelInfo>>>,
4041
consume_type: Arc<RwLock<ConsumeType>>,
4142
message_model: Arc<RwLock<MessageModel>>,
@@ -45,13 +46,13 @@ pub struct ConsumerGroupInfo {
4546

4647
impl ConsumerGroupInfo {
4748
pub fn new(
48-
group_name: String,
49+
group_name: impl Into<CheetahString>,
4950
consume_type: ConsumeType,
5051
message_model: MessageModel,
5152
consume_from_where: ConsumeFromWhere,
5253
) -> Self {
5354
ConsumerGroupInfo {
54-
group_name,
55+
group_name: group_name.into(),
5556
subscription_table: Arc::new(RwLock::new(HashMap::new())),
5657
channel_info_table: Arc::new(RwLock::new(HashMap::new())),
5758
consume_type: Arc::new(RwLock::new(consume_type)),
@@ -61,9 +62,9 @@ impl ConsumerGroupInfo {
6162
}
6263
}
6364

64-
pub fn with_group_name(group_name: String) -> Self {
65+
pub fn with_group_name(group_name: impl Into<CheetahString>) -> Self {
6566
ConsumerGroupInfo {
66-
group_name,
67+
group_name: group_name.into(),
6768
subscription_table: Arc::new(RwLock::new(HashMap::new())),
6869
channel_info_table: Arc::new(RwLock::new(HashMap::new())),
6970
consume_type: Arc::new(RwLock::new(ConsumeType::ConsumePassively)),
@@ -83,7 +84,7 @@ impl ConsumerGroupInfo {
8384
None
8485
}
8586

86-
pub fn get_subscription_table(&self) -> Arc<RwLock<HashMap<String, SubscriptionData>>> {
87+
pub fn get_subscription_table(&self) -> Arc<RwLock<HashMap<CheetahString, SubscriptionData>>> {
8788
Arc::clone(&self.subscription_table)
8889
}
8990

@@ -101,7 +102,7 @@ impl ConsumerGroupInfo {
101102
channel_info_table.keys().cloned().collect()
102103
}
103104

104-
pub fn get_all_client_ids(&self) -> Vec<String> {
105+
pub fn get_all_client_ids(&self) -> Vec<CheetahString> {
105106
let channel_info_table = self.channel_info_table.read();
106107
channel_info_table
107108
.values()
@@ -195,7 +196,7 @@ impl ConsumerGroupInfo {
195196

196197
pub fn update_subscription(&self, sub_list: &HashSet<SubscriptionData>) -> bool {
197198
let mut updated = false;
198-
let mut topic_set: HashSet<String> = HashSet::new();
199+
let mut topic_set = HashSet::new();
199200

200201
let mut subscription_table = self.subscription_table.write();
201202
for sub in sub_list.iter() {
@@ -238,7 +239,7 @@ impl ConsumerGroupInfo {
238239
updated
239240
}
240241

241-
pub fn get_subscribe_topics(&self) -> HashSet<String> {
242+
pub fn get_subscribe_topics(&self) -> HashSet<CheetahString> {
242243
let subscription_table = self.subscription_table.read();
243244
subscription_table.keys().cloned().collect()
244245
}
@@ -266,7 +267,7 @@ impl ConsumerGroupInfo {
266267
*message_model_lock = message_model;
267268
}
268269

269-
pub fn get_group_name(&self) -> &String {
270+
pub fn get_group_name(&self) -> &CheetahString {
270271
&self.group_name
271272
}
272273

@@ -395,8 +396,8 @@ mod tests {
395396

396397
let mut sub_list = HashSet::new();
397398
let subscription_data = SubscriptionData {
398-
topic: "topic".to_string(),
399-
sub_string: "sub_string".to_string(),
399+
topic: "topic".into(),
400+
sub_string: "sub_string".into(),
400401
..Default::default()
401402
};
402403
sub_list.insert(subscription_data);

rocketmq-broker/src/client/manager/consumer_manager.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ impl ConsumerManager {
146146
consumer_group_info
147147
.get_subscription_table()
148148
.write()
149-
.insert(topic.to_string(), subscription_data.clone());
149+
.insert(topic.into(), subscription_data.clone());
150150
}
151151

152152
pub fn compensate_basic_consumer_info(

rocketmq-broker/src/client/manager/producer_manager.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
use std::collections::HashMap;
1919

20+
use cheetah_string::CheetahString;
2021
use rocketmq_common::TimeUtils::get_current_millis;
2122
use rocketmq_remoting::net::channel::Channel;
2223
use rocketmq_remoting::runtime::connection_handler_context::ConnectionHandlerContext;
@@ -26,9 +27,10 @@ use crate::client::client_channel_info::ClientChannelInfo;
2627

2728
#[derive(Default)]
2829
pub struct ProducerManager {
29-
group_channel_table:
30-
parking_lot::Mutex<HashMap<String /* group name */, HashMap<Channel, ClientChannelInfo>>>,
31-
client_channel_table: parking_lot::Mutex<HashMap<String, Channel /* client ip:port */>>,
30+
group_channel_table: parking_lot::Mutex<
31+
HashMap<CheetahString /* group name */, HashMap<Channel, ClientChannelInfo>>,
32+
>,
33+
client_channel_table: parking_lot::Mutex<HashMap<CheetahString, Channel /* client ip:port */>>,
3234
}
3335

3436
impl ProducerManager {
@@ -43,7 +45,7 @@ impl ProducerManager {
4345
impl ProducerManager {
4446
pub fn group_online(&self, group: String) -> bool {
4547
let binding = self.group_channel_table.lock();
46-
let channels = binding.get(&group);
48+
let channels = binding.get(group.as_str());
4749
if channels.is_none() {
4850
return false;
4951
}
@@ -82,10 +84,14 @@ impl ProducerManager {
8284
}
8385

8486
#[allow(clippy::mutable_key_type)]
85-
pub fn register_producer(&self, group: &str, client_channel_info: &ClientChannelInfo) {
87+
pub fn register_producer(
88+
&self,
89+
group: &CheetahString,
90+
client_channel_info: &ClientChannelInfo,
91+
) {
8692
let mut group_channel_table = self.group_channel_table.lock();
8793

88-
let key = group.to_string();
94+
let key = group.clone();
8995
let channel_table = group_channel_table.entry(key).or_default();
9096

9197
if let Some(client_channel_info_found) =

rocketmq-broker/src/hook/batch_check_before_put_message.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use std::collections::HashMap;
1818
use std::sync::Arc;
1919

20+
use cheetah_string::CheetahString;
2021
use rocketmq_common::common::config::TopicConfig;
2122
use rocketmq_common::common::message::message_ext::MessageExt;
2223
use rocketmq_store::base::message_result::PutMessageResult;
@@ -25,11 +26,13 @@ use rocketmq_store::hook::put_message_hook::PutMessageHook;
2526
use crate::util::hook_utils::HookUtils;
2627

2728
pub struct BatchCheckBeforePutMessageHook {
28-
topic_config_table: Arc<parking_lot::Mutex<HashMap<String, TopicConfig>>>,
29+
topic_config_table: Arc<parking_lot::Mutex<HashMap<CheetahString, TopicConfig>>>,
2930
}
3031

3132
impl BatchCheckBeforePutMessageHook {
32-
pub fn new(topic_config_table: Arc<parking_lot::Mutex<HashMap<String, TopicConfig>>>) -> Self {
33+
pub fn new(
34+
topic_config_table: Arc<parking_lot::Mutex<HashMap<CheetahString, TopicConfig>>>,
35+
) -> Self {
3336
Self { topic_config_table }
3437
}
3538
}

rocketmq-broker/src/out_api/broker_outer_api.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -128,17 +128,17 @@ impl BrokerOuterAPI {
128128
self.remoting_client.start(wrapper).await;
129129
}
130130

131-
pub async fn update_name_server_address_list(&self, addrs: String) {
131+
pub async fn update_name_server_address_list(&self, addrs: CheetahString) {
132132
let addr_vec = addrs
133-
.split("';'")
134-
.map(|s| s.to_string())
135-
.collect::<Vec<String>>();
133+
.split(";")
134+
.map(CheetahString::from_slice)
135+
.collect::<Vec<CheetahString>>();
136136
self.remoting_client
137137
.update_name_server_address_list(addr_vec)
138138
.await
139139
}
140140

141-
pub async fn update_name_server_address_list_by_dns_lookup(&self, domain: String) {
141+
pub async fn update_name_server_address_list_by_dns_lookup(&self, domain: CheetahString) {
142142
let address_list = dns_lookup_address_by_domain(domain.as_str());
143143
self.remoting_client
144144
.update_name_server_address_list(address_list)
@@ -233,7 +233,7 @@ impl BrokerOuterAPI {
233233

234234
async fn register_broker(
235235
&self,
236-
namesrv_addr: String,
236+
namesrv_addr: CheetahString,
237237
oneway: bool,
238238
timeout_mills: u64,
239239
request_header: RegisterBrokerRequestHeader,
@@ -338,7 +338,7 @@ impl BrokerOuterAPI {
338338

339339
pub async fn lock_batch_mq_async(
340340
&self,
341-
addr: String,
341+
addr: CheetahString,
342342
request_body: bytes::Bytes,
343343
timeout_millis: u64,
344344
) -> Result<HashSet<MessageQueue>> {
@@ -375,7 +375,7 @@ impl BrokerOuterAPI {
375375

376376
pub async fn unlock_batch_mq_async(
377377
&self,
378-
addr: String,
378+
addr: CheetahString,
379379
request_body: bytes::Bytes,
380380
timeout_millis: u64,
381381
) -> Result<()> {
@@ -409,7 +409,7 @@ impl BrokerOuterAPI {
409409
}
410410
}
411411

412-
fn dns_lookup_address_by_domain(domain: &str) -> Vec<String> {
412+
fn dns_lookup_address_by_domain(domain: &str) -> Vec<CheetahString> {
413413
let mut address_list = Vec::new();
414414
// Ensure logging is initialized
415415

@@ -419,7 +419,7 @@ fn dns_lookup_address_by_domain(domain: &str) -> Vec<String> {
419419
match lookup_host(domain_str) {
420420
Ok(addresses) => {
421421
for address in addresses {
422-
address_list.push(format!("{}{}", address, port_str));
422+
address_list.push(format!("{}{}", address, port_str).into());
423423
}
424424
info!(
425425
"DNS lookup address by domain success, domain={}, result={:?}",
@@ -450,7 +450,7 @@ mod tests {
450450
fn dns_lookup_address_by_domain_returns_correct_addresses() {
451451
let domain = "localhost:8080";
452452
let addresses = dns_lookup_address_by_domain(domain);
453-
assert!(addresses.contains(&"127.0.0.1:8080".to_string()));
453+
assert!(addresses.contains(&"127.0.0.1:8080".into()));
454454
}
455455

456456
#[test]

rocketmq-broker/src/processor/admin_broker_processor/broker_config_request_handler.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
use std::collections::HashMap;
1919

20+
use cheetah_string::CheetahString;
2021
use rocketmq_common::common::mix_all;
2122
use rocketmq_common::common::mq_version::RocketMqVersion;
2223
use rocketmq_remoting::code::request_code::RequestCode;
@@ -98,7 +99,7 @@ impl BrokerConfigRequestHandler {
9899
Some(response)
99100
}
100101

101-
fn prepare_runtime_info(&self) -> HashMap<String, String> {
102+
fn prepare_runtime_info(&self) -> HashMap<CheetahString, CheetahString> {
102103
let mut runtime_info = self.inner.default_message_store.get_runtime_info();
103104
self.inner
104105
.schedule_message_service
@@ -262,7 +263,7 @@ impl BrokerConfigRequestHandler {
262263
),
263264
);
264265
let store_path_root_dir = &self.inner.message_store_config.store_path_root_dir;
265-
let commit_log_dir = std::path::Path::new(&store_path_root_dir);
266+
let commit_log_dir = std::path::Path::new(store_path_root_dir.as_str());
266267
if commit_log_dir.exists() {
267268
let disks = Disks::new_with_refreshed_list();
268269
let path_str = commit_log_dir.to_str().unwrap();
@@ -283,6 +284,9 @@ impl BrokerConfigRequestHandler {
283284
}
284285
}
285286
runtime_info
287+
.into_iter()
288+
.map(|(k, v)| (CheetahString::from_string(k), CheetahString::from_string(v)))
289+
.collect()
286290
}
287291
fn is_special_service_running(&self) -> bool {
288292
true

rocketmq-broker/src/processor/admin_broker_processor/consumer_request_handler.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ impl ConsumerRequestHandler {
8080
connection.set_client_id(info.client_id().clone());
8181
connection.set_language(info.language());
8282
connection.set_version(info.version());
83-
connection.set_client_addr(channel.remote_address().to_string());
83+
connection.set_client_addr(channel.remote_address().to_string().into());
8484
body_data.get_connection_set().insert(connection);
8585
}
8686
let body = body_data.encode();
@@ -158,7 +158,7 @@ impl ConsumerRequestHandler {
158158
for i in 0..topic_config.unwrap().get_read_queue_nums() {
159159
let mut mq = MessageQueue::new();
160160
mq.set_topic(topic.to_string().into());
161-
mq.set_broker_name(self.inner.broker_config.broker_name.clone().into());
161+
mq.set_broker_name(self.inner.broker_config.broker_name.clone());
162162
mq.set_queue_id(i as i32);
163163

164164
let mut offset_wrapper = OffsetWrapper::new();

0 commit comments

Comments
 (0)