mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-18 18:02:08 +08:00
* perf(enhance): cache chain items by file identity Every config generation re-read and re-parsed all seven chain files. Cache the parsed ChainItem keyed by (path, mtime, length) and clone on hit: measured 486us -> 20us per generation (-95.9%) on a seven-file chain set (merge/script/rules fixtures). Profile saves go through tmp+rename so mtime always changes. * perf(config): guard runtime yaml re-serialization runtime_config_yaml re-serialized the multi-megabyte runtime Mapping on every call even when unchanged. Hash the Mapping tree (Value: Hash) and return the cached Arc<String> handle on hit: 4.10ms -> ~0.39ms (hash 290us, ~-90%) for a 3000-node config on the unchanged path, and the handle keeps both hit and miss at a refcount clone instead of copying the yaml body. The 64-bit SipHash key is sound here: a birthday collision needs ~2^32 distinct config states at a volume of a few hundred generations per day, and a colliding pair only serves one stale yaml until the next config change re-hashes. * build(deps): bump tauri-plugin-mihomo to 0.7.1 Pins the revision that derives Clone on ProxyType and DelayHistory, which the upcoming proxy-view cache needs to return cached views without changing the IPC contract. * refactor(backend): right-size the new caches' storage The chain cache never shares its stored ChainItem -- every hit deep-clones it -- so its Arc wrapper only added indirection; store it directly and const-shape it behind a LazyLock static instead of a OnceLock accessor. The yaml cache keeps Arc<String> because callers receive the stored handle, making hit and miss clones O(1). * style(config): hoist the runtime yaml cache to module scope Match the module-level placement of the chain cache and the other named statics; the closure-local declaration shadowed nothing but read worse and forced rustfmt to split the line.
91 lines
2.5 KiB
Rust
91 lines
2.5 KiB
Rust
use super::SeqMap;
|
|
use crate::{
|
|
config::PrfItem,
|
|
utils::{dirs, help},
|
|
};
|
|
use parking_lot::Mutex;
|
|
use serde_yaml_ng::Mapping;
|
|
use std::collections::HashMap;
|
|
use std::path::PathBuf;
|
|
use std::sync::{Arc, LazyLock};
|
|
use std::time::SystemTime;
|
|
use tokio::fs;
|
|
use tristr::alias::String;
|
|
|
|
#[derive(Debug)]
|
|
pub struct ChainItem {
|
|
pub uid: String,
|
|
pub data: ChainType,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub enum ChainType {
|
|
Merge(Mapping),
|
|
Script(String),
|
|
Rules(SeqMap),
|
|
Proxies(SeqMap),
|
|
Groups(SeqMap),
|
|
}
|
|
|
|
struct CachedChain {
|
|
mtime: SystemTime,
|
|
len: u64,
|
|
item: Arc<ChainItem>,
|
|
}
|
|
|
|
static CHAIN_CACHE: LazyLock<Mutex<HashMap<PathBuf, CachedChain>>> = LazyLock::new(|| Mutex::new(HashMap::new()));
|
|
|
|
// Helper trait to allow async conversion
|
|
pub trait AsyncChainItemFrom {
|
|
async fn from_async(item: &PrfItem) -> Option<Arc<ChainItem>>;
|
|
}
|
|
|
|
impl AsyncChainItemFrom for Option<Arc<ChainItem>> {
|
|
async fn from_async(item: &PrfItem) -> Self {
|
|
let itype = item.itype.as_ref()?.as_str();
|
|
let file = item.file.clone()?;
|
|
let uid = item.uid.clone().unwrap_or_else(|| "".into());
|
|
let path = dirs::app_profiles_dir().ok()?.join(file.as_str());
|
|
|
|
let meta = fs::metadata(&path).await.ok()?;
|
|
let (mtime, len) = (meta.modified().ok()?, meta.len());
|
|
if let Some(cached) = CHAIN_CACHE.lock().get(&path)
|
|
&& cached.mtime == mtime
|
|
&& cached.len == len
|
|
{
|
|
return Some(Arc::clone(&cached.item));
|
|
}
|
|
|
|
let loaded = Arc::new(match itype {
|
|
"script" => Some(ChainItem {
|
|
uid,
|
|
data: ChainType::Script(fs::read_to_string(&path).await.ok()?.as_str().into()),
|
|
}),
|
|
"merge" => Some(ChainItem {
|
|
uid,
|
|
data: ChainType::Merge(help::read_mapping(&path).await.ok()?),
|
|
}),
|
|
"rules" | "proxies" | "groups" => {
|
|
let seq_map = help::read_yaml::<SeqMap>(&path).await.ok()?;
|
|
let data = match itype {
|
|
"rules" => ChainType::Rules(seq_map),
|
|
"proxies" => ChainType::Proxies(seq_map),
|
|
_ => ChainType::Groups(seq_map),
|
|
};
|
|
Some(ChainItem { uid, data })
|
|
}
|
|
_ => None,
|
|
}?);
|
|
|
|
CHAIN_CACHE.lock().insert(
|
|
path,
|
|
CachedChain {
|
|
mtime,
|
|
len,
|
|
item: Arc::clone(&loaded),
|
|
},
|
|
);
|
|
Some(loaded)
|
|
}
|
|
}
|