From f1076e5ede3d258995207aaa14d329036e5e94a0 Mon Sep 17 00:00:00 2001 From: uye <99072975+ABA2396@users.noreply.github.com> Date: Sat, 27 Jun 2026 18:25:25 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E7=BC=93=E5=AD=98=20ConfigManager=20?= =?UTF-8?q?=E5=8F=8D=E5=B0=84=E5=B1=9E=E6=80=A7=E5=85=83=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=B9=B6=E6=B7=BB=E5=8A=A0=E7=BA=BF=E7=A8=8B=E5=AE=89=E5=85=A8?= =?UTF-8?q?=E4=BF=9D=E6=8A=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ConfigManager.cs | 122 +++++++++++++++++++++++++------------------ 1 file changed, 70 insertions(+), 52 deletions(-) diff --git a/src/ConfigManager.cs b/src/ConfigManager.cs index 3e07275..a0d23fb 100644 --- a/src/ConfigManager.cs +++ b/src/ConfigManager.cs @@ -1,7 +1,6 @@ using Microsoft.Win32; -using System; -using System.Collections.Generic; -using System.Linq; +using System.Collections.Concurrent; +using System.Reflection; namespace BASpark { @@ -96,6 +95,12 @@ public static class ConfigManager private static List _profiles = new List(); + // 缓存属性元数据,避免 Save() 每次都反射查找 + private static readonly ConcurrentDictionary _propertyCache = new(); + + // 保护并发 Save / _profiles 访问 + private static readonly object _syncLock = new(); + public static void Load() { try @@ -229,20 +234,29 @@ public static void GetAnimationSpeedsForOverlay(out double trailSpeed, out doubl } } - public static List GetProfiles() => _profiles; + public static List GetProfiles() + { + lock (_syncLock) { return [.. _profiles]; } + } public static FilterProfile? GetActiveProfile() { - return _profiles.FirstOrDefault(p => p.Id == ActiveProfileId) ?? _profiles.FirstOrDefault(); + lock (_syncLock) + { + return _profiles.FirstOrDefault(p => p.Id == ActiveProfileId) ?? _profiles.FirstOrDefault(); + } } public static void SaveProfiles(List profiles, string activeId) { - _profiles = profiles; - ActiveProfileId = activeId; - string json = System.Text.Json.JsonSerializer.Serialize(_profiles); - Save("FilterProfiles", json); - Save("ActiveProfileId", activeId); + lock (_syncLock) + { + _profiles = profiles; + ActiveProfileId = activeId; + string json = System.Text.Json.JsonSerializer.Serialize(_profiles); + Save("FilterProfiles", json); + Save("ActiveProfileId", activeId); + } } /// 将选中的视觉表现项恢复默认值并写入注册表 @@ -296,8 +310,9 @@ public static void Save(string name, object value) { try { - using (RegistryKey key = Registry.CurrentUser.CreateSubKey(RegPath)) + lock (_syncLock) { + using RegistryKey key = Registry.CurrentUser.CreateSubKey(RegPath); if (value is Enum enumValue) { key.SetValue(name, enumValue.ToString()); @@ -307,7 +322,7 @@ public static void Save(string name, object value) key.SetValue(name, value); } - var prop = typeof(ConfigManager).GetProperty(name); + var prop = _propertyCache.GetOrAdd(name, n => typeof(ConfigManager).GetProperty(n)); if (prop != null) { object propertyValue = value; @@ -475,48 +490,51 @@ public static void ResetAndClear() { try { - Registry.CurrentUser.DeleteSubKeyTree(RegPath, false); - - string oldJson = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config.json"); - if (System.IO.File.Exists(oldJson)) + lock (_syncLock) { - System.IO.File.Delete(oldJson); - } + Registry.CurrentUser.DeleteSubKeyTree(RegPath, false); - ParticleColor = "45,175,255"; - IsEffectEnabled = true; - AutoStart = false; - AgreedToPrivacy = false; - EnableTelemetry = false; - TotalClicks = 0; - LastNoticeContent = ""; - EnableAlwaysTrailEffect = false; - StartSilent = false; - RunAsAdmin = false; - EffectScale = 1.5; - EffectOpacity = 1.0; - EffectSpeed = 1.0; - UseLinkedAnimationSpeed = true; - TrailAnimationSpeed = 1.0; - ClickAnimationSpeed = 1.0; - TrailRefreshRate = 40; - EnableEnvironmentFilter = false; - HideInFullscreen = true; - ShowEffectOnDesktop = true; - FilterProfiles = ""; - ActiveProfileId = ""; - _profiles.Clear(); - IsTouchscreenMode = false; - ClickTriggerType = 0; - EnableMiddleClickTrigger = false; - ScreenshotCompatibilityMode = false; - EnabledScreenIds = ""; - ScreenSelections = ""; - UiLanguage = ""; - NetworkRegion = NetworkRegionOption.Auto; - SidebarBackgroundImagePath = ""; - TelemetryClientId = ""; - LastTelemetrySentUtc = ""; + string oldJson = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config.json"); + if (System.IO.File.Exists(oldJson)) + { + System.IO.File.Delete(oldJson); + } + + ParticleColor = "45,175,255"; + IsEffectEnabled = true; + AutoStart = false; + AgreedToPrivacy = false; + EnableTelemetry = false; + TotalClicks = 0; + LastNoticeContent = ""; + EnableAlwaysTrailEffect = false; + StartSilent = false; + RunAsAdmin = false; + EffectScale = 1.5; + EffectOpacity = 1.0; + EffectSpeed = 1.0; + UseLinkedAnimationSpeed = true; + TrailAnimationSpeed = 1.0; + ClickAnimationSpeed = 1.0; + TrailRefreshRate = 40; + EnableEnvironmentFilter = false; + HideInFullscreen = true; + ShowEffectOnDesktop = true; + FilterProfiles = ""; + ActiveProfileId = ""; + _profiles.Clear(); + IsTouchscreenMode = false; + ClickTriggerType = 0; + EnableMiddleClickTrigger = false; + ScreenshotCompatibilityMode = false; + EnabledScreenIds = ""; + ScreenSelections = ""; + UiLanguage = ""; + NetworkRegion = NetworkRegionOption.Auto; + SidebarBackgroundImagePath = ""; + TelemetryClientId = ""; + LastTelemetrySentUtc = ""; + } } catch { } }