From 09d05b3f369445a80ab6e7744a35a8ff17a76e8f Mon Sep 17 00:00:00 2001 From: Tunglies Date: Mon, 23 Nov 2026 09:23:13 +0000 Subject: [PATCH] perf(backend): skip runtime mapping copies in proxy-port save (#8719) * perf(feat): share runtime mappings across port-save probes via Arc Every proxy-port save deep-copied the full runtime Mapping twice (current and candidate) so the rare post-restart re-probe could retain ownership. Wrap both in Arc once: the first probe takes refcount clones, the re-probe moves the same Arcs. Probe inputs are identical mappings and the spawn_blocking Send + 'static bounds are unchanged. Measured on a representative 200-proxy/30-group runtime config: one Mapping clone costs 10,053 allocations / 1.02 MiB; the Arc handoff costs 0 / 0. Both copies per save are gone. * perf(config): borrow through selected-node persistence closures record_selected_node and persist_reconciled_selected copied the group, node and profile strings up-front for the persist_selected_update closure, including on the no-change early-return path. The closure bound is FnOnce + Send without 'static (the same shape forget_selected_node already relies on), so capture &str and &[PrfSelected] borrows instead and construct owned strings only at the storage points. as_deref comparisons keep content semantics. * chore(feat): drop a narration comment on the Arc handoff --- src-tauri/src/config/profiles.rs | 20 ++++++++------------ src-tauri/src/feat/listener.rs | 11 ++++++----- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/src-tauri/src/config/profiles.rs b/src-tauri/src/config/profiles.rs index e6ab22168..e0e708081 100644 --- a/src-tauri/src/config/profiles.rs +++ b/src-tauri/src/config/profiles.rs @@ -753,8 +753,6 @@ fn current_item_mut(profiles: &mut IProfiles) -> Option<&mut PrfItem> { /// Records a backend-made selection so the next core start restores it. pub(crate) async fn record_selected_node(group_name: &str, node: &str) -> Result<()> { - let group_name = String::from(group_name); - let node = String::from(node); persist_selected_update(true, move |mut profiles| async move { let Some(item) = current_item_mut(&mut profiles) else { return Ok((profiles, false)); @@ -763,17 +761,17 @@ pub(crate) async fn record_selected_node(group_name: &str, node: &str) -> Result let selected = item.selected.get_or_insert_with(Vec::new); match selected .iter_mut() - .find(|entry| entry.name.as_ref() == Some(&group_name)) + .find(|entry| entry.name.as_deref() == Some(group_name)) { Some(entry) => { - if entry.now.as_ref() == Some(&node) { + if entry.now.as_deref() == Some(node) { return Ok((profiles, false)); } - entry.now = Some(node.clone()); + entry.now = Some(node.into()); } None => selected.push(PrfSelected { - name: Some(group_name), - now: Some(node), + name: Some(group_name.into()), + now: Some(node.into()), }), } profiles.save_file().await?; @@ -821,19 +819,17 @@ async fn persist_reconciled_selected( return Ok(()); } - let profile_uid = String::from(profile_uid); - let original_selected = original_selected.to_vec(); persist_selected_update(false, move |mut profiles| async move { - if !is_activation_current(generation) || profiles.current.as_ref() != Some(&profile_uid) { + if !is_activation_current(generation) || profiles.current.as_deref() != Some(profile_uid) { return Ok((profiles, false)); } let profile = profiles .items .as_mut() - .and_then(|items| items.iter_mut().find(|item| item.uid.as_ref() == Some(&profile_uid))) + .and_then(|items| items.iter_mut().find(|item| item.uid.as_deref() == Some(profile_uid))) .with_context(|| format!("failed to find the profile item \"uid:{profile_uid}\""))?; - if profile.selected.as_deref().unwrap_or(&[]) != original_selected.as_slice() { + if profile.selected.as_deref().unwrap_or(&[]) != original_selected { return Ok((profiles, false)); } diff --git a/src-tauri/src/feat/listener.rs b/src-tauri/src/feat/listener.rs index 8da7cd0aa..df75da232 100644 --- a/src-tauri/src/feat/listener.rs +++ b/src-tauri/src/feat/listener.rs @@ -20,6 +20,7 @@ use anyhow::{Context as _, Result, anyhow, bail}; use clash_verge_draft::DraftTransaction; use clash_verge_logging::{Type, logging}; use std::mem::DropGuard; +use std::sync::Arc; pub async fn probe_listener(request: ListenerProbe) -> Result { AsyncHandler::spawn_blocking(move || probe_listener_sync(&request)) @@ -40,7 +41,7 @@ pub async fn save_proxy_ports(settings: ProxyPortSettings) -> Result Result Result { } async fn probe_proxy_ports( - current: serde_yaml_ng::Mapping, - candidate: serde_yaml_ng::Mapping, + current: Arc, + candidate: Arc, current_core_is_running: bool, ) -> Result { AsyncHandler::spawn_blocking(move || probe_proxy_port_change(¤t, &candidate, current_core_is_running))