perf(backend): cut redundant copies and rebuilds in enhance, config and tray paths (#8666)

* fix: mechanical string conversions

- &String parameters become &str (event payload fields borrow &str; wire format unchanged)
- Cow/std-string double conversions collapse to a single Into
- sha256 digests format through a shared hex lookup table instead of one
  format! allocation per byte
- i18n placeholder replacement only allocates when the placeholder exists

* perf(enhance): marshal scripts by value and diff fields by fingerprint

- scripts cross into boa via JsValue::from_json/to_json instead of the
  JSON text round-trip (config clone + json string + UTF-16 JsString +
  stringify + re-parse all disappear); size guards use an allocation-free
  approximate walk
- merge documents and tun/dns patches move values instead of deep-cloning
  whole sub-trees per pass
- authoritative-field snapshots hash dns/hosts/tun values instead of
  cloning them around every chain item (9 snapshots per enhance)
- proxy-name cleanup sets build from &str in one conversion

* perf(config): write yaml headers in place and record selections without copying

- save_yaml_str takes an optional header prefix written as separate chunks,
  so the MB-scale serialized payload is never duplicated by format! just
  to prepend a comment (profiles/verge/clash/runtime yaml all benefit;
  file bytes unchanged)
- the runtime yaml string no longer round-trips through a prefixed copy;
  write_runtime_file adds the header when persisting
- record/forget selected nodes mutate the stored selection list in place
  instead of cloning it per click

* perf(tray): drop the redundant full rebuild in update_part

update_part rebuilt the menu twice back to back: once without proxy groups,
then a spawned full update_menu. Keep the partial rebuild only.
This commit is contained in:
Tunglies
2026-11-17 02:17:42 +00:00
committed by GitHub
parent d7ac6b5bca
commit 6a202e8d00
21 changed files with 238 additions and 165 deletions
+12 -8
View File
@@ -605,7 +605,7 @@ async fn fetch_proxies_with_timeout() -> Result<Proxies> {
.map_err(Into::into)
}
async fn select_node_with_timeout(group_name: &String, node: &String) -> Result<()> {
async fn select_node_with_timeout(group_name: &str, node: &str) -> Result<()> {
tokio::time::timeout(MIHOMO_OPERATION_TIMEOUT, async {
handle::Handle::mihomo().select_node_for_group(group_name, node).await
})
@@ -761,7 +761,7 @@ pub(crate) async fn record_selected_node(group_name: &str, node: &str) -> Result
return Ok((profiles, false));
};
let mut selected = item.selected.clone().unwrap_or_default();
let selected = item.selected.get_or_insert_with(Vec::new);
match selected
.iter_mut()
.find(|entry| entry.name.as_ref() == Some(&group_name))
@@ -770,14 +770,13 @@ pub(crate) async fn record_selected_node(group_name: &str, node: &str) -> Result
if entry.now.as_ref() == Some(&node) {
return Ok((profiles, false));
}
entry.now = Some(node);
entry.now = Some(node.clone());
}
None => selected.push(PrfSelected {
name: Some(group_name),
now: Some(node),
}),
}
item.selected = Some(selected);
profiles.save_file().await?;
Ok((profiles, true))
})
@@ -796,12 +795,17 @@ pub(crate) async fn forget_selected_node(group_name: &str) -> Result<()> {
return Ok((profiles, false));
};
let mut selected = item.selected.clone().unwrap_or_default();
let Some(mut selected) = item.selected.take() else {
return Ok((profiles, false));
};
if !remove_selected_node(&mut selected, group_name) {
item.selected = Some(selected);
return Ok((profiles, false));
}
item.selected = (!selected.is_empty()).then_some(selected);
if !selected.is_empty() {
item.selected = Some(selected);
}
profiles.save_file().await?;
Ok((profiles, true))
})
@@ -809,7 +813,7 @@ pub(crate) async fn forget_selected_node(group_name: &str) -> Result<()> {
}
async fn persist_reconciled_selected(
profile_uid: &String,
profile_uid: &str,
original_selected: &[PrfSelected],
selected: Vec<PrfSelected>,
generation: u64,
@@ -818,7 +822,7 @@ async fn persist_reconciled_selected(
return Ok(());
}
let profile_uid = profile_uid.clone();
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) {