diff --git a/src/GameUtils/Types/Collections/SynchronizedCollection.cs b/src/GameUtils/Types/Collections/SynchronizedCollection.cs index 5d29626..901c76c 100644 --- a/src/GameUtils/Types/Collections/SynchronizedCollection.cs +++ b/src/GameUtils/Types/Collections/SynchronizedCollection.cs @@ -31,6 +31,8 @@ public abstract class SynchronizedCollection : IEnumerable where T : notnu { private readonly ConcurrentQueue> _pending = new(); private readonly SemaphoreSlim _integrating = new(1, 1); + private IReadOnlyList? _cachedSnapshot; + private bool _dirty = true; /// /// Schedules an entity to be added to the collection. @@ -57,9 +59,16 @@ public void Integrate() _integrating.Wait(); try { + bool modified = false; while (_pending.TryDequeue(out var operation)) { HandleOperation(operation); + modified = true; + } + + if (modified) + { + _dirty = true; } } finally @@ -101,7 +110,13 @@ public IEnumerable Get() _integrating.Wait(); try { - return GetInternal().ToList(); + if (_dirty || _cachedSnapshot is null) + { + _cachedSnapshot = GetInternal().ToList(); + _dirty = false; + } + + return _cachedSnapshot; } finally {