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
This commit is contained in:
Tunglies
2026-11-23 09:23:15 +00:00
committed by GitHub
parent 09d05b3f36
commit 6bb7d71156
3 changed files with 23 additions and 19 deletions
+10 -5
View File
@@ -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<Submenu<Wry>>, Vec<Box<dyn IsMenuItem<Wry>>>);
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{}: {}",