perf: change patch_config parameter from Mapping to &Mapping for efficiency

This commit is contained in:
Tunglies
2025-11-30 20:44:21 +08:00
parent ae8c302bf4
commit 6897ead070
5 changed files with 19 additions and 20 deletions

View File

@@ -22,7 +22,7 @@ impl IRuntime {
// 这里只更改 allow-lan | ipv6 | log-level | tun // 这里只更改 allow-lan | ipv6 | log-level | tun
#[inline] #[inline]
pub fn patch_config(&mut self, patch: Mapping) { pub fn patch_config(&mut self, patch: &Mapping) {
let config = if let Some(config) = self.config.as_mut() { let config = if let Some(config) = self.config.as_mut() {
config config
} else { } else {

View File

@@ -29,7 +29,7 @@ pub async fn get_clash_info() -> CmdResult<ClashInfo> {
/// 修改Clash配置 /// 修改Clash配置
#[tauri::command] #[tauri::command]
pub async fn patch_clash_config(payload: Mapping) -> CmdResult { pub async fn patch_clash_config(payload: Mapping) -> CmdResult {
feat::patch_clash(payload).await.stringify_err() feat::patch_clash(&payload).await.stringify_err()
} }
/// 修改Clash模式 /// 修改Clash模式
@@ -171,7 +171,7 @@ pub async fn apply_dns_config(apply: bool) -> CmdResult {
// 应用DNS配置到运行时配置 // 应用DNS配置到运行时配置
Config::runtime().await.edit_draft(|d| { Config::runtime().await.edit_draft(|d| {
d.patch_config(patch); d.patch_config(&patch);
}); });
// 重新生成配置 // 重新生成配置

View File

@@ -16,7 +16,6 @@ pub struct IClashTemp(pub Mapping);
impl IClashTemp { impl IClashTemp {
pub async fn new() -> Self { pub async fn new() -> Self {
let template = Self::template();
let clash_path_result = dirs::clash_path(); let clash_path_result = dirs::clash_path();
let map_result = if let Ok(path) = clash_path_result { let map_result = if let Ok(path) = clash_path_result {
help::read_mapping(&path).await help::read_mapping(&path).await
@@ -26,24 +25,26 @@ impl IClashTemp {
match map_result { match map_result {
Ok(mut map) => { Ok(mut map) => {
template.0.keys().for_each(|key| { let template_map = Self::template().0;
if !map.contains_key(key) for (key, value) in template_map.into_iter() {
&& let Some(value) = template.0.get(key) if !map.contains_key(&key) {
{ map.insert(key, value);
map.insert(key.clone(), value.clone());
} }
}); }
// 确保 secret 字段存在且不为空 // 确保 secret 字段存在且不为空
if let Some(Value::String(s)) = map.get_mut("secret") if let Some(val) = map.get_mut("secret")
&& let Value::String(s) = val
&& s.is_empty() && s.is_empty()
{ {
*s = "set-your-secret".into(); *s = "set-your-secret".into();
} }
Self(Self::guard(map)) Self(Self::guard(map))
} }
Err(err) => { Err(err) => {
logging!(error, Type::Config, "{err}"); logging!(error, Type::Config, "{err}");
template Self::template()
} }
} }
} }
@@ -144,9 +145,9 @@ impl IClashTemp {
config config
} }
pub fn patch_config(&mut self, patch: Mapping) { pub fn patch_config(&mut self, patch: &Mapping) {
for (key, value) in patch.into_iter() { for (key, value) in patch.iter() {
self.0.insert(key, value); self.0.insert(key.to_owned(), value.to_owned());
} }
} }

View File

@@ -84,7 +84,7 @@ pub async fn change_clash_mode(mode: String) {
// 更新订阅 // 更新订阅
Config::clash() Config::clash()
.await .await
.edit_draft(|d| d.patch_config(mapping)); .edit_draft(|d| d.patch_config(&mapping));
// 分离数据获取和异步调用 // 分离数据获取和异步调用
let clash_data = Config::clash().await.data_arc(); let clash_data = Config::clash().await.data_arc();

View File

@@ -9,10 +9,8 @@ use clash_verge_logging::{Type, logging, logging_error};
use serde_yaml_ng::Mapping; use serde_yaml_ng::Mapping;
/// Patch Clash configuration /// Patch Clash configuration
pub async fn patch_clash(patch: Mapping) -> Result<()> { pub async fn patch_clash(patch: &Mapping) -> Result<()> {
Config::clash() Config::clash().await.edit_draft(|d| d.patch_config(patch));
.await
.edit_draft(|d| d.patch_config(patch.clone()));
let res = { let res = {
// 激活订阅 // 激活订阅