Conversation
There was a problem hiding this comment.
Pull request overview
This PR optimizes the backup and restore functionality for the context menu manager by introducing a dictionary-based lookup system to improve performance when searching for backup items.
Changes:
- Replaced O(n) linear searches with O(1) dictionary lookups using a new
backupLookupdictionary - Added
GetRegistryKeySafemethod to handle registry key access differently during backup vs restore operations - Optimized registry ownership operations to only execute during restore operations
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
|
@LanYun2022 这只包含代码质量的更新吗?能否将与原先逻辑改变的地方做出标注? |
|
@Jack251970 只是把代码拆分和优化了 |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // 获取MetaData对象 | ||
| metaData = myData.MetaData; | ||
|
|
||
| // 清理backupRestoreList变量 | ||
| backupRestoreList.Clear(); | ||
| backupRestoreList = null; | ||
|
|
||
| // 获取BackupList对象 | ||
| backupRestoreList = myData.BackupList; | ||
| backupLookup.Clear(); | ||
| foreach (var item in backupRestoreList) | ||
| { | ||
| var key = GetLookupKey(item.BackupScene, item.KeyName, item.ItemType); | ||
| if (!backupLookup.ContainsKey(key)) backupLookup.Add(key, item); | ||
| } |
There was a problem hiding this comment.
LoadBackupList clears backupRestoreList and then sets it to null before reassigning from myData.BackupList. If the XML is missing <BackupList> (or it deserializes as null), the subsequent foreach will throw, and later calls like ClearBackupList() will also crash. Consider keeping the field non-null and defaulting to an empty list (e.g., assign myData.BackupList ?? new List<BackupItem>()) and remove the = null step.
| public static void LoadBackupDataMetaData(string filePath) | ||
| { | ||
| // 反序列化root对象并保存到XML文档 | ||
| using var stream = new FileStream(filePath, FileMode.Open); | ||
| // 读取 <MetaData> 节点 | ||
| using var reader = XmlReader.Create(stream); | ||
| // 寻找第一个<MetaData>节点 | ||
| reader.ReadToFollowing("MetaData"); | ||
|
|
||
| // 清理metaData变量 | ||
| metaData = null; | ||
|
|
||
| // 反序列化<MetaData>节点为MetaData对象 | ||
| metaData = (MetaData)metaDataSerializer.Deserialize(reader); | ||
| } |
There was a problem hiding this comment.
LoadBackupDataMetaData ignores the return value of reader.ReadToFollowing("MetaData"). If the node is missing/corrupt, Deserialize(reader) will throw (and metaData is set to null just before that). Consider checking the boolean result and handling the error (e.g., keep existing metaData, or initialize a new MetaData, or early-return) to avoid exceptions during backup cleanup.
No description provided.