diff --git a/UPDATELOG.md b/UPDATELOG.md index 52de283b1..cc8e27591 100644 --- a/UPDATELOG.md +++ b/UPDATELOG.md @@ -1,6 +1,8 @@ ## v2.4.1 -Something Should Be Done +### 🚀 性能优化 + +- 优化异步程序性能 ## v2.4.0 diff --git a/src-tauri/src/cmd/profile.rs b/src-tauri/src/cmd/profile.rs index 961bec884..a57b30b6a 100644 --- a/src-tauri/src/cmd/profile.rs +++ b/src-tauri/src/cmd/profile.rs @@ -2,7 +2,9 @@ use super::CmdResult; use crate::{ config::{Config, IProfiles, PrfItem, PrfOption}, core::{handle, timer::Timer, tray::Tray, CoreManager}, - feat, logging, ret_err, + feat, logging, + process::AsyncHandler, + ret_err, utils::{dirs, help, logging::Type}, wrap_err, }; @@ -37,7 +39,7 @@ pub async fn get_profiles() -> CmdResult { // 策略1: 尝试快速获取latest数据 let latest_result = tokio::time::timeout( Duration::from_millis(500), - tokio::task::spawn_blocking(move || { + AsyncHandler::spawn_blocking(move || { let profiles = Config::profiles(); let latest = profiles.latest_ref(); IProfiles { @@ -64,7 +66,7 @@ pub async fn get_profiles() -> CmdResult { // 策略2: 如果快速获取失败,尝试获取data() let data_result = tokio::time::timeout( Duration::from_secs(2), - tokio::task::spawn_blocking(move || { + AsyncHandler::spawn_blocking(move || { let profiles = Config::profiles(); let data = profiles.latest_ref(); IProfiles { @@ -102,7 +104,7 @@ pub async fn get_profiles() -> CmdResult { "所有获取配置策略都失败,尝试fallback" ); - match tokio::task::spawn_blocking(IProfiles::new).await { + match AsyncHandler::spawn_blocking(IProfiles::new).await { Ok(profiles) => { logging!(info, Type::Cmd, true, "使用fallback配置成功"); Ok(profiles) @@ -372,7 +374,7 @@ pub async fn patch_profiles_config(profiles: IProfiles) -> CmdResult { match file_read_result { Ok(Ok(content)) => { - let yaml_parse_result = tokio::task::spawn_blocking(move || { + let yaml_parse_result = AsyncHandler::spawn_blocking(move || { serde_yaml::from_str::(&content) }) .await; diff --git a/src-tauri/src/core/async_proxy_query.rs b/src-tauri/src/core/async_proxy_query.rs index 800dbe129..0b6a1e7bc 100644 --- a/src-tauri/src/core/async_proxy_query.rs +++ b/src-tauri/src/core/async_proxy_query.rs @@ -1,3 +1,5 @@ +#[cfg(any(target_os = "windows", target_os = "linux"))] +use crate::process::AsyncHandler; use anyhow::Result; use serde::{Deserialize, Serialize}; use tokio::time::{timeout, Duration}; @@ -74,7 +76,7 @@ impl AsyncProxyQuery { #[cfg(target_os = "windows")] async fn get_auto_proxy_impl() -> Result { // Windows: 从注册表读取PAC配置 - tokio::task::spawn_blocking(move || -> Result { + AsyncHandler::spawn_blocking(move || -> Result { Self::get_pac_config_from_registry() }) .await? @@ -258,7 +260,7 @@ impl AsyncProxyQuery { #[cfg(target_os = "windows")] async fn get_system_proxy_impl() -> Result { // Windows: 使用注册表直接读取代理设置 - tokio::task::spawn_blocking(move || -> Result { + AsyncHandler::spawn_blocking(move || -> Result { Self::get_system_proxy_from_registry() }) .await? diff --git a/src-tauri/src/core/core.rs b/src-tauri/src/core/core.rs index c32a50e99..05d371d8a 100644 --- a/src-tauri/src/core/core.rs +++ b/src-tauri/src/core/core.rs @@ -533,7 +533,7 @@ impl CoreManager { use winapi::um::winnt::HANDLE; let process_name_clone = process_name.clone(); - let pids = tokio::task::spawn_blocking(move || -> Result> { + let pids = AsyncHandler::spawn_blocking(move || -> Result> { let mut pids = Vec::new(); unsafe { @@ -628,7 +628,7 @@ impl CoreManager { use winapi::um::processthreadsapi::{OpenProcess, TerminateProcess}; use winapi::um::winnt::{HANDLE, PROCESS_TERMINATE}; - tokio::task::spawn_blocking(move || -> bool { + AsyncHandler::spawn_blocking(move || -> bool { unsafe { let process_handle: HANDLE = OpenProcess(PROCESS_TERMINATE, 0, pid); if process_handle.is_null() { @@ -703,7 +703,7 @@ impl CoreManager { use winapi::um::processthreadsapi::OpenProcess; use winapi::um::winnt::{HANDLE, PROCESS_QUERY_INFORMATION}; - let result = tokio::task::spawn_blocking(move || -> Result { + let result = AsyncHandler::spawn_blocking(move || -> Result { unsafe { let process_handle: HANDLE = OpenProcess(PROCESS_QUERY_INFORMATION, 0, pid); if process_handle.is_null() { diff --git a/src-tauri/src/core/service_ipc.rs b/src-tauri/src/core/service_ipc.rs index d14d8484a..a5ce5bdf6 100644 --- a/src-tauri/src/core/service_ipc.rs +++ b/src-tauri/src/core/service_ipc.rs @@ -1,3 +1,5 @@ +#[cfg(target_os = "windows")] +use crate::process::AsyncHandler; use crate::{logging, utils::logging::Type}; use anyhow::{bail, Context, Result}; use hmac::{Hmac, Mac}; @@ -143,7 +145,7 @@ pub async fn send_ipc_request( let request_json = serde_json::to_string(&request)?; - let result = tokio::task::spawn_blocking(move || -> Result { + let result = AsyncHandler::spawn_blocking(move || -> Result { let c_pipe_name = match CString::new(IPC_SOCKET_NAME) { Ok(name) => name, Err(e) => { diff --git a/src-tauri/src/process/async_handler.rs b/src-tauri/src/process/async_handler.rs index c5a809d4e..a8bad052f 100644 --- a/src-tauri/src/process/async_handler.rs +++ b/src-tauri/src/process/async_handler.rs @@ -11,4 +11,12 @@ impl AsyncHandler { { async_runtime::spawn(f()) } + + pub fn spawn_blocking(f: F) -> JoinHandle + where + F: FnOnce() -> T + Send + 'static, + T: Send + 'static, + { + async_runtime::spawn_blocking(f) + } }