From bc6d7e90ffb2e3c985bee0816c0184f9d3a2040e Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 28 Mar 2026 23:52:50 -0400 Subject: [PATCH] optimize AssetsInvalidated event args Some quick benchmarks show that... - `new HashSet(values)` (266ns) is 79% faster than `values.ToImmutableHashSet()` (1440ns); - filling a hash set with a loop (554ns) is 21% faster than filling it from a LINQ enumerable (697ns); - and searching a `HashSet` (18ns) is 49% faster than searching an `ImmutableHashSet` (35ns). --- src/SMAPI/Events/AssetsInvalidatedEventArgs.cs | 16 ++++++++++------ src/SMAPI/Framework/SCore.cs | 2 +- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/SMAPI/Events/AssetsInvalidatedEventArgs.cs b/src/SMAPI/Events/AssetsInvalidatedEventArgs.cs index 73ae738c..5efaa917 100644 --- a/src/SMAPI/Events/AssetsInvalidatedEventArgs.cs +++ b/src/SMAPI/Events/AssetsInvalidatedEventArgs.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Collections.Immutable; namespace StardewModdingAPI.Events; @@ -22,11 +21,16 @@ public class AssetsInvalidatedEventArgs : EventArgs ** Public methods *********/ /// Construct an instance. - /// The asset names that were invalidated. - /// The with any locale codes stripped. - internal AssetsInvalidatedEventArgs(IEnumerable names, IEnumerable namesWithoutLocale) + /// The asset names that were invalidated. + internal AssetsInvalidatedEventArgs(ICollection assetNames) { - this.Names = names.ToImmutableHashSet(); - this.NamesWithoutLocale = namesWithoutLocale.ToImmutableHashSet(); + HashSet names = new(assetNames); + + HashSet namesWithoutLocale = []; + foreach (IAssetName name in names) + namesWithoutLocale.Add(name.GetBaseAssetName()); + + this.Names = names; + this.NamesWithoutLocale = namesWithoutLocale; } } diff --git a/src/SMAPI/Framework/SCore.cs b/src/SMAPI/Framework/SCore.cs index 7e64dd16..eceda4d0 100644 --- a/src/SMAPI/Framework/SCore.cs +++ b/src/SMAPI/Framework/SCore.cs @@ -1415,7 +1415,7 @@ internal class SCore : IDisposable private void OnAssetsInvalidated(ICollection assetNames) { if (this.EventManager.AssetsInvalidated.HasListeners) - this.EventManager.AssetsInvalidated.Raise(new AssetsInvalidatedEventArgs(assetNames, assetNames.Select(p => p.GetBaseAssetName()))); + this.EventManager.AssetsInvalidated.Raise(new AssetsInvalidatedEventArgs(assetNames)); } /// Reload the SMAPI settings.