Skip to content

Commit

Permalink
Performance improvements, code beautification, adding test app
Browse files Browse the repository at this point in the history
  • Loading branch information
kpocza committed Dec 5, 2017
1 parent 621e5b5 commit a27d687
Show file tree
Hide file tree
Showing 18 changed files with 313 additions and 71 deletions.
4 changes: 2 additions & 2 deletions build/pck/GoSharp.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
<metadata>
<id>GoSharp</id>
<version>0.3.0</version>
<version>0.4.0</version>
<title>Go#</title>
<description>Go# library provides a mini embedded DSL for C# that mimics Go concurrency statements like channels and select.</description>
<authors>Krisztián Pócza</authors>
Expand All @@ -18,6 +18,6 @@
</group>
<group targetFramework=".NETStandard2.0" />
</dependencies>
<releaseNotes>Support for .NET Standard 2.0</releaseNotes>
<releaseNotes>Performance improvements, minor code beautification</releaseNotes>
</metadata>
</package>
104 changes: 52 additions & 52 deletions src/GoSharp.PerfTest/TaskRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,62 +194,62 @@ private void SelectSendRecvRunTask(int channelCount, int senderTaskCount, int se
//~~~~~~~~~~~

//Non-buffered:
//Sender tsk: 1, items/tsk: 200000; Recv tsk: 1, items/tsk: 200000: 408 ms
//Sender tsk: 2, items/tsk: 100000; Recv tsk: 1, items/tsk: 200000: 390 ms
//Sender tsk: 1, items/tsk: 200000; Recv tsk: 2, items/tsk: 100000: 394 ms
//Sender tsk: 10, items/tsk: 50000; Recv tsk: 10, items/tsk: 50000: 994 ms
//Sender tsk: 50, items/tsk: 10000; Recv tsk: 50, items/tsk: 10000: 516 ms
//Sender tsk: 100, items/tsk: 10000; Recv tsk: 100, items/tsk: 10000: 1091 ms
//Sender tsk: 10, items/tsk: 100000; Recv tsk: 100, items/tsk: 10000: 1092 ms
//Sender tsk: 100, items/tsk: 10000; Recv tsk: 10, items/tsk: 100000: 1375 ms
//Sender tsk: 500, items/tsk: 1000; Recv tsk: 500, items/tsk: 1000: 485 ms
//Sender tsk: 1000, items/tsk: 1000; Recv tsk: 1000, items/tsk: 1000: 1038 ms
//Sender tsk: 10, items/tsk: 100000; Recv tsk: 1000, items/tsk: 1000: 1116 ms
//Sender tsk: 1000, items/tsk: 1000; Recv tsk: 10, items/tsk: 100000: 1065 ms
//Sender tsk: 2000, items/tsk: 1000; Recv tsk: 2000, items/tsk: 1000: 2705 ms
//Sender tsk: 1, items/tsk: 200000; Recv tsk: 1, items/tsk: 200000: 145 ms
//Sender tsk: 2, items/tsk: 100000; Recv tsk: 1, items/tsk: 200000: 130 ms
//Sender tsk: 1, items/tsk: 200000; Recv tsk: 2, items/tsk: 100000: 131 ms
//Sender tsk: 10, items/tsk: 50000; Recv tsk: 10, items/tsk: 50000: 339 ms
//Sender tsk: 50, items/tsk: 10000; Recv tsk: 50, items/tsk: 10000: 283 ms
//Sender tsk: 100, items/tsk: 10000; Recv tsk: 100, items/tsk: 10000: 550 ms
//Sender tsk: 10, items/tsk: 100000; Recv tsk: 100, items/tsk: 10000: 590 ms
//Sender tsk: 100, items/tsk: 10000; Recv tsk: 10, items/tsk: 100000: 633 ms
//Sender tsk: 500, items/tsk: 1000; Recv tsk: 500, items/tsk: 1000: 324 ms
//Sender tsk: 1000, items/tsk: 1000; Recv tsk: 1000, items/tsk: 1000: 644 ms
//Sender tsk: 10, items/tsk: 100000; Recv tsk: 1000, items/tsk: 1000: 588 ms
//Sender tsk: 1000, items/tsk: 1000; Recv tsk: 10, items/tsk: 100000: 610 ms
//Sender tsk: 2000, items/tsk: 1000; Recv tsk: 2000, items/tsk: 1000: 1386 ms

//Buffered:
//Sender tsk: 1, items/tsk: 200000; Recv tsk: 1, items/tsk: 200000: 399 ms
//Sender tsk: 2, items/tsk: 100000; Recv tsk: 1, items/tsk: 200000: 365 ms
//Sender tsk: 1, items/tsk: 200000; Recv tsk: 2, items/tsk: 100000: 394 ms
//Sender tsk: 10, items/tsk: 50000; Recv tsk: 10, items/tsk: 50000: 489 ms
//Sender tsk: 50, items/tsk: 10000; Recv tsk: 50, items/tsk: 10000: 504 ms
//Sender tsk: 100, items/tsk: 10000; Recv tsk: 100, items/tsk: 10000: 1032 ms
//Sender tsk: 10, items/tsk: 100000; Recv tsk: 100, items/tsk: 10000: 1055 ms
//Sender tsk: 100, items/tsk: 10000; Recv tsk: 10, items/tsk: 100000: 1000 ms
//Sender tsk: 500, items/tsk: 1000; Recv tsk: 500, items/tsk: 1000: 550 ms
//Sender tsk: 1000, items/tsk: 1000; Recv tsk: 1000, items/tsk: 1000: 1215 ms
//Sender tsk: 10, items/tsk: 100000; Recv tsk: 1000, items/tsk: 1000: 1265 ms
//Sender tsk: 1000, items/tsk: 1000; Recv tsk: 10, items/tsk: 100000: 1169 ms
//Sender tsk: 2000, items/tsk: 1000; Recv tsk: 2000, items/tsk: 1000: 3027 ms
//Sender tsk: 1, items/tsk: 200000; Recv tsk: 1, items/tsk: 200000: 111 ms
//Sender tsk: 2, items/tsk: 100000; Recv tsk: 1, items/tsk: 200000: 140 ms
//Sender tsk: 1, items/tsk: 200000; Recv tsk: 2, items/tsk: 100000: 168 ms
//Sender tsk: 10, items/tsk: 50000; Recv tsk: 10, items/tsk: 50000: 326 ms
//Sender tsk: 50, items/tsk: 10000; Recv tsk: 50, items/tsk: 10000: 382 ms
//Sender tsk: 100, items/tsk: 10000; Recv tsk: 100, items/tsk: 10000: 790 ms
//Sender tsk: 10, items/tsk: 100000; Recv tsk: 100, items/tsk: 10000: 728 ms
//Sender tsk: 100, items/tsk: 10000; Recv tsk: 10, items/tsk: 100000: 713 ms
//Sender tsk: 500, items/tsk: 1000; Recv tsk: 500, items/tsk: 1000: 368 ms
//Sender tsk: 1000, items/tsk: 1000; Recv tsk: 1000, items/tsk: 1000: 719 ms
//Sender tsk: 10, items/tsk: 100000; Recv tsk: 1000, items/tsk: 1000: 714 ms
//Sender tsk: 1000, items/tsk: 1000; Recv tsk: 10, items/tsk: 100000: 635 ms
//Sender tsk: 2000, items/tsk: 1000; Recv tsk: 2000, items/tsk: 1000: 1596 ms

//Select non-buffered:
//Sender tsk: 1, items/tsk: 100000; Recv tsk: 1, items/tsk: 100000: 816 ms
//Sender tsk: 2, items/tsk: 100000; Recv tsk: 1, items/tsk: 200000: 1620 ms
//Sender tsk: 1, items/tsk: 200000; Recv tsk: 2, items/tsk: 100000: 1778 ms
//Sender tsk: 10, items/tsk: 10000; Recv tsk: 10, items/tsk: 10000: 641 ms
//Sender tsk: 50, items/tsk: 2000; Recv tsk: 50, items/tsk: 2000: 642 ms
//Sender tsk: 100, items/tsk: 1000; Recv tsk: 100, items/tsk: 1000: 636 ms
//Sender tsk: 10, items/tsk: 10000; Recv tsk: 100, items/tsk: 1000: 644 ms
//Sender tsk: 100, items/tsk: 1000; Recv tsk: 10, items/tsk: 10000: 626 ms
//Sender tsk: 500, items/tsk: 250; Recv tsk: 500, items/tsk: 250: 917 ms
//Sender tsk: 1000, items/tsk: 100; Recv tsk: 1000, items/tsk: 100: 939 ms
//Sender tsk: 10, items/tsk: 10000; Recv tsk: 1000, items/tsk: 100: 932 ms
//Sender tsk: 1000, items/tsk: 100; Recv tsk: 10, items/tsk: 10000: 620 ms
//Sender tsk: 2000, items/tsk: 100; Recv tsk: 2000, items/tsk: 100: 2345 ms
//Sender tsk: 1, items/tsk: 100000; Recv tsk: 1, items/tsk: 100000: 709 ms
//Sender tsk: 2, items/tsk: 100000; Recv tsk: 1, items/tsk: 200000: 1383 ms
//Sender tsk: 1, items/tsk: 200000; Recv tsk: 2, items/tsk: 100000: 1504 ms
//Sender tsk: 10, items/tsk: 10000; Recv tsk: 10, items/tsk: 10000: 528 ms
//Sender tsk: 50, items/tsk: 2000; Recv tsk: 50, items/tsk: 2000: 543 ms
//Sender tsk: 100, items/tsk: 1000; Recv tsk: 100, items/tsk: 1000: 472 ms
//Sender tsk: 10, items/tsk: 10000; Recv tsk: 100, items/tsk: 1000: 486 ms
//Sender tsk: 100, items/tsk: 1000; Recv tsk: 10, items/tsk: 10000: 403 ms
//Sender tsk: 500, items/tsk: 250; Recv tsk: 500, items/tsk: 250: 856 ms
//Sender tsk: 1000, items/tsk: 100; Recv tsk: 1000, items/tsk: 100: 1105 ms
//Sender tsk: 10, items/tsk: 10000; Recv tsk: 1000, items/tsk: 100: 1089 ms
//Sender tsk: 1000, items/tsk: 100; Recv tsk: 10, items/tsk: 10000: 441 ms
//Sender tsk: 2000, items/tsk: 100; Recv tsk: 2000, items/tsk: 100: 3256 ms

//Select buffered:
//Sender tsk: 1, items/tsk: 100000; Recv tsk: 1, items/tsk: 100000: 641 ms
//Sender tsk: 2, items/tsk: 100000; Recv tsk: 1, items/tsk: 200000: 1290 ms
//Sender tsk: 1, items/tsk: 200000; Recv tsk: 2, items/tsk: 100000: 758 ms
//Sender tsk: 10, items/tsk: 10000; Recv tsk: 10, items/tsk: 10000: 686 ms
//Sender tsk: 50, items/tsk: 2000; Recv tsk: 50, items/tsk: 2000: 715 ms
//Sender tsk: 100, items/tsk: 1000; Recv tsk: 100, items/tsk: 1000: 766 ms
//Sender tsk: 10, items/tsk: 10000; Recv tsk: 100, items/tsk: 1000: 784 ms
//Sender tsk: 100, items/tsk: 1000; Recv tsk: 10, items/tsk: 10000: 707 ms
//Sender tsk: 500, items/tsk: 250; Recv tsk: 500, items/tsk: 250: 1019 ms
//Sender tsk: 1000, items/tsk: 100; Recv tsk: 1000, items/tsk: 100: 1052 ms
//Sender tsk: 10, items/tsk: 10000; Recv tsk: 1000, items/tsk: 100: 1040 ms
//Sender tsk: 1000, items/tsk: 100; Recv tsk: 10, items/tsk: 10000: 701 ms
//Sender tsk: 2000, items/tsk: 100; Recv tsk: 2000, items/tsk: 100: 2733 ms
//Sender tsk: 1, items/tsk: 100000; Recv tsk: 1, items/tsk: 100000: 584 ms
//Sender tsk: 2, items/tsk: 100000; Recv tsk: 1, items/tsk: 200000: 1169 ms
//Sender tsk: 1, items/tsk: 200000; Recv tsk: 2, items/tsk: 100000: 1563 ms
//Sender tsk: 10, items/tsk: 10000; Recv tsk: 10, items/tsk: 10000: 521 ms
//Sender tsk: 50, items/tsk: 2000; Recv tsk: 50, items/tsk: 2000: 687 ms
//Sender tsk: 100, items/tsk: 1000; Recv tsk: 100, items/tsk: 1000: 573 ms
//Sender tsk: 10, items/tsk: 10000; Recv tsk: 100, items/tsk: 1000: 594 ms
//Sender tsk: 100, items/tsk: 1000; Recv tsk: 10, items/tsk: 10000: 518 ms
//Sender tsk: 500, items/tsk: 250; Recv tsk: 500, items/tsk: 250: 1026 ms
//Sender tsk: 1000, items/tsk: 100; Recv tsk: 1000, items/tsk: 100: 1217 ms
//Sender tsk: 10, items/tsk: 10000; Recv tsk: 1000, items/tsk: 100: 1162 ms
//Sender tsk: 1000, items/tsk: 100; Recv tsk: 10, items/tsk: 10000: 399 ms
//Sender tsk: 2000, items/tsk: 100; Recv tsk: 2000, items/tsk: 100: 3376 ms
}
12 changes: 6 additions & 6 deletions src/GoSharp.Test/TimerTickerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,17 @@ public void MultiTicksTest()
ticker.Start();

var now = ticker.Recv();
Assert.IsTrue(IsInDiff(start, now, 18, 22));
Assert.IsTrue(IsInDiff(start, now, 10, 35));
var last = now;
now = ticker.Recv();
Assert.IsTrue(IsInDiff(last, now, 18, 22));
Assert.IsTrue(IsInDiff(last, now, 10, 35));
last = now;
now = ticker.Recv();
Assert.IsTrue(IsInDiff(last, now, 18, 22));
Assert.IsTrue(IsInDiff(last, now, 10, 35));
last = now;
now = ticker.Recv();
ticker.Close();
Assert.IsTrue(IsInDiff(last, now, 18, 22));
Assert.IsTrue(IsInDiff(last, now, 10, 35));
}

[TestMethod]
Expand All @@ -139,11 +139,11 @@ public void MultiTicksSkipTest()
Thread.Sleep(21);
var last = now;
now = ticker.Recv();
Assert.IsTrue(IsInDiff(last, now, 18, 22));
Assert.IsTrue(IsInDiff(last, now, 18, 32));
last = now;
now = ticker.Recv();
ticker.Close();
Assert.IsTrue(IsInDiff(last, now, 18, 22));
Assert.IsTrue(IsInDiff(last, now, 18, 32));
}

[TestMethod]
Expand Down
4 changes: 2 additions & 2 deletions src/GoSharp/Channel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public Task<bool> SendAsync(T msg)
return SendCoreAsync(msg);
}

public async Task<T> RecvAsync()
public Task<T> RecvAsync()
{
return (T)await RecvCoreAsync();
return RecvCoreAsync<T>();
}

public IEnumerable<T> Range
Expand Down
10 changes: 5 additions & 5 deletions src/GoSharp/Impl/ChannelBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected async Task<bool> SendCoreAsync(object msg)
return true;
}

protected async Task<object> RecvCoreAsync()
protected async Task<T> RecvCoreAsync<T>()
{
if (_isClosed)
throw new ChannelClosedException();
Expand All @@ -94,7 +94,7 @@ protected async Task<object> RecvCoreAsync()

Unlock();

return msg;
return (T)msg;
}

if (_writerQueue.TryDequeue(out TransferQueueItem writerQueueItem))
Expand All @@ -103,7 +103,7 @@ protected async Task<object> RecvCoreAsync()

var msg = writerQueueItem.ChannelOperation.Msg;
writerQueueItem.ChannelOperation.Notify();
return msg;
return (T)msg;
}

var evt = new CompletionEvent();
Expand All @@ -117,7 +117,7 @@ protected async Task<object> RecvCoreAsync()
if (_isClosed)
throw new ChannelClosedException();

return recvOperation.Msg;
return (T)recvOperation.Msg;
}


Expand All @@ -137,7 +137,7 @@ protected object RecvCore()
{
try
{
return RecvCoreAsync().Result;
return RecvCoreAsync<object>().Result;
}
catch (AggregateException aggregateException)
{
Expand Down
6 changes: 2 additions & 4 deletions src/GoSharp/Impl/SelectLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,8 @@ internal async Task GoAsync()

foreach (var channelOperation in _channelOperations)
{
if (channelOperation is RecvChannelOperation)
if (channelOperation is RecvChannelOperation recvChannelOperation)
{
var recvChannelOperation = (RecvChannelOperation)channelOperation;
var channel = channelOperation.Channel;

if (channel.ReceiveFast(recvChannelOperation, UnlockAllChannels))
Expand Down Expand Up @@ -94,9 +93,8 @@ internal async Task GoAsync()
if (channelOperation.Channel.IsClosed)
continue;

if (channelOperation is RecvChannelOperation)
if (channelOperation is RecvChannelOperation recvChannelOperation)
{
var recvChannelOperation = (RecvChannelOperation)channelOperation;
var channel = channelOperation.Channel;

channel.Enqueue(recvChannelOperation, _selectFireContext);
Expand Down
12 changes: 12 additions & 0 deletions usetest/NetCore11/NetCore11.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="GoSharp" Version="0.4.0" />
</ItemGroup>

</Project>
17 changes: 17 additions & 0 deletions usetest/NetCore11/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using GoSharp;
using System;

namespace NetCore12
{
class Program
{
static void Main(string[] args)
{
var chan = Channel<int>.CreateBuffered(1);

chan.Send(1);

Console.WriteLine(chan.Recv());
}
}
}
13 changes: 13 additions & 0 deletions usetest/NetCore11/Properties/PublishProfiles/FolderProfile.pubxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
This file is used by the publish/package process of your project. You can customize the behavior of this process
by editing this MSBuild file. In order to learn more about this please visit https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<PublishProtocol>FileSystem</PublishProtocol>
<Configuration>Release</Configuration>
<TargetFramework>netcoreapp1.1</TargetFramework>
<PublishDir>bin\Release\PublishOutput</PublishDir>
</PropertyGroup>
</Project>
12 changes: 12 additions & 0 deletions usetest/NetCore2/NetCore2.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="GoSharp" Version="0.4.0" />
</ItemGroup>

</Project>
17 changes: 17 additions & 0 deletions usetest/NetCore2/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using GoSharp;
using System;

namespace NetCore2
{
class Program
{
static void Main(string[] args)
{
var chan = Channel<int>.CreateBuffered(1);

chan.Send(1);

Console.WriteLine(chan.Recv());
}
}
}
13 changes: 13 additions & 0 deletions usetest/NetCore2/Properties/PublishProfiles/FolderProfile.pubxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
This file is used by the publish/package process of your project. You can customize the behavior of this process
by editing this MSBuild file. In order to learn more about this please visit https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<PublishProtocol>FileSystem</PublishProtocol>
<Configuration>Release</Configuration>
<TargetFramework>netcoreapp2.0</TargetFramework>
<PublishDir>bin\Release\PublishOutput</PublishDir>
</PropertyGroup>
</Project>
6 changes: 6 additions & 0 deletions usetest/NetFw/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>
Loading

0 comments on commit a27d687

Please sign in to comment.