Skip to content

Commit

Permalink
[ISSUE #1132]⚡️Optimize std String with Cheetah String🔥
Browse files Browse the repository at this point in the history
  • Loading branch information
mxsm committed Nov 12, 2024
1 parent ef76152 commit 0598720
Show file tree
Hide file tree
Showing 59 changed files with 1,294 additions and 1,304 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,4 @@ uuid = { version = "1.11.0", features = ["v4", # Lets you generate random UUIDs

futures = "0.3"

cheetah-string = { version = "0.1.0", features = ["serde", "bytes"] }
cheetah-string = { git = "https://github.com/mxsm/cheetah-string.git", features = ["serde", "bytes"] }
9 changes: 5 additions & 4 deletions rocketmq-broker/src/broker_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use std::sync::atomic::Ordering;
use std::sync::Arc;
use std::time::Duration;

use cheetah_string::CheetahString;
use rocketmq_common::common::broker::broker_config::BrokerConfig;
use rocketmq_common::common::config::TopicConfig;
use rocketmq_common::common::config_manager::ConfigManager;
Expand Down Expand Up @@ -955,10 +956,10 @@ impl BrokerRuntime {
.broker_cluster_name
.clone();
let broker_name = self.broker_config.broker_identity.broker_name.clone();
let broker_addr = format!(
let broker_addr = CheetahString::from_string(format!(
"{}:{}",
self.broker_config.broker_ip1, self.server_config.listen_port
);
));
let broker_id = self.broker_config.broker_identity.broker_id;
let weak = Arc::downgrade(&self.broker_out_api);
self.broker_out_api
Expand Down Expand Up @@ -1074,10 +1075,10 @@ impl BrokerRuntimeInner {
.broker_cluster_name
.clone();
let broker_name = self.broker_config.broker_identity.broker_name.clone();
let broker_addr = format!(
let broker_addr = CheetahString::from_string(format!(
"{}:{}",
self.broker_config.broker_ip1, self.server_config.listen_port
);
));
let broker_id = self.broker_config.broker_identity.broker_id;
let weak = Arc::downgrade(&self.broker_out_api);
self.broker_out_api
Expand Down
21 changes: 11 additions & 10 deletions rocketmq-broker/src/mqtrace/consume_message_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,36 @@
use std::collections::HashMap;
use std::sync::Arc;

use cheetah_string::CheetahString;
use rocketmq_common::common::config::TopicConfig;
use rocketmq_store::stats::stats_type::StatsType;

#[derive(Default)]
pub struct ConsumeMessageContext {
pub consumer_group: String,
pub topic: String,
pub consumer_group: CheetahString,
pub topic: CheetahString,
pub queue_id: Option<i32>,
pub client_host: String,
pub store_host: String,
pub client_host: CheetahString,
pub store_host: CheetahString,
pub message_ids: HashMap<String, i64>,
pub body_length: i32,
pub success: bool,
pub status: String,
pub status: CheetahString,
//mq_trace_context: Option<Box<dyn std::any::Any>>, // Replace with actual type
pub topic_config: Arc<TopicConfig>,

pub account_auth_type: Option<String>,
pub account_owner_parent: Option<String>,
pub account_owner_self: Option<String>,
pub account_auth_type: Option<CheetahString>,
pub account_owner_parent: Option<CheetahString>,
pub account_owner_self: Option<CheetahString>,
pub rcv_msg_num: i32,
pub rcv_msg_size: i32,
pub rcv_stat: StatsType,
pub commercial_rcv_msg_num: i32,

pub commercial_owner: Option<String>,
pub commercial_owner: Option<CheetahString>,
pub commercial_rcv_stats: StatsType,
pub commercial_rcv_times: i32,
pub commercial_rcv_size: i32,

pub namespace: String,
pub namespace: CheetahString,
}
51 changes: 26 additions & 25 deletions rocketmq-broker/src/mqtrace/send_message_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,41 @@
use std::any::Any;
use std::collections::HashMap;

use cheetah_string::CheetahString;
use rocketmq_common::common::message::message_enum::MessageType;
use rocketmq_store::stats::stats_type::StatsType;

#[derive(Debug, Default)]
pub struct SendMessageContext {
pub namespace: String,
pub producer_group: String,
pub topic: String,
pub msg_id: String,
pub origin_msg_id: String,
pub namespace: CheetahString,
pub producer_group: CheetahString,
pub topic: CheetahString,
pub msg_id: CheetahString,
pub origin_msg_id: CheetahString,
pub queue_id: Option<i32>,
pub queue_offset: Option<i64>,
pub broker_addr: String,
pub born_host: String,
pub broker_addr: CheetahString,
pub born_host: CheetahString,
pub body_length: i32,
pub code: i32,
pub error_msg: String,
pub msg_props: String,
pub error_msg: CheetahString,
pub msg_props: CheetahString,
pub mq_trace_context: Option<Box<dyn Any + Send + 'static>>,
pub ext_props: HashMap<String, String>,
pub broker_region_id: String,
pub msg_unique_key: String,
pub broker_region_id: CheetahString,
pub msg_unique_key: CheetahString,
pub born_time_stamp: i64,
pub request_time_stamp: i64,
pub msg_type: MessageType,
pub is_success: bool,
pub account_auth_type: String,
pub account_owner_parent: String,
pub account_owner_self: String,
pub account_auth_type: CheetahString,
pub account_owner_parent: CheetahString,
pub account_owner_self: CheetahString,
pub send_msg_num: i32,
pub send_msg_size: i32,
pub send_stat: StatsType,
pub commercial_send_msg_num: i32,
pub commercial_owner: String,
pub commercial_owner: CheetahString,
pub commercial_send_stats: StatsType,
pub commercial_send_size: i32,
pub commercial_send_times: i32,
Expand All @@ -60,19 +61,19 @@ impl SendMessageContext {
Self::default()
}

pub fn topic(&mut self, topic: String) {
pub fn topic(&mut self, topic: CheetahString) {
self.topic = topic;
}

pub fn body_length(&mut self, body_length: i32) {
self.body_length = body_length;
}

pub fn msg_props(&mut self, msg_props: String) {
pub fn msg_props(&mut self, msg_props: CheetahString) {
self.msg_props = msg_props;
}

pub fn broker_addr(&mut self, broker_addr: String) {
pub fn broker_addr(&mut self, broker_addr: CheetahString) {
self.broker_addr = broker_addr;
}

Expand All @@ -84,15 +85,15 @@ impl SendMessageContext {
self.queue_offset = Some(queue_offset);
}

pub fn born_host(&mut self, born_host: String) {
pub fn born_host(&mut self, born_host: CheetahString) {
self.born_host = born_host;
}

pub fn broker_region_id(&mut self, broker_region_id: String) {
pub fn broker_region_id(&mut self, broker_region_id: CheetahString) {
self.broker_region_id = broker_region_id;
}

pub fn msg_unique_key(&mut self, msg_unique_key: String) {
pub fn msg_unique_key(&mut self, msg_unique_key: CheetahString) {
self.msg_unique_key = msg_unique_key;
}

Expand All @@ -112,15 +113,15 @@ impl SendMessageContext {
self.is_success = is_success;
}

pub fn account_auth_type(&mut self, account_auth_type: String) {
pub fn account_auth_type(&mut self, account_auth_type: CheetahString) {
self.account_auth_type = account_auth_type;
}

pub fn account_owner_parent(&mut self, account_owner_parent: String) {
pub fn account_owner_parent(&mut self, account_owner_parent: CheetahString) {
self.account_owner_parent = account_owner_parent;
}

pub fn account_owner_self(&mut self, account_owner_self: String) {
pub fn account_owner_self(&mut self, account_owner_self: CheetahString) {
self.account_owner_self = account_owner_self;
}

Expand All @@ -140,7 +141,7 @@ impl SendMessageContext {
self.commercial_send_msg_num = commercial_send_msg_num;
}

pub fn commercial_owner(&mut self, commercial_owner: String) {
pub fn commercial_owner(&mut self, commercial_owner: CheetahString) {
self.commercial_owner = commercial_owner;
}

Expand Down
31 changes: 23 additions & 8 deletions rocketmq-broker/src/out_api/broker_outer_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl BrokerOuterAPI {

fn create_request(broker_name: CheetahString, topic_config: TopicConfig) -> RemotingCommand {
let request_header =
RegisterTopicRequestHeader::new(topic_config.topic_name.as_ref().unwrap());
RegisterTopicRequestHeader::new(topic_config.topic_name.as_ref().cloned().unwrap());
let queue_data = QueueData::new(
broker_name.clone(),
topic_config.read_queue_nums,
Expand Down Expand Up @@ -148,10 +148,10 @@ impl BrokerOuterAPI {
pub async fn register_broker_all(
&self,
cluster_name: CheetahString,
broker_addr: String,
broker_addr: CheetahString,
broker_name: CheetahString,
broker_id: u64,
ha_server_addr: String,
ha_server_addr: CheetahString,
topic_config_wrapper: TopicConfigAndMappingSerializeWrapper,
filter_server_list: Vec<String>,
oneway: bool,
Expand Down Expand Up @@ -273,9 +273,16 @@ impl BrokerOuterAPI {
response.decode_command_custom_header::<RegisterBrokerResponseHeader>();
let mut result = RegisterBrokerResult::default();
if let Some(header) = register_broker_result {
result.ha_server_addr =
header.ha_server_addr.clone().unwrap_or("".to_string());
result.master_addr = header.master_addr.clone().unwrap_or("".to_string());
result.ha_server_addr = header
.ha_server_addr
.clone()
.unwrap_or(CheetahString::empty())
.to_string();
result.master_addr = header
.master_addr
.clone()
.unwrap_or(CheetahString::empty())
.to_string();
}
if let Some(body) = response.body() {
result.kv_table = SerdeJsonUtils::decode::<KVTable>(body.as_ref()).unwrap();
Expand Down Expand Up @@ -353,7 +360,11 @@ impl BrokerOuterAPI {
} else {
Err(BrokerError::MQBrokerError(
response.code(),
response.remark().cloned().unwrap_or("".to_string()),
response
.remark()
.cloned()
.unwrap_or(CheetahString::empty())
.to_json(),
"".to_string(),
))
}
Expand Down Expand Up @@ -384,7 +395,11 @@ impl BrokerOuterAPI {
} else {
Err(BrokerError::MQBrokerError(
response.code(),
response.remark().cloned().unwrap_or("".to_string()),
response
.remark()
.cloned()
.unwrap_or(CheetahString::empty())
.to_string(),
"".to_string(),
))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl ConsumerRequestHandler {
.consumer_offset_manager
.which_topic_by_consumer(request_header.get_consumer_group());
} else {
topics.insert(request_header.get_topic().clone());
topics.insert(request_header.get_topic().to_string());
}
for topic in topics.iter() {
let topic_config = self.inner.topic_config_manager.select_topic_config(topic);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use std::collections::HashMap;
use std::collections::HashSet;

use cheetah_string::CheetahString;
use rocketmq_common::common::attribute::attribute_parser::AttributeParser;
use rocketmq_common::common::attribute::topic_message_type::TopicMessageType;
use rocketmq_common::common::config::TopicConfig;
Expand Down Expand Up @@ -109,7 +110,7 @@ impl TopicRequestHandler {
request_header
.attributes
.clone()
.unwrap_or("".to_string())
.unwrap_or(CheetahString::empty())
.as_str(),
) {
Ok(value) => value,
Expand Down
2 changes: 1 addition & 1 deletion rocketmq-broker/src/processor/client_manage_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ where

let client_channel_info = ClientChannelInfo::new(
channel.clone(),
request_header.client_id.clone(),
request_header.client_id.to_string(),
request.language(),
request.version(),
);
Expand Down
6 changes: 3 additions & 3 deletions rocketmq-broker/src/processor/consumer_manage_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,8 @@ where
.topic_queue_mapping_info
.bname
.as_ref()
.unwrap_or(&"".to_string())
.cloned()
.unwrap_or_default()
),
));
}
Expand All @@ -354,8 +355,7 @@ where
break;
}
} else {
request_header
.set_broker_name(mapping_item.bname.clone().unwrap_or("".to_string()));
request_header.set_broker_name(mapping_item.bname.clone().unwrap_or_default());
request_header.queue_id = mapping_item.queue_id;
request_header.set_lo(Some(false));
request_header.set_zero_if_not_found = Some(true);
Expand Down
Loading

0 comments on commit 0598720

Please sign in to comment.