mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-22 12:22:07 +08:00
Every settings toggle reparsed the profile and chain files, ran four Boa interpreters, serialized the runtime config twice, and re-ran mihomo -t over bytes the core had just accepted. Builtin and template scripts now run without Boa, custom scripts bind the config through JSON.parse and take ownership instead of per-script clones, the config serializes once, and mihomo -t is skipped unless the bytes, core build, or geo assets changed. Heavy parses and serializations moved to spawn_blocking. Large-subscription toggles drop from hundreds of milliseconds to ~140ms with no new retained state.
72 lines
1.6 KiB
Rust
72 lines
1.6 KiB
Rust
use clash_verge_logging::{Type, logging};
|
|
|
|
use super::field::use_lowercase;
|
|
use serde_yaml_ng::{self, Mapping, Value};
|
|
|
|
fn deep_merge(a: &mut Value, b: Value) {
|
|
match (a, b) {
|
|
(Value::Mapping(a_map), Value::Mapping(b_map)) => {
|
|
for (key, value) in b_map {
|
|
if let Some(existing) = a_map.get_mut(&key) {
|
|
deep_merge(existing, value);
|
|
} else {
|
|
a_map.insert(key, value);
|
|
}
|
|
}
|
|
}
|
|
(a, b) => *a = b,
|
|
}
|
|
}
|
|
|
|
pub fn use_merge(merge: &Mapping, config: Mapping) -> Mapping {
|
|
let mut config = Value::from(config);
|
|
let merge = use_lowercase(merge);
|
|
|
|
deep_merge(&mut config, Value::from(merge));
|
|
|
|
config.as_mapping().cloned().unwrap_or_else(|| {
|
|
logging!(
|
|
error,
|
|
Type::Core,
|
|
"Failed to convert merged config to mapping, using empty mapping"
|
|
);
|
|
Mapping::new()
|
|
})
|
|
}
|
|
|
|
#[test]
|
|
fn test_merge() -> anyhow::Result<()> {
|
|
let merge = r"
|
|
prepend-rules:
|
|
- prepend
|
|
- 1123123
|
|
append-rules:
|
|
- append
|
|
prepend-proxies:
|
|
- 9999
|
|
append-proxies:
|
|
- 1111
|
|
rules:
|
|
- replace
|
|
proxy-groups:
|
|
- 123781923810
|
|
tun:
|
|
enable: true
|
|
dns:
|
|
enable: true
|
|
";
|
|
|
|
let config = r"
|
|
rules:
|
|
- aaaaa
|
|
script1: test
|
|
";
|
|
|
|
let merge = serde_yaml_ng::from_str::<Mapping>(merge)?;
|
|
let config = serde_yaml_ng::from_str::<Mapping>(config)?;
|
|
|
|
let _ = serde_yaml_ng::to_string(&use_merge(&merge, config))?;
|
|
|
|
Ok(())
|
|
}
|