Skip to content

Commit de17325

Browse files
authored
Merge pull request #5 from itn3000/spanify
support netstandard2.1 and adding azure pipelines file
2 parents ff32928 + f2e8a45 commit de17325

20 files changed

+634
-199
lines changed

.config/dotnet-tools.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": 1,
3+
"isRoot": true,
4+
"tools": {
5+
"cake.tool": {
6+
"version": "0.35.0",
7+
"commands": [
8+
"dotnet-cake"
9+
]
10+
}
11+
}
12+
}

.editorconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[*.cs]
2+
indent_size = 4
3+
indent_style = space
4+
trim_trailing_whitespace = true

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,4 +278,6 @@ __pycache__/
278278
# !tools/packages.config
279279

280280
.vscode/
281-
BenchmarkDotNet.Artifacts/
281+
BenchmarkDotNet.Artifacts/
282+
tools/
283+
*.sln
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System;
2+
using System.IO;
3+
using BenchmarkDotNet.Attributes;
4+
using Microsoft.Extensions.ObjectPool;
5+
using System.Buffers;
6+
using System.Linq;
7+
8+
namespace PooledStream.Benchmark
9+
{
10+
[Config(typeof(MultiPlatformConfig))]
11+
[MemoryDiagnoser]
12+
[DisassemblyDiagnoser(printAsm: true, printIL: true, printSource: true)]
13+
public class ObjectPoolStreamBench
14+
{
15+
[Params(10000)]
16+
public int DataLength;
17+
[Params(1000)]
18+
public int LoopNum;
19+
[Benchmark(Baseline = true)]
20+
public void Normal()
21+
{
22+
var data = new byte[DataLength];
23+
for (int i = 0; i < LoopNum; i++)
24+
{
25+
using (var mstm = new PooledMemoryStream(ArrayPool<byte>.Shared, DataLength))
26+
{
27+
#if NETCOREAPP3_0
28+
mstm.Write(data.AsSpan());
29+
#else
30+
mstm.Write(data, 0, data.Length);
31+
#endif
32+
}
33+
}
34+
}
35+
readonly ObjectPool<PooledMemoryStream> _Pool = ObjectPool.Create<PooledMemoryStream>();
36+
[Benchmark]
37+
public void Pooled()
38+
{
39+
var data = new byte[DataLength];
40+
for (int i = 0; i < LoopNum; i++)
41+
{
42+
var mstm = _Pool.Get();
43+
{
44+
// mstm.Reserve(DataLength);
45+
mstm.SetLength(0);
46+
// #if NETCOREAPP3_0
47+
// mstm.Write(data.AsSpan());
48+
// #else
49+
mstm.Write(data, 0, data.Length);
50+
// #endif
51+
_Pool.Return(mstm);
52+
}
53+
}
54+
}
55+
}
56+
57+
}

PooledStream.Benchmark/PooledStream.Benchmark.csproj

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<OutputType>Exe</OutputType>
4-
<TargetFrameworks>netcoreapp2.0;netcoreapp2.1</TargetFrameworks>
4+
<TargetFrameworks>netcoreapp3.0;netcoreapp2.1</TargetFrameworks>
5+
<PlatformTarget>AnyCPU</PlatformTarget>
6+
<DebugType>pdbonly</DebugType>
7+
<DebugSymbols>true</DebugSymbols>
8+
<IsPackable>false</IsPackable>
59
</PropertyGroup>
610
<ItemGroup>
7-
<PackageReference Include="BenchmarkDotNet" Version="0.10.14" />
11+
<PackageReference Include="BenchmarkDotNet" Version="0.12.0" />
812
<PackageReference Include="CodeProject.ObjectPool" Version="3.2.2" />
913
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" Version="1.2.2" />
14+
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="3.0.0"/>
1015
</ItemGroup>
1116
<ItemGroup>
1217
<ProjectReference Include="..\PooledStream\PooledStream.csproj" />

PooledStream.Benchmark/Program.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace PooledStream.Benchmark
1414
using ObjectMemoryStream = CodeProject.ObjectPool.Specialized.MemoryStreamPool;
1515
[MemoryDiagnoser]
1616
[Config(typeof(MultiPlatformConfig))]
17+
// [DisassemblyDiagnoser(printIL: true, printSource: true, printAsm: true)]
1718
public class StreamBenchmark
1819
{
1920
[Params(100, 1_000, 50_000)]
@@ -40,9 +41,10 @@ public void PooledStreamBench()
4041
{
4142
using (var stm = new PooledMemoryStream(ArrayPool<byte>.Shared, DataSize))
4243
{
43-
stm.Write(data, 0, data.Length);
44+
stm.Write(data, 0, DataSize);
4445
}
4546
}
47+
// ArrayPool<byte>.Shared.Return(data);
4648
}
4749
[Benchmark]
4850
public void RecyclableStreamTest()
@@ -75,22 +77,18 @@ class MultiPlatformConfig : ManualConfig
7577
{
7678
public MultiPlatformConfig()
7779
{
78-
Add(Job.Default.WithWarmupCount(3).WithTargetCount(3)
79-
.With(CsProjCoreToolchain.NetCoreApp20));
80-
Add(Job.Default.WithWarmupCount(3).WithTargetCount(3)
80+
Add(Job.Default.WithWarmupCount(3).WithIterationCount(3)
8181
.With(CsProjCoreToolchain.NetCoreApp21));
82+
Add(Job.Default.WithWarmupCount(3).WithIterationCount(3)
83+
.With(CsProjCoreToolchain.NetCoreApp30));
84+
this.Options |= ConfigOptions.DisableOptimizationsValidator;
8285
}
8386
}
8487
class Program
8588
{
8689
static void Main(string[] args)
8790
{
88-
var switcher = new BenchmarkSwitcher(new Type[]
89-
{
90-
typeof(StreamBenchmark),
91-
typeof(StreamPrallelBenchmark)
92-
}
93-
);
91+
var switcher = new BenchmarkSwitcher(typeof(Program).Assembly);
9492
switcher.Run();
9593
}
9694
}
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp2.0</TargetFramework>
4+
<TargetFrameworks>netcoreapp2.1;netcoreapp3.0</TargetFrameworks>
55

66
<IsPackable>false</IsPackable>
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0-preview-20170427-09" />
11-
<PackageReference Include="xunit" Version="2.2.0" />
12-
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
13-
<ProjectReference Include="../PooledStream/PooledStream.csproj"/>
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />
11+
<PackageReference Include="xunit" Version="2.4.1" />
12+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
13+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
14+
<PrivateAssets>all</PrivateAssets>
15+
</PackageReference>
16+
<ProjectReference Include="../PooledStream/PooledStream.csproj" />
1417
</ItemGroup>
1518

1619
</Project>

PooledStream.Test/TestPooledMemoryStream.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,37 @@ public void TestSetLength()
7272
Assert.Equal(data, ar.Take(data.Length));
7373
}
7474
}
75+
[Fact]
76+
public void TestDispose()
77+
{
78+
var stm = new PooledMemoryStream();
79+
stm.Write(new byte[]{ 1, 2, 3, 4 }, 0, 4);
80+
stm.Dispose();
81+
stm.Write(new byte[]{ 1, 2, 3, 4 }, 0, 4);
82+
stm.Dispose();
83+
}
84+
[Fact]
85+
public void TestToMemory()
86+
{
87+
var data = new byte[] { 1, 2, 3, 4 };
88+
using(var stm = new PooledMemoryStream(data))
89+
{
90+
var mem = stm.ToMemoryUnsafe();
91+
Assert.Equal(data, mem);
92+
var sp = stm.ToSpanUnsafe();
93+
Assert.True(sp.SequenceEqual(data));
94+
}
95+
}
96+
[Fact]
97+
public void TestShrink()
98+
{
99+
var data = new byte[] { 1, 2, 3, 4 };
100+
using(var stm = new PooledMemoryStream())
101+
{
102+
stm.Write(data, 0, data.Length);
103+
stm.Shrink(2);
104+
Assert.Equal(data.AsSpan(0, 2).ToArray(), stm.ToArray());
105+
}
106+
}
75107
}
76108
}

PooledStream.sln

Lines changed: 0 additions & 62 deletions
This file was deleted.

0 commit comments

Comments
 (0)