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

Improve performance of Swarm<T> #837

Merged
merged 3 commits into from
Mar 30, 2020
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ To be released.
- `Swarm<T>.PreloadAsync()` and `Swarm<T>.StartAsync()` became to download
only a list of block hashes first and then download blocks from
simultaneously multiple peers. [[#707], [#798]]
- Improved performance of `Swarm<T>` by preventing unnecessary task
creation. [[#817], [#837]]

### Bug fixes

Expand Down Expand Up @@ -128,9 +130,11 @@ To be released.
[#802]: https://github.com/planetarium/libplanet/pull/802
[#803]: https://github.com/planetarium/libplanet/pull/803
[#815]: https://github.com/planetarium/libplanet/pull/815
[#817]: https://github.com/planetarium/libplanet/issues/817
[#820]: https://github.com/planetarium/libplanet/pull/820
[#825]: https://github.com/planetarium/libplanet/pull/825
[#831]: https://github.com/planetarium/libplanet/pull/831
[#837]: https://github.com/planetarium/libplanet/pull/837


Version 0.8.0
Expand Down
63 changes: 29 additions & 34 deletions Libplanet/Net/NetMQSocketExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Libplanet.Net
{
internal static class NetMQSocketExtensions
{
public static async Task SendMultipartMessageAsync(
public static Task SendMultipartMessageAsync(
this NetMQSocket socket,
NetMQMessage message,
TimeSpan? timeout = null,
Expand All @@ -19,32 +19,31 @@ public static async Task SendMultipartMessageAsync(
cts.CancelAfter(timeoutNotNull);
}

var ct = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, cts.Token);
try
{
await socket.SendMultipartMessageAsync(message, false, cancellationToken: ct.Token);
}
catch (TaskCanceledException)
var ct = CancellationTokenSource.CreateLinkedTokenSource(
cancellationToken,
cts.Token
);

return socket.SendMultipartMessageAsync(
message,
false,
cancellationToken: ct.Token
).ContinueWith(t =>
{
if (cts.Token.IsCancellationRequested)
if (t.IsCanceled && cts.Token.IsCancellationRequested)
{
throw new TimeoutException(
$"The operation exceeded the specified time: {timeout}."
);
}
else
{
throw;
}
}
finally
{

cts.Dispose();
ct.Dispose();
}
return t;
});
}

public static async Task<NetMQMessage> ReceiveMultipartMessageAsync(
public static Task<NetMQMessage> ReceiveMultipartMessageAsync(
this NetMQSocket socket,
TimeSpan? timeout = null,
CancellationToken cancellationToken = default(CancellationToken))
Expand All @@ -55,31 +54,27 @@ public static async Task<NetMQMessage> ReceiveMultipartMessageAsync(
cts.CancelAfter(timeoutNotNull);
}

var ct = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, cts.Token);
try
{
return await socket.ReceiveMultipartMessageAsync(
expectedFrameCount: 4,
cancellationToken: ct.Token);
}
catch (TaskCanceledException)
var ct = CancellationTokenSource.CreateLinkedTokenSource(
cancellationToken,
cts.Token
);

return socket.ReceiveMultipartMessageAsync(
expectedFrameCount: 4,
cancellationToken: ct.Token
).ContinueWith(t =>
{
if (cts.Token.IsCancellationRequested)
if (t.IsCanceled && cts.IsCancellationRequested)
{
throw new TimeoutException(
$"The operation exceeded the specified time: {timeout}."
);
}
else
{
throw;
}
}
finally
{

cts.Dispose();
ct.Dispose();
}
return t.Result;
});
}
}
}
19 changes: 14 additions & 5 deletions Libplanet/Net/Swarm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2119,27 +2119,36 @@ private async Task ProcessFillTxs(CancellationToken cancellationToken)
"Processing txids: {@txIds}",
_demandTxIds.Keys.Select(txid => txid.ToString()));
var demandTxIds = _demandTxIds.ToArray();
var demands = new Dictionary<BoundPeer, List<TxId>>();
var demands = new Dictionary<BoundPeer, HashSet<TxId>>();

foreach (var kv in demandTxIds)
{
if (!demands.ContainsKey(kv.Value))
{
demands[kv.Value] = new List<TxId>();
demands[kv.Value] = new HashSet<TxId>();
}

demands[kv.Value].Add(kv.Key);
}

var txs = new HashSet<Transaction<T>>();
var tasks = new List<Task<List<Transaction<T>>>>();
foreach (var kv in demands)
{
IAsyncEnumerable<Transaction<T>> fetched =
GetTxsAsync(kv.Key, kv.Value, cancellationToken);
tasks.Add(fetched.ToListAsync(cancellationToken).AsTask());
ValueTask<List<Transaction<T>>> vt = fetched.ToListAsync(cancellationToken);

if (vt.IsCompletedSuccessfully)
{
txs.UnionWith(vt.Result);
}
else
{
tasks.Add(vt.AsTask());
}
}

var txs = new List<Transaction<T>>();
try
{
await tasks.WhenAll();
Expand All @@ -2155,7 +2164,7 @@ private async Task ProcessFillTxs(CancellationToken cancellationToken)
if (!task.IsFaulted)
{
// `task.Result` is okay because we've already waited.
txs.AddRange(task.Result);
txs.UnionWith(task.Result);
}
}

Expand Down