optimize AssetsInvalidated event args

Some quick benchmarks show that...
- `new HashSet<T>(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).
This commit is contained in:
Jesse Plamondon-Willard
2026-03-28 23:52:50 -04:00
parent 6bfb481838
commit bc6d7e90ff
2 changed files with 11 additions and 7 deletions
+10 -6
View File
@@ -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
*********/
/// <summary>Construct an instance.</summary>
/// <param name="names">The asset names that were invalidated.</param>
/// <param name="namesWithoutLocale">The <paramref name="names"/> with any locale codes stripped.</param>
internal AssetsInvalidatedEventArgs(IEnumerable<IAssetName> names, IEnumerable<IAssetName> namesWithoutLocale)
/// <param name="assetNames">The asset names that were invalidated.</param>
internal AssetsInvalidatedEventArgs(ICollection<IAssetName> assetNames)
{
this.Names = names.ToImmutableHashSet();
this.NamesWithoutLocale = namesWithoutLocale.ToImmutableHashSet();
HashSet<IAssetName> names = new(assetNames);
HashSet<IAssetName> namesWithoutLocale = [];
foreach (IAssetName name in names)
namesWithoutLocale.Add(name.GetBaseAssetName());
this.Names = names;
this.NamesWithoutLocale = namesWithoutLocale;
}
}
+1 -1
View File
@@ -1415,7 +1415,7 @@ internal class SCore : IDisposable
private void OnAssetsInvalidated(ICollection<IAssetName> 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));
}
/// <summary>Reload the SMAPI settings.</summary>