Skip to content

Commit

Permalink
Optimize RemoteNode (#2800)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang authored Aug 3, 2022
1 parent e32ef6e commit 76f9d9a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
41 changes: 41 additions & 0 deletions src/Neo/IO/Caching/KeyedCollectionSlim.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;

namespace Neo.IO.Caching;

abstract class KeyedCollectionSlim<TKey, TItem> where TKey : notnull
{
private readonly LinkedList<TItem> _items = new();
private readonly Dictionary<TKey, LinkedListNode<TItem>> dict = new();

public int Count => dict.Count;
public TItem First => _items.First.Value;

protected abstract TKey GetKeyForItem(TItem item);

public void Add(TItem item)
{
var key = GetKeyForItem(item);
var node = _items.AddLast(item);
if (!dict.TryAdd(key, node))
{
_items.RemoveLast();
throw new ArgumentException("An element with the same key already exists in the collection.");
}
}

public bool Contains(TKey key) => dict.ContainsKey(key);

public void Remove(TKey key)
{
if (dict.Remove(key, out var node))
_items.Remove(node);
}

public void RemoveFirst()
{
var key = GetKeyForItem(_items.First.Value);
dict.Remove(key);
_items.RemoveFirst();
}
}
7 changes: 3 additions & 4 deletions src/Neo/Network/P2P/RemoteNode.ProtocolHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;

Expand All @@ -30,7 +29,7 @@ namespace Neo.Network.P2P
partial class RemoteNode
{
private class Timer { }
private class PendingKnownHashesCollection : KeyedCollection<UInt256, (UInt256, DateTime)>
private class PendingKnownHashesCollection : KeyedCollectionSlim<UInt256, (UInt256, DateTime)>
{
protected override UInt256 GetKeyForItem((UInt256, DateTime) item)
{
Expand Down Expand Up @@ -412,9 +411,9 @@ private void OnTimer()
DateTime oneMinuteAgo = TimeProvider.Current.UtcNow.AddMinutes(-1);
while (pendingKnownHashes.Count > 0)
{
var (_, time) = pendingKnownHashes[0];
var (_, time) = pendingKnownHashes.First;
if (oneMinuteAgo <= time) break;
pendingKnownHashes.RemoveAt(0);
pendingKnownHashes.RemoveFirst();
}
if (oneMinuteAgo > lastSent)
EnqueueMessage(Message.Create(MessageCommand.Ping, PingPayload.Create(NativeContract.Ledger.CurrentIndex(system.StoreView))));
Expand Down

0 comments on commit 76f9d9a

Please sign in to comment.