From 6bb7d7115676b61229bfc70f47f3219427ad3d38 Mon Sep 17 00:00:00 2001 From: Tunglies Date: Mon, 23 Nov 2026 09:23:15 +0000 Subject: [PATCH] perf(backend): move instead of copy in enhance, tray and sidecar fallback (#8721) * perf(enhance): move tun mapping and alpn strings instead of cloning enforce_tun deep-cloned the tun mapping two lines before overwriting it; take_mapping now moves it instead (end state identical for absent, non-mapping and mapping values, including key order). builtin_hy_alpn cloned each hysteria node's alpn String only to dodge a borrow conflict while re-inserting under the same key; take it in place via get_mut + mem::take. The in-place guard preserves non-string alpn values that a remove-based shape would silently drop. Measured per enhance run on 200 hysteria nodes: hy_alpn 600 -> 200 allocations; enforce_tun 29 -> 3. * perf(tray): hoist tooltip version string update_tooltip re-derived the version string from CARGO_PKG_VERSION on every tray refresh; compute it once in a LazyLock. std::string::String is spelled out because bare String in this module resolves to the tristr alias. * refactor(cmd): take sidecar fallback as AsyncFnOnce(&str) finish_service_installation's FnOnce(String) -> FallbackFuture bound forced reason.clone() before the call. AsyncFnOnce(&str) borrows the reason and moves it into the Sidecar outcome - one allocation instead of two - and drops the FallbackFuture generic entirely. The windows-only call site's closure shape is verified with standalone rustc (edition 2024) repros; Windows CI compiles it definitively. * refactor(tray): store the tooltip version as a static str The only consumer reads the value as a str, so the String static allocated even when the version carries no build metadata. Return the env! literal directly in that case and leak the trimmed form otherwise; the LazyLock already owns the value for the process lifetime. * chore(enhance): drop comment on the alpn take --- src-tauri/src/cmd/service.rs | 13 ++++++------- src-tauri/src/core/tray/mod.rs | 15 ++++++++++----- src-tauri/src/enhance/mod.rs | 14 +++++++------- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src-tauri/src/cmd/service.rs b/src-tauri/src/cmd/service.rs index 592df0417..4a3445fed 100644 --- a/src-tauri/src/cmd/service.rs +++ b/src-tauri/src/cmd/service.rs @@ -34,8 +34,8 @@ async fn execute_service_operation_sync(status: ServiceStatus, error_code: &str) SERVICE_MANAGER.handle_service_status(status).await }; #[cfg(windows)] - return finish_service_installation(result, error_code, |reason| async move { - manager.continue_with_sidecar(Some(&reason)).await + return finish_service_installation(result, error_code, async |reason| { + manager.continue_with_sidecar(Some(reason)).await }) .await; @@ -46,14 +46,13 @@ async fn execute_service_operation_sync(status: ServiceStatus, error_code: &str) } #[cfg(any(windows, test))] -async fn finish_service_installation( +async fn finish_service_installation( installation: anyhow::Result<()>, error_code: &str, fallback: Fallback, ) -> CmdResult where - Fallback: FnOnce(String) -> FallbackFuture, - FallbackFuture: std::future::Future>, + Fallback: AsyncFnOnce(&str) -> anyhow::Result<()>, { let Err(error) = installation else { return Ok(ServiceInstallOutcome::Installed); @@ -75,7 +74,7 @@ where return Err(proxy_aware_coded_error(&error, error_code)); } let reason = reasons.join("\n"); - fallback(reason.clone()) + fallback(reason.as_str()) .await .map_err(|error| proxy_aware_coded_error(&error, "SERVICE_SIDECAR_FAILED"))?; Ok(ServiceInstallOutcome::Sidecar { reason }) @@ -191,7 +190,7 @@ mod tests { None => anyhow::anyhow!("{reason}"), } .context("install service failed"); - let result = finish_service_installation(Err(error), "SERVICE_INSTALL_FAILED", |_| async { + let result = finish_service_installation(Err(error), "SERVICE_INSTALL_FAILED", async |_| { fallback_called.set(true); anyhow::ensure!(fallback_ready, "another core is running"); Ok(()) diff --git a/src-tauri/src/core/tray/mod.rs b/src-tauri/src/core/tray/mod.rs index 4f474a782..052fa05f9 100644 --- a/src-tauri/src/core/tray/mod.rs +++ b/src-tauri/src/core/tray/mod.rs @@ -21,6 +21,7 @@ use super::handle; use anyhow::Result; use std::borrow::Cow; use std::collections::HashMap; +use std::sync::LazyLock; use std::time::Duration; use tauri::{ AppHandle, Wry, @@ -40,6 +41,14 @@ type ProxyMenuItem = (Option>, Vec>>); const TRAY_CLICK_DEBOUNCE_MS: u64 = 300; pub const TRAY_ID: &str = "clash-verge-rev-tray"; +static TOOLTIP_VERSION: LazyLock<&'static str> = LazyLock::new(|| { + let v = env!("CARGO_PKG_VERSION"); + v.split_once('+').map_or_else( + || v, + |(main, rest)| Box::leak(format!("{main}+{}", rest.split('.').next().unwrap_or("")).into_boxed_str()), + ) +}); + #[derive(Clone, Copy)] struct TrayMenuOptions<'a> { mode: Option<&'a str>, @@ -352,11 +361,7 @@ impl Tray { let tun_text = clash_verge_i18n::t!("tray.tooltip.tun"); let profile_text = clash_verge_i18n::t!("tray.tooltip.profile"); - let v = env!("CARGO_PKG_VERSION"); - let reassembled_version = v.split_once('+').map_or_else( - || v.into(), - |(main, rest)| format!("{main}+{}", rest.split('.').next().unwrap_or("")), - ); + let reassembled_version = *TOOLTIP_VERSION; let tooltip = format!( "Clash Verge {}\n{}: {}\n{}: {}\n{}: {}", diff --git a/src-tauri/src/enhance/mod.rs b/src-tauri/src/enhance/mod.rs index 8fa3c3944..ed541cf4f 100644 --- a/src-tauri/src/enhance/mod.rs +++ b/src-tauri/src/enhance/mod.rs @@ -526,11 +526,7 @@ fn enforce_tun(mut config: Mapping, snapshot: Mapping) -> Mapping { if snapshot.is_empty() { return config; } - let mut tun = config - .get("tun") - .and_then(Value::as_mapping) - .cloned() - .unwrap_or_default(); + let mut tun = take_mapping(&mut config, "tun"); tun.extend(snapshot); config.insert(Value::from("tun"), Value::Mapping(tun)); config @@ -673,8 +669,12 @@ fn builtin_hy_alpn(mut config: Mapping) -> Mapping { for proxy in proxies.iter_mut() { let Some(proxy) = proxy.as_mapping_mut() else { continue }; let is_hysteria = proxy.get("type").and_then(Value::as_str) == Some("hysteria"); - if is_hysteria && let Some(Value::String(alpn)) = proxy.get("alpn").cloned() { - proxy.insert("alpn".into(), Value::Sequence(vec![Value::String(alpn)])); + if is_hysteria + && let Some(alpn) = proxy.get_mut("alpn") + && let Value::String(text) = alpn + { + let text = std::mem::take(text); + *alpn = Value::Sequence(vec![Value::String(text)]); } } }