mirror of
https://github.com/Pathoschild/SMAPI.git
synced 2026-10-12 02:08:51 +00:00
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:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user