perf: 将 poll_points 改为 Arc<Vec<PollPointInfo>> 以减少 clone 开销

This commit is contained in:
caoqianming 2026-03-06 09:16:18 +08:00
parent 487d3cdf26
commit a2208e8958
1 changed files with 9 additions and 9 deletions

View File

@ -95,7 +95,7 @@ pub struct ConnectionStatus {
pub next_client_handle: u32, pub next_client_handle: u32,
pub client_handle_map: HashMap<u32, Uuid>, // client_handle -> point_id pub client_handle_map: HashMap<u32, Uuid>, // client_handle -> point_id
pub monitored_item_map: HashMap<Uuid, u32>, // point_id -> monitored_item_id pub monitored_item_map: HashMap<Uuid, u32>, // point_id -> monitored_item_id
pub poll_points: Vec<PollPointInfo>, // 正在轮询的点集合 pub poll_points: Arc<Vec<PollPointInfo>>, // 正在轮询的点集合
poll_handle: Option<JoinHandle<()>>, // 统一的轮询任务句柄 poll_handle: Option<JoinHandle<()>>, // 统一的轮询任务句柄
heartbeat_handle: Option<JoinHandle<()>>, // 心跳任务句柄 heartbeat_handle: Option<JoinHandle<()>>, // 心跳任务句柄
} }
@ -370,7 +370,7 @@ impl ConnectionManager {
let poll_points = { let poll_points = {
let status = status_ref.read().await; let status = status_ref.read().await;
status.get(&source_id) status.get(&source_id)
.map(|conn_status| conn_status.poll_points.clone()) .map(|conn_status| Arc::clone(&conn_status.poll_points))
.unwrap_or_default() .unwrap_or_default()
}; };
@ -458,7 +458,7 @@ impl ConnectionManager {
for point in points { for point in points {
// 检查点是否已经在轮询列表中 // 检查点是否已经在轮询列表中
if !conn_status.poll_points.iter().any(|p| p.point_id == point.point_id) { if !conn_status.poll_points.iter().any(|p| p.point_id == point.point_id) {
conn_status.poll_points.push(PollPointInfo { Arc::make_mut(&mut conn_status.poll_points).push(PollPointInfo {
point_id: point.point_id, point_id: point.point_id,
external_id: point.external_id.clone(), external_id: point.external_id.clone(),
}); });
@ -592,7 +592,7 @@ impl ConnectionManager {
next_client_handle: 1000, next_client_handle: 1000,
client_handle_map: HashMap::new(), client_handle_map: HashMap::new(),
monitored_item_map: HashMap::new(), monitored_item_map: HashMap::new(),
poll_points: Vec::new(), poll_points: Arc::new(Vec::new()),
poll_handle: None, poll_handle: None,
heartbeat_handle: None, heartbeat_handle: None,
}, },
@ -622,7 +622,7 @@ impl ConnectionManager {
client_handle_map: HashMap::new(), client_handle_map: HashMap::new(),
monitored_item_map: HashMap::new(), monitored_item_map: HashMap::new(),
next_client_handle: 1000, next_client_handle: 1000,
poll_points: Vec::new(), poll_points: Arc::new(Vec::new()),
poll_handle: None, poll_handle: None,
heartbeat_handle: None, heartbeat_handle: None,
}, },
@ -653,7 +653,7 @@ impl ConnectionManager {
{ {
let mut status = self.status.write().await; let mut status = self.status.write().await;
if let Some(conn_status) = status.get_mut(&source_id) { if let Some(conn_status) = status.get_mut(&source_id) {
conn_status.poll_points.clear(); Arc::make_mut(&mut conn_status.poll_points).clear();
if let Some(handle) = conn_status.poll_handle.take() { if let Some(handle) = conn_status.poll_handle.take() {
handle.abort(); handle.abort();
} }
@ -685,7 +685,7 @@ impl ConnectionManager {
{ {
let mut status = self.status.write().await; let mut status = self.status.write().await;
if let Some(conn_status) = status.get_mut(&source_id) { if let Some(conn_status) = status.get_mut(&source_id) {
conn_status.poll_points.clear(); Arc::make_mut(&mut conn_status.poll_points).clear();
if let Some(handle) = conn_status.poll_handle.take() { if let Some(handle) = conn_status.poll_handle.take() {
handle.abort(); handle.abort();
} }
@ -1154,7 +1154,7 @@ impl ConnectionManager {
.monitored_item_map .monitored_item_map
.insert(point.point_id, monitored_item_result.result.monitored_item_id); .insert(point.point_id, monitored_item_result.result.monitored_item_id);
// 从轮询列表中移除该点 // 从轮询列表中移除该点
conn_status.poll_points.retain(|p| p.point_id != point.point_id); Arc::make_mut(&mut conn_status.poll_points).retain(|p| p.point_id != point.point_id);
} }
} else { } else {
tracing::error!( tracing::error!(
@ -1302,7 +1302,7 @@ impl ConnectionManager {
let mut status = self.status.write().await; let mut status = self.status.write().await;
if let Some(conn_status) = status.get_mut(&source_id) { if let Some(conn_status) = status.get_mut(&source_id) {
let before_count = conn_status.poll_points.len(); let before_count = conn_status.poll_points.len();
conn_status.poll_points.retain(|p| !target_ids.contains(&p.point_id)); Arc::make_mut(&mut conn_status.poll_points).retain(|p| !target_ids.contains(&p.point_id));
let after_count = conn_status.poll_points.len(); let after_count = conn_status.poll_points.len();
before_count - after_count before_count - after_count
} else { } else {