From 0a82a194c51a3f7a9dfcce7444d1c0016cd589f4 Mon Sep 17 00:00:00 2001 From: ManlyMarco <39247311+ManlyMarco@users.noreply.github.com> Date: Sat, 2 Sep 2023 13:37:26 +0200 Subject: [PATCH] [Sideloader] Improve card saving speed --- .../UniversalAutoResolver/Core.UAR.Hooks.cs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Core_Sideloader/UniversalAutoResolver/Core.UAR.Hooks.cs b/src/Core_Sideloader/UniversalAutoResolver/Core.UAR.Hooks.cs index 01c30055..83748ed2 100644 --- a/src/Core_Sideloader/UniversalAutoResolver/Core.UAR.Hooks.cs +++ b/src/Core_Sideloader/UniversalAutoResolver/Core.UAR.Hooks.cs @@ -211,11 +211,23 @@ private static void ChaFileSaveFilePostHook(ChaFile __instance) Sideloader.Logger.LogDebug($"External info: {info.GUID} : {info.Property} : {info.Slot}"); } + // Create a property name lookup to speed up saving (on the order of 4 minutes -> 15 seconds when saving fully populated KK game) + var extInfoLookup = new Dictionary(); + // To keep backwards compatibility, only the first entry with a given property in the extInfo list should be used if there are multiple. + // Achieve this by adding items in reverse so the first instance overwrites the second instance that was already set. + // There should be no duplicates most of the time so it's faster to do it this way than check if item exists on each iteration. + for (var index = extInfo.Count - 1; index >= 0; index--) + { + var info = extInfo[index]; + extInfoLookup[info.Property] = info; + } + void ResetStructResolveStructure(Dictionary> propertyDict, object structure, IEnumerable extInfo2, string propertyPrefix = "") { foreach (var kv in propertyDict) { - var extResolve = extInfo.FirstOrDefault(x => x.Property == $"{propertyPrefix}{kv.Key}"); + // Old and slow: var extResolve = extInfo.FirstOrDefault(x => x.Property == $"{propertyPrefix}{kv.Key}"); + extInfoLookup.TryGetValue($"{propertyPrefix}{kv.Key}", out var extResolve); if (extResolve != null) kv.Value.SetMethod(structure, extResolve.LocalSlot);