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

Use HashsetCache instead of FIFOSet for KnownHashes and SentHashes #1389

Merged
merged 28 commits into from
Jan 9, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
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
13 changes: 13 additions & 0 deletions src/neo/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,19 @@ internal static void Remove<T>(this HashSet<T> set, FIFOSet<T> other)
}
}

internal static void Remove<T>(this HashSet<T> set, HashSetCache<T> other)
where T : IEquatable<T>
{
if (set.Count > other.Count)
{
set.ExceptWith(other);
}
else
{
set.RemoveWhere(u => other.Contains(u));
}
}

internal static void Remove<T, V>(this HashSet<T> set, IReadOnlyDictionary<T, V> other)
{
if (set.Count > other.Count)
Expand Down
89 changes: 89 additions & 0 deletions src/neo/IO/Caching/HashSetCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System;
using System.Collections;
using System.Collections.Generic;

namespace Neo.IO.Caching
{
public class HashSetCache<T> : IEnumerable<T> where T : IEquatable<T>
{
private readonly int hashSetCapacity;
private readonly List<HashSet<T>> sets = new List<HashSet<T>>();

public int Count
{
get
{
int count = 0;
foreach (var set in sets)
{
count += set.Count;
}
return count;
}
}

public HashSetCache(int hashSetCapacity, int hashSetCount = 10)
{
if (hashSetCapacity <= 0) throw new ArgumentOutOfRangeException(nameof(hashSetCapacity));
if (hashSetCount <= 0 || hashSetCount > 20) throw new ArgumentOutOfRangeException($"{nameof(hashSetCount)} should between 1 and 20");

this.hashSetCapacity = hashSetCapacity;
for (int i = 0; i < hashSetCount; i++)
{
sets.Add(new HashSet<T>());
}
}

public bool Add(T item)
{
if (Contains(item)) return false;
foreach (var set in sets)
{
if (set.Count < hashSetCapacity)
{
return set.Add(item);
}
}
sets.RemoveAt(0);
var newSet = new HashSet<T>
{
item
};
sets.Add(newSet);
return true;
}

public bool Contains(T item)
{
foreach (var set in sets)
{
if (set.Contains(item)) return true;
}
return false;
}

public void ExceptWith(IEnumerable<T> items)
{
foreach (var item in items)
{
foreach (var set in sets)
{
if (set.Remove(item)) break;
}
}
}

public IEnumerator<T> GetEnumerator()
{
foreach (var set in sets)
{
foreach (var item in set)
{
yield return item;
}
}
}

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
}
8 changes: 4 additions & 4 deletions src/neo/Network/P2P/ProtocolHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ protected override UInt256 GetKeyForItem((UInt256, DateTime) item)

private readonly NeoSystem system;
private readonly PendingKnownHashesCollection pendingKnownHashes;
private readonly FIFOSet<UInt256> knownHashes;
private readonly FIFOSet<UInt256> sentHashes;
private readonly HashSetCache<UInt256> knownHashes;
private readonly HashSetCache<UInt256> sentHashes;
private VersionPayload version;
private bool verack = false;
private BloomFilter bloom_filter;
Expand All @@ -47,8 +47,8 @@ public ProtocolHandler(NeoSystem system)
{
this.system = system;
this.pendingKnownHashes = new PendingKnownHashesCollection();
this.knownHashes = new FIFOSet<UInt256>(Blockchain.Singleton.MemPool.Capacity * 2);
this.sentHashes = new FIFOSet<UInt256>(Blockchain.Singleton.MemPool.Capacity * 2);
this.knownHashes = new HashSetCache<UInt256>(Blockchain.Singleton.MemPool.Capacity * 2 / 10);
this.sentHashes = new HashSetCache<UInt256>(Blockchain.Singleton.MemPool.Capacity * 2 / 10);
}

protected override void OnReceive(object message)
Expand Down
4 changes: 2 additions & 2 deletions src/neo/Network/P2P/TaskManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private class Timer { }
/// <summary>
/// A set of known hashes, of inventories or payloads, already received.
/// </summary>
private readonly FIFOSet<UInt256> knownHashes;
private readonly HashSetCache<UInt256> knownHashes;
private readonly Dictionary<UInt256, int> globalTasks = new Dictionary<UInt256, int>();
private readonly Dictionary<IActorRef, TaskSession> sessions = new Dictionary<IActorRef, TaskSession>();
private readonly ICancelable timer = Context.System.Scheduler.ScheduleTellRepeatedlyCancelable(TimerInterval, TimerInterval, Context.Self, new Timer(), ActorRefs.NoSender);
Expand All @@ -43,7 +43,7 @@ private class Timer { }
public TaskManager(NeoSystem system)
{
this.system = system;
this.knownHashes = new FIFOSet<UInt256>(Blockchain.Singleton.MemPool.Capacity * 2);
this.knownHashes = new HashSetCache<UInt256>(Blockchain.Singleton.MemPool.Capacity * 2 / 10);
}

private void OnHeaderTaskCompleted()
Expand Down
44 changes: 44 additions & 0 deletions tests/neo.UnitTests/IO/Caching/UT_HashSetCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.IO.Caching;

namespace Neo.UnitTests.IO.Caching
{
[TestClass]
public class UT_HashSetCache
{
[TestMethod]
public void TestHashSetCache()
{
var bucket = new HashSetCache<int>(10);
for (int i = 1; i <= 100; i++)
{
bucket.Add(i);
}
bucket.Count.Should().Be(100);

int sum = 0;
foreach (var ele in bucket)
{
sum += ele;
}
sum.Should().Be(5050);

bucket.Add(101);
bucket.Count.Should().Be(91);

var items = new int[10];
var value = 11;
for (int i = 0; i < 10; i++)
{
items[i] = value;
value += 2;
}
bucket.ExceptWith(items);
bucket.Count.Should().Be(81);

bucket.Contains(13).Should().BeFalse();
bucket.Contains(50).Should().BeTrue();
}
}
}