diff --git a/src/neo/IO/Caching/HashSetCache.cs b/src/neo/IO/Caching/HashSetCache.cs index a3fe71c559..f563ee82b4 100644 --- a/src/neo/IO/Caching/HashSetCache.cs +++ b/src/neo/IO/Caching/HashSetCache.cs @@ -6,6 +6,11 @@ namespace Neo.IO.Caching { public class HashSetCache : IReadOnlyCollection where T : IEquatable { + /// + /// Cached count + /// + private int _count = -1; + /// /// Sets where the Hashes are stored /// @@ -25,12 +30,13 @@ public int Count { get { - int count = 0; + if (_count != -1) return _count; + _count = 0; foreach (var set in sets) { - count += set.Count; + _count += set.Count; } - return count; + return _count; } } @@ -47,6 +53,7 @@ public HashSetCache(int bucketCapacity, int maxBucketCount = 10) public bool Add(T item) { if (Contains(item)) return false; + _count = -1; if (sets.First.Value.Count < bucketCapacity) return sets.First.Value.Add(item); var newSet = new HashSet { @@ -72,7 +79,11 @@ public void ExceptWith(IEnumerable items) { foreach (var set in sets) { - if (set.Remove(item)) break; + if (set.Remove(item)) + { + _count = -1; + break; + } } } }