summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAmeisen <14104310+ameisen@users.noreply.github.com>2022-06-01 19:39:47 -0500
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2022-06-10 00:04:26 -0400
commit03897776e08cb0703c9763e3b5dcc81d85f277f9 (patch)
tree9789abb7291304b66858ba931363fc3f524ac550 /src
parent43e9e2cfe2c6938121d9a5e106acfc944a1821c6 (diff)
downloadSMAPI-03897776e08cb0703c9763e3b5dcc81d85f277f9.tar.gz
SMAPI-03897776e08cb0703c9763e3b5dcc81d85f277f9.tar.bz2
SMAPI-03897776e08cb0703c9763e3b5dcc81d85f277f9.zip
Cleaning up and optimizing `ContentCache.cs`
Diffstat (limited to 'src')
-rw-r--r--src/SMAPI/Framework/Content/ContentCache.cs31
1 files changed, 19 insertions, 12 deletions
diff --git a/src/SMAPI/Framework/Content/ContentCache.cs b/src/SMAPI/Framework/Content/ContentCache.cs
index 736ee5da..959d4fb3 100644
--- a/src/SMAPI/Framework/Content/ContentCache.cs
+++ b/src/SMAPI/Framework/Content/ContentCache.cs
@@ -14,7 +14,7 @@ namespace StardewModdingAPI.Framework.Content
** Fields
*********/
/// <summary>The underlying asset cache.</summary>
- private readonly IDictionary<string, object> Cache;
+ private readonly Dictionary<string, object> Cache;
/*********
@@ -29,7 +29,7 @@ namespace StardewModdingAPI.Framework.Content
}
/// <summary>The current cache keys.</summary>
- public IEnumerable<string> Keys => this.Cache.Keys;
+ public Dictionary<string, object>.KeyCollection Keys => this.Cache.Keys;
/*********
@@ -89,33 +89,40 @@ namespace StardewModdingAPI.Framework.Content
/// <returns>Returns the removed key (if any).</returns>
public bool Remove(string key, bool dispose)
{
- // get entry
- if (!this.Cache.TryGetValue(key, out object? value))
+ // remove and get entry
+ if (!this.Cache.Remove(key, out object? value))
return false;
// dispose & remove entry
if (dispose && value is IDisposable disposable)
disposable.Dispose();
- return this.Cache.Remove(key);
+ return true;
}
- /// <summary>Purge matched assets from the cache.</summary>
+ /// <summary>Purge assets matching <paramref name="predicate"/> from the cache.</summary>
/// <param name="predicate">Matches the asset keys to invalidate.</param>
- /// <param name="dispose">Whether to dispose invalidated assets. This should only be <c>true</c> when they're being invalidated as part of a dispose, to avoid crashing the game.</param>
- /// <returns>Returns the removed keys (if any).</returns>
+ /// <param name="dispose">Whether to dispose invalidated assets. This should only be <see langword="true"/> when they're being invalidated as part of a <see cref="IDisposable.Dispose"/>, to avoid crashing the game.</param>
+ /// <returns>Returns any removed keys.</returns>
public IEnumerable<string> Remove(Func<string, object, bool> predicate, bool dispose)
{
List<string> removed = new List<string>();
- foreach (string key in this.Cache.Keys.ToArray())
+ foreach ((string key, object value) in this.Cache)
{
- if (predicate(key, this.Cache[key]))
+ if (predicate(key, value))
{
- this.Remove(key, dispose);
removed.Add(key);
}
}
- return removed;
+
+ foreach (string key in removed)
+ {
+ this.Remove(key, dispose);
+ }
+
+ // If `removed` is empty, return an empty `Enumerable` instead so that `removed`
+ // can be quickly collected in Gen0 instead of potentially living longer.
+ return removed.Count == 0 ? Enumerable.Empty<string>() : removed;
}
}
}