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
+6 -7
View File
@@ -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<Fallback, FallbackFuture>(
async fn finish_service_installation<Fallback>(
installation: anyhow::Result<()>,
error_code: &str,
fallback: Fallback,
) -> CmdResult<ServiceInstallOutcome>
where
Fallback: FnOnce(String) -> FallbackFuture,
FallbackFuture: std::future::Future<Output = anyhow::Result<()>>,
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(())