Skip to content

Commit

Permalink
Merge pull request #2 from shargon/review
Browse files Browse the repository at this point in the history
Optimize remove
  • Loading branch information
Qiao-Jin committed Oct 23, 2019
2 parents a745880 + a84eff9 commit dd36f20
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
12 changes: 12 additions & 0 deletions neo/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ internal static int GetLowestSetBit(this BigInteger i)
throw new Exception();
}

internal static void Remove<T>(this HashSet<T> set, IEnumerable<T> remove)
{
if (set.Count > 600000)
{
set.ExceptWith(remove);
}
else
{
set.RemoveWhere(u => remove.Contains(u));
}
}

internal static string GetVersion(this Assembly assembly)
{
CustomAttributeData attribute = assembly.CustomAttributes.FirstOrDefault(p => p.AttributeType == typeof(AssemblyInformationalVersionAttribute));
Expand Down
8 changes: 4 additions & 4 deletions neo/Network/P2P/TaskManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ private void OnNewTasks(InvPayload payload)
return;
}
HashSet<UInt256> hashes = new HashSet<UInt256>(payload.Hashes);
hashes.RemoveWhere(q => knownHashes.Contains(q));
hashes.Remove(knownHashes);
if (payload.Type == InventoryType.Block)
session.AvailableTasks.UnionWith(hashes.Where(p => globalTasks.ContainsKey(p)));

hashes.RemoveWhere(q => globalTasks.ContainsKey(q));
hashes.Remove(globalTasks.Keys);
if (hashes.Count == 0)
{
RequestTasks(session);
Expand Down Expand Up @@ -203,7 +203,7 @@ private void RequestTasks(TaskSession session)
if (session.HasTask) return;
if (session.AvailableTasks.Count > 0)
{
session.AvailableTasks.RemoveWhere(q => knownHashes.Contains(q));
session.AvailableTasks.Remove(knownHashes);
session.AvailableTasks.RemoveWhere(p => Blockchain.Singleton.ContainsBlock(p));
HashSet<UInt256> hashes = new HashSet<UInt256>(session.AvailableTasks);
if (hashes.Count > 0)
Expand All @@ -213,7 +213,7 @@ private void RequestTasks(TaskSession session)
if (!IncrementGlobalTask(hash))
hashes.Remove(hash);
}
session.AvailableTasks.ExceptWith(hashes);
session.AvailableTasks.Remove(hashes);
foreach (UInt256 hash in hashes)
session.Tasks[hash] = DateTime.UtcNow;
foreach (InvPayload group in InvPayload.CreateGroup(InventoryType.Block, hashes.ToArray()))
Expand Down

0 comments on commit dd36f20

Please sign in to comment.