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
-1
View File
@@ -25,7 +25,6 @@ pub async fn patch_clash(patch: &Mapping) -> Result<()> {
if patch.get("mode").is_some() {
tray::Tray::global().update_menu_and_icon().await;
}
Config::runtime().await.edit_draft(|d| d.patch_config(patch));
CoreManager::global().update_config_checked().await?;
}
handle::Handle::refresh_clash();
+7 -4
View File
@@ -1,6 +1,6 @@
use crate::{
config::{
Config, ConfigType, MixedPort,
Config, MixedPort,
snapshot::{FileSnapshot, capture_config_files, restore_files},
},
core::{
@@ -71,8 +71,11 @@ pub async fn save_proxy_ports(settings: ProxyPortSettings) -> Result<SaveProxyPo
return Ok(outcome);
}
let yaml = Config::runtime_config_yaml()
.await
.context("failed to serialize candidate proxy port configuration")?;
let validation = CoreConfigValidator::global()
.validate_config_outcome()
.validate_config_outcome_with(&yaml)
.await
.context("failed to validate candidate proxy port configuration")?;
if !validation.is_valid() {
@@ -81,7 +84,7 @@ pub async fn save_proxy_ports(settings: ProxyPortSettings) -> Result<SaveProxyPo
let snapshots = capture_config_files().await?;
// Once files change, failures must restore both files and drafts.
if let Err(error) = Config::generate_file(ConfigType::Run).await {
if let Err(error) = Config::write_runtime_file(&yaml).await {
transaction.rollback();
return match restore_files(&snapshots).await {
Ok(()) => Err(error).context("failed to persist candidate Runtime Configuration"),
@@ -232,7 +235,7 @@ async fn rollback_proxy_ports(snapshots: &[FileSnapshot], was_running: bool) ->
discard_proxy_port_drafts().await;
let file_result = restore_files(snapshots).await;
if file_result.is_err() {
let _ = Config::generate_file(ConfigType::Run).await;
let _ = Config::generate_file().await;
}
let lifecycle_result = if was_running {