Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize FIFOSet #1392

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions src/neo/IO/Caching/FIFOSet.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Collections.ObjectModel;

namespace Neo.IO.Caching
{
internal class FIFOSet<T> : IReadOnlyCollection<T> where T : IEquatable<T>
{
private class Collection : KeyedCollection<T, T>
{
protected override T GetKeyForItem(T item)
{
return item;
}
}

private readonly int maxCapacity;
private readonly int removeCount;
private readonly OrderedDictionary dictionary;
private readonly Collection dictionary;

public int Count => dictionary.Count;

Expand All @@ -21,7 +28,7 @@ public FIFOSet(int maxCapacity, decimal batchSize = 0.1m)

this.maxCapacity = maxCapacity;
this.removeCount = Math.Max((int)(maxCapacity * batchSize), 1);
this.dictionary = new OrderedDictionary(maxCapacity);
this.dictionary = new Collection();
}

public bool Add(T item)
Expand All @@ -39,7 +46,7 @@ public bool Add(T item)
dictionary.RemoveAt(0);
}
}
dictionary.Add(item, null);
dictionary.Add(item);
return true;
}

Expand All @@ -58,8 +65,7 @@ public void ExceptWith(IEnumerable<T> entries)

public IEnumerator<T> GetEnumerator()
{
var entries = dictionary.Keys.Cast<T>().ToArray();
foreach (var entry in entries) yield return entry;
return dictionary.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
Expand Down