mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-12-05 14:10:18 +08:00
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
This commit is contained in:
@@ -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<ListenerProbeOutcome> {
|
||||
AsyncHandler::spawn_blocking(move || probe_listener_sync(&request))
|
||||
@@ -40,7 +41,7 @@ pub async fn save_proxy_ports(settings: ProxyPortSettings) -> Result<SaveProxyPo
|
||||
});
|
||||
|
||||
// Read first because transaction rollback discards all claimed drafts.
|
||||
let current = current_runtime_mapping().await?;
|
||||
let current = Arc::new(current_runtime_mapping().await?);
|
||||
|
||||
let clash = Config::clash().await;
|
||||
let verge = Config::verge().await;
|
||||
@@ -63,10 +64,10 @@ pub async fn save_proxy_ports(settings: ProxyPortSettings) -> Result<SaveProxyPo
|
||||
Config::generate()
|
||||
.await
|
||||
.context("failed to generate candidate proxy port configuration")?;
|
||||
let candidate = latest_runtime_mapping().await?;
|
||||
let candidate = Arc::new(latest_runtime_mapping().await?);
|
||||
|
||||
let was_running = !matches!(*manager.get_running_mode(), RunningMode::NotRunning);
|
||||
let assessment = probe_proxy_ports(current.clone(), candidate.clone(), was_running).await?;
|
||||
let assessment = probe_proxy_ports(Arc::clone(¤t), Arc::clone(&candidate), was_running).await?;
|
||||
if let Some(outcome) = rejected_save_outcome(assessment)? {
|
||||
return Ok(outcome);
|
||||
}
|
||||
@@ -159,8 +160,8 @@ async fn latest_runtime_mapping() -> Result<serde_yaml_ng::Mapping> {
|
||||
}
|
||||
|
||||
async fn probe_proxy_ports(
|
||||
current: serde_yaml_ng::Mapping,
|
||||
candidate: serde_yaml_ng::Mapping,
|
||||
current: Arc<serde_yaml_ng::Mapping>,
|
||||
candidate: Arc<serde_yaml_ng::Mapping>,
|
||||
current_core_is_running: bool,
|
||||
) -> Result<ListenerProbeOutcome> {
|
||||
AsyncHandler::spawn_blocking(move || probe_proxy_port_change(¤t, &candidate, current_core_is_running))
|
||||
|
||||
Reference in New Issue
Block a user