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
+6 -3
View File
@@ -42,9 +42,12 @@ pub async fn notify_event<'a>(event: NotificationEvent<'a>) {
}
NotificationEvent::ClashModeChanged { mode } => {
let title = clash_verge_i18n::t!("notifications.clashModeChanged.title");
let body = clash_verge_i18n::t!("notifications.clashModeChanged.body")
.replace("{mode}", mode)
.into();
let body = clash_verge_i18n::t!("notifications.clashModeChanged.body");
let body = if body.contains("{mode}") {
body.replace("{mode}", mode).into()
} else {
body
};
notify(title, body);
}
NotificationEvent::SystemProxyToggled(enabled) => {
+5 -4
View File
@@ -1,5 +1,6 @@
use super::NoticeStatus;
use crate::{APP_HANDLE, singleton};
#[cfg(target_os = "macos")]
use smartstring::alias::String;
use std::sync::{
Arc,
@@ -55,21 +56,21 @@ impl Handle {
Self::send_event(FrontendEvent::RunStateChanged { state: state.clone() });
}
pub fn notify_profile_changed(profile_id: &String) {
pub fn notify_profile_changed(profile_id: &str) {
Self::send_event(FrontendEvent::ProfileChanged {
current_profile_id: profile_id,
});
}
pub fn notify_timer_updated(profile_index: &String) {
pub fn notify_timer_updated(profile_index: &str) {
Self::send_event(FrontendEvent::TimerUpdated { profile_index });
}
pub fn notify_profile_update_started(uid: &String) {
pub fn notify_profile_update_started(uid: &str) {
Self::send_event(FrontendEvent::ProfileUpdateStarted { uid });
}
pub fn notify_profile_update_completed(uid: &String) {
pub fn notify_profile_update_completed(uid: &str) {
Self::send_event(FrontendEvent::ProfileUpdateCompleted { uid });
}
+5 -5
View File
@@ -20,16 +20,16 @@ pub enum FrontendEvent<'a> {
message: Arc<str>,
},
ProfileChanged {
current_profile_id: &'a String,
current_profile_id: &'a str,
},
TimerUpdated {
profile_index: &'a String,
profile_index: &'a str,
},
ProfileUpdateStarted {
uid: &'a String,
uid: &'a str,
},
ProfileUpdateCompleted {
uid: &'a String,
uid: &'a str,
},
RunStateChanged {
state: crate::core::runstate::RunStateView,
@@ -369,7 +369,7 @@ mod wire_tests {
/// One sample per `FrontendEvent` variant, paired with its wire name and payload.
#[cfg(test)]
fn sample_frontend_events(uid: &String) -> Vec<(FrontendEvent<'_>, &'static str, serde_json::Value)> {
fn sample_frontend_events(uid: &str) -> Vec<(FrontendEvent<'_>, &'static str, serde_json::Value)> {
let state = crate::core::runstate::RunState {
health: Default::default(),
pending: None,
+1 -1
View File
@@ -122,7 +122,7 @@ fn allocate_provider_destination(destination: &str, url: &str, reserved: &mut Ha
digest.update(destination.as_bytes());
digest.update([0]);
digest.update(url.as_bytes());
let digest: String = digest.finalize().iter().map(|byte| format!("{byte:02x}")).collect();
let digest: String = crate::utils::help::hex_encode(&digest.finalize());
let path = Path::new(destination);
let extension = path
.extension()
+1 -1
View File
@@ -458,7 +458,7 @@ impl Timer {
}
}
fn emit_update_event(uid: &String, is_start: bool) {
fn emit_update_event(uid: &str, is_start: bool) {
if is_start {
super::handle::Handle::notify_profile_update_started(uid);
} else {
-3
View File
@@ -342,9 +342,6 @@ impl Tray {
let verge = Config::verge().await.data_arc();
let app_handle = handle::Handle::app_handle();
self.update_menu_internal(app_handle, false).await?;
AsyncHandler::spawn(|| async {
logging_error!(Type::Tray, Self::global().update_menu().await);
});
self.update_icon(&verge).await?;
#[cfg(target_os = "macos")]
self.update_speed_task(verge.enable_tray_speed.unwrap_or(false));
+1 -1
View File
@@ -435,7 +435,7 @@ impl CoreConfigValidator {
}
let check_path = dirs::app_home_dir()?.join(crate::constants::files::CHECK_CONFIG);
help::save_yaml_str(&check_path, yaml).await?;
help::save_yaml_str(&check_path, yaml, None).await?;
let outcome = Self::validate_config_internal_outcome(dirs::path_to_str(&check_path)?).await?;
if outcome.is_valid() {
*LAST_VALIDATED.lock() = Some(fingerprint);