mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-29 17:15:38 +08:00
perf: utilize smartstring for string handling (#5149)
* perf: utilize smartstring for string handling - Updated various modules to replace standard String with smartstring::alias::String for improved performance and memory efficiency. - Adjusted string manipulations and conversions throughout the codebase to ensure compatibility with the new smartstring type. - Enhanced readability and maintainability by using `.into()` for conversions where applicable. - Ensured that all instances of string handling in configuration, logging, and network management leverage the benefits of smartstring. * fix: replace wrap_err with stringify_err for better error handling in UWP tool invocation * refactor: update import path for StringifyErr and adjust string handling in sysopt * fix: correct import path for CmdResult in UWP module * fix: update argument type for execute_sysproxy_command to use std::string::String * fix: add missing CmdResult import in UWP platform module * fix: improve string handling and error messaging across multiple files * style: format code for improved readability and consistency across multiple files * fix: remove unused file
This commit is contained in:
@@ -4,6 +4,7 @@ use crate::{
|
||||
utils::{dirs, help},
|
||||
};
|
||||
use serde_yaml_ng::Mapping;
|
||||
use smartstring::alias::String;
|
||||
use std::fs;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -73,7 +74,7 @@ impl AsyncChainItemFrom for Option<ChainItem> {
|
||||
let itype = item.itype.as_ref()?.as_str();
|
||||
let file = item.file.clone()?;
|
||||
let uid = item.uid.clone().unwrap_or("".into());
|
||||
let path = dirs::app_profiles_dir().ok()?.join(file);
|
||||
let path = dirs::app_profiles_dir().ok()?.join(file.as_str());
|
||||
|
||||
if !path.exists() {
|
||||
return None;
|
||||
@@ -82,7 +83,7 @@ impl AsyncChainItemFrom for Option<ChainItem> {
|
||||
match itype {
|
||||
"script" => Some(ChainItem {
|
||||
uid,
|
||||
data: ChainType::Script(fs::read_to_string(path).ok()?),
|
||||
data: ChainType::Script(fs::read_to_string(path).ok()?.into()),
|
||||
}),
|
||||
"merge" => Some(ChainItem {
|
||||
uid,
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use serde_yaml_ng::{Mapping, Value};
|
||||
use smartstring::alias::String;
|
||||
use std::collections::HashSet;
|
||||
|
||||
pub const HANDLE_FIELDS: [&str; 12] = [
|
||||
@@ -31,7 +32,7 @@ pub fn use_lowercase(config: Mapping) -> Mapping {
|
||||
if let Some(key_str) = key.as_str() {
|
||||
let mut key_str = String::from(key_str);
|
||||
key_str.make_ascii_lowercase();
|
||||
ret.insert(Value::from(key_str), value);
|
||||
ret.insert(Value::from(key_str.as_str()), value);
|
||||
}
|
||||
}
|
||||
ret
|
||||
@@ -70,8 +71,8 @@ pub fn use_keys(config: &Mapping) -> Vec<String> {
|
||||
config
|
||||
.iter()
|
||||
.filter_map(|(key, _)| key.as_str())
|
||||
.map(|s| {
|
||||
let mut s = s.to_string();
|
||||
.map(|s: &str| {
|
||||
let mut s: String = s.into();
|
||||
s.make_ascii_lowercase();
|
||||
s
|
||||
})
|
||||
|
||||
@@ -8,6 +8,7 @@ mod tun;
|
||||
use self::{chain::*, field::*, merge::*, script::*, seq::*, tun::*};
|
||||
use crate::{config::Config, utils::tmpl};
|
||||
use serde_yaml_ng::Mapping;
|
||||
use smartstring::alias::String;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
type ResultLog = Vec<(String, String)>;
|
||||
@@ -192,7 +193,7 @@ pub async fn enhance() -> (Mapping, Vec<String>, HashMap<String, ResultLog>) {
|
||||
let item = {
|
||||
let profiles = Config::profiles().await;
|
||||
let profiles = profiles.latest_ref();
|
||||
profiles.get_item(&"Merge".to_string()).ok().cloned()
|
||||
profiles.get_item(&"Merge".into()).ok().cloned()
|
||||
};
|
||||
if let Some(item) = item {
|
||||
<Option<ChainItem>>::from_async(&item).await
|
||||
@@ -209,7 +210,7 @@ pub async fn enhance() -> (Mapping, Vec<String>, HashMap<String, ResultLog>) {
|
||||
let item = {
|
||||
let profiles = Config::profiles().await;
|
||||
let profiles = profiles.latest_ref();
|
||||
profiles.get_item(&"Script".to_string()).ok().cloned()
|
||||
profiles.get_item(&"Script".into()).ok().cloned()
|
||||
};
|
||||
if let Some(item) = item {
|
||||
<Option<ChainItem>>::from_async(&item).await
|
||||
@@ -253,7 +254,7 @@ pub async fn enhance() -> (Mapping, Vec<String>, HashMap<String, ResultLog>) {
|
||||
config = res_config;
|
||||
logs.extend(res_logs);
|
||||
}
|
||||
Err(err) => logs.push(("exception".into(), err.to_string())),
|
||||
Err(err) => logs.push(("exception".into(), err.to_string().into())),
|
||||
}
|
||||
|
||||
result_map.insert(global_script.uid, logs);
|
||||
@@ -280,13 +281,13 @@ pub async fn enhance() -> (Mapping, Vec<String>, HashMap<String, ResultLog>) {
|
||||
if let ChainType::Script(script) = script_item.data {
|
||||
let mut logs = vec![];
|
||||
|
||||
match use_script(script, config.to_owned(), profile_name.to_owned()) {
|
||||
match use_script(script, config.to_owned(), profile_name) {
|
||||
Ok((res_config, res_logs)) => {
|
||||
exists_keys.extend(use_keys(&res_config));
|
||||
config = res_config;
|
||||
logs.extend(res_logs);
|
||||
}
|
||||
Err(err) => logs.push(("exception".into(), err.to_string())),
|
||||
Err(err) => logs.push(("exception".into(), err.to_string().into())),
|
||||
}
|
||||
|
||||
result_map.insert(script_item.uid, logs);
|
||||
@@ -361,7 +362,7 @@ pub async fn enhance() -> (Mapping, Vec<String>, HashMap<String, ResultLog>) {
|
||||
.for_each(|item| {
|
||||
log::debug!(target: "app", "run builtin script {}", item.uid);
|
||||
if let ChainType::Script(script) = item.data {
|
||||
match use_script(script, config.to_owned(), "".to_string()) {
|
||||
match use_script(script, config.to_owned(), "".into()) {
|
||||
Ok((res_config, _)) => {
|
||||
config = res_config;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use super::use_lowercase;
|
||||
use anyhow::{Error, Result};
|
||||
use serde_yaml_ng::Mapping;
|
||||
use smartstring::alias::String;
|
||||
|
||||
pub fn use_script(
|
||||
script: String,
|
||||
@@ -44,7 +45,7 @@ pub fn use_script(
|
||||
)
|
||||
})?;
|
||||
let mut out = copy_outputs.borrow_mut();
|
||||
out.push((level, data));
|
||||
out.push((level.into(), data.into()));
|
||||
Ok(JsValue::undefined())
|
||||
},
|
||||
),
|
||||
@@ -94,7 +95,7 @@ pub fn use_script(
|
||||
match res {
|
||||
Ok(config) => Ok((use_lowercase(config), out.to_vec())),
|
||||
Err(err) => {
|
||||
out.push(("exception".into(), err.to_string()));
|
||||
out.push(("exception".into(), err.to_string().into()));
|
||||
Ok((config, out.to_vec()))
|
||||
}
|
||||
}
|
||||
@@ -121,7 +122,7 @@ fn strip_outer_quotes(s: &str) -> &str {
|
||||
|
||||
// 转义单引号和反斜杠,用于单引号包裹的JavaScript字符串
|
||||
fn escape_js_string_for_single_quote(s: &str) -> String {
|
||||
s.replace('\\', "\\\\").replace('\'', "\\'")
|
||||
s.replace('\\', "\\\\").replace('\'', "\\'").into()
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -150,7 +151,7 @@ fn test_script() {
|
||||
";
|
||||
|
||||
let config = serde_yaml_ng::from_str(config).expect("Failed to parse test config YAML");
|
||||
let (config, results) = use_script(script.into(), config, "".to_string())
|
||||
let (config, results) = use_script(script.into(), config, "".into())
|
||||
.expect("Script execution should succeed in test");
|
||||
|
||||
let _ = serde_yaml_ng::to_string(&config).expect("Failed to serialize config to YAML");
|
||||
|
||||
Reference in New Issue
Block a user