refactor(enhance): lowercase takes a reference and chain support is copy

use_lowercase borrows the mapping and clones only the values; call
sites no longer clone whole configs. ChainSupport is a fieldless enum
and now Copy; builtin() registers each support pair without clones.
This commit is contained in:
Tunglies
2026-10-28 00:08:06 +08:00
parent 5780915d16
commit 64ac6001e4
5 changed files with 13 additions and 16 deletions
+5 -8
View File
@@ -22,7 +22,7 @@ pub enum ChainType {
Groups(SeqMap),
}
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Copy)]
pub enum ChainSupport {
Mihomo,
MihomoAlpha,
@@ -70,14 +70,11 @@ impl ChainItem {
/// 内建支持一些脚本
pub fn builtin() -> Vec<(ChainSupport, Self)> {
// data intentionally empty: the enhance loop dispatches on uid to native implementations
let hy_alpn = Self::to_script("verge_hy_alpn", "");
let meta_guard = Self::to_script("verge_meta_guard", "");
vec![
(ChainSupport::Mihomo, hy_alpn.clone()),
(ChainSupport::Mihomo, meta_guard.clone()),
(ChainSupport::MihomoAlpha, hy_alpn),
(ChainSupport::MihomoAlpha, meta_guard),
(ChainSupport::Mihomo, Self::to_script("verge_hy_alpn", "")),
(ChainSupport::Mihomo, Self::to_script("verge_meta_guard", "")),
(ChainSupport::MihomoAlpha, Self::to_script("verge_hy_alpn", "")),
(ChainSupport::MihomoAlpha, Self::to_script("verge_meta_guard", "")),
]
}
+3 -3
View File
@@ -24,12 +24,12 @@ fn lowercase_key(key: &str) -> Value {
Value::from(key.as_str())
}
pub fn use_lowercase(config: Mapping) -> Mapping {
pub fn use_lowercase(config: &Mapping) -> Mapping {
let mut lowercased = Mapping::new();
for (key, value) in config {
for (key, value) in config.iter() {
if let Some(key_str) = key.as_str() {
lowercased.insert(lowercase_key(key_str), value);
lowercased.insert(lowercase_key(key_str), value.clone());
}
}
lowercased
+1 -1
View File
@@ -17,7 +17,7 @@ fn deep_merge(a: &mut Value, b: Value) {
}
pub fn use_merge(merge: &Mapping, mut config: Mapping) -> Mapping {
for (key, value) in use_lowercase(merge.clone()) {
for (key, value) in use_lowercase(merge) {
match (config.get_mut(&key), value) {
(Some(Value::Mapping(existing)), Value::Mapping(overlay)) if key.as_str() == Some("dns") => {
existing.extend(overlay);
+1 -1
View File
@@ -614,7 +614,7 @@ fn apply_builtin_scripts(mut config: Mapping, clash_core: Option<String>, enable
.collect();
if !items.is_empty() {
// The JS path saw a lowercased view; lowercase once, only when a builtin runs.
config = use_lowercase(config);
config = use_lowercase(&config);
for item in items {
logging!(debug, Type::Core, "run builtin script {}", item.uid);
config = match item.uid.as_str() {
+3 -3
View File
@@ -22,10 +22,10 @@ type EvalOutcome = (Result<Mapping, std::string::String>, Vec<(String, String)>)
pub(super) async fn use_script(script: String, config: Mapping, name: String) -> ScriptResult {
// The template is identity-plus-lowercasing; skip the Boa round-trip.
if script == tmpl::ITEM_SCRIPT {
return (use_lowercase(config), vec![], vec![]);
return (use_lowercase(&config), vec![], vec![]);
}
let json = match serde_json::to_string(&use_lowercase(config.clone())) {
let json = match serde_json::to_string(&use_lowercase(&config)) {
Ok(json) if json.len() <= MAX_JSON_SIZE => json,
Ok(_) => {
let message = "Configuration size exceeds maximum allowed size";
@@ -173,7 +173,7 @@ fn eval_script(script: &str, config_json: &str, name: &String) -> EvalOutcome {
}
match parse_json_safely(&result) {
Ok(config) => (Ok(use_lowercase(config)), outputs.lock().to_vec()),
Ok(config) => (Ok(use_lowercase(&config)), outputs.lock().to_vec()),
Err(err) => {
logging!(
error,