perf(enhance): cut fixed config-generation cost

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.
This commit is contained in:
Tunglies
2026-09-10 06:32:51 +08:00
parent 92624108c9
commit 8812923eda
13 changed files with 269 additions and 239 deletions
+29 -22
View File
@@ -147,12 +147,16 @@ impl Config {
}
logging!(debug, Type::Config, "生成运行时配置成功");
let config_result = Self::generate_file(ConfigType::Run).await;
// Run file first: startup rewrites it via use_default_config when validation fails.
let config_result = match Self::runtime_config_yaml().await {
Ok(yaml) => Self::write_runtime_file(&yaml).await.map(|_| yaml),
Err(error) => Err(error),
};
if config_result.is_ok() {
if let Ok(yaml) = &config_result {
logging!(debug, Type::Config, "开始验证配置");
match CoreConfigValidator::global().validate_config_outcome().await {
match CoreConfigValidator::global().validate_config_outcome_with(yaml).await {
Ok(outcome) if outcome.is_valid() => {
logging!(debug, Type::Config, "配置验证成功");
Ok(None)
@@ -188,22 +192,31 @@ impl Config {
}
}
pub async fn generate_file(typ: ConfigType) -> Result<PathBuf> {
let path = match typ {
ConfigType::Run => dirs::app_home_dir()?.join(files::RUNTIME_CONFIG),
ConfigType::Check => dirs::app_home_dir()?.join(files::CHECK_CONFIG),
};
pub async fn generate_file() -> Result<PathBuf> {
let yaml = Self::runtime_config_yaml().await?;
Self::write_runtime_file(&yaml).await
}
pub(crate) async fn runtime_config_yaml() -> Result<std::string::String> {
let runtime = Self::runtime().await;
let runtime_lastest = runtime.latest_arc();
let runtime_data = runtime.data_arc();
let config = runtime_lastest
.config
.as_ref()
.or_else(|| runtime_data.config.as_ref())
.ok_or_else(|| anyhow!("failed to generate runtime config, might need to restart application"))?;
AsyncHandler::spawn_blocking(move || {
let runtime_lastest = runtime.latest_arc();
let runtime_data = runtime.data_arc();
let config = runtime_lastest
.config
.as_ref()
.or_else(|| runtime_data.config.as_ref())
.ok_or_else(|| anyhow!("failed to generate runtime config, might need to restart application"))?;
let yaml_str = serde_yaml_ng::to_string(config)?;
Ok(format!("# Generated by Clash Verge\n\n{}", yaml_str))
})
.await
.map_err(|join| anyhow!("runtime serialization task failed: {join}"))?
}
help::save_yaml(&path, config, Some("# Generated by Clash Verge")).await?;
pub(crate) async fn write_runtime_file(yaml: &str) -> Result<PathBuf> {
let path = dirs::app_home_dir()?.join(files::RUNTIME_CONFIG);
help::save_yaml_str(&path, yaml).await?;
Ok(path)
}
@@ -346,9 +359,3 @@ fn collect_names(config: &Mapping, list_key: &str, out: &mut HashSet<String>) {
}
}
}
#[derive(Debug)]
pub(crate) enum ConfigType {
Run,
Check,
}
+1 -1
View File
@@ -11,7 +11,7 @@ pub mod runtime;
pub(crate) mod snapshot;
mod verge;
pub(crate) use self::config::{Config, ConfigType};
pub(crate) use self::config::Config;
pub use self::{clash::*, encrypt::*, mixed_port::*, prfitem::*, profiles::*, verge::*};
pub const DEFAULT_PAC: &str = r#"function FindProxyForURL(url, host) {
+6 -3
View File
@@ -1,4 +1,4 @@
use super::{Config, ConfigType, IClashTemp, IVerge, MixedPort};
use super::{Config, IClashTemp, IVerge, MixedPort};
use crate::{
constants::timing,
core::{
@@ -129,15 +129,18 @@ impl Config {
.await
.context("failed to materialize runtime configuration with fallback port")?;
let yaml = Self::runtime_config_yaml()
.await
.context("failed to validate runtime configuration with fallback port")?;
let validation = CoreConfigValidator::global()
.validate_config_outcome()
.validate_config_outcome_with(&yaml)
.await
.context("failed to validate runtime configuration with fallback port")?;
if !validation.is_valid() {
bail!("runtime configuration with fallback port is invalid: {validation}");
}
Self::generate_file(ConfigType::Run)
Self::generate_file()
.await
.context("failed to write Runtime Configuration")?;
Ok(())