Skip to content

Commit a36a375

Browse files
author
Petr Sramek
committed
huge update. should have used different branch for experimenting :)
1 parent a32a8f4 commit a36a375

32 files changed

+1133
-558
lines changed

PolylineAlgorithm.sln

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "benchmarks", "benchmarks",
1414
EndProject
1515
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PolylineAlgorithm.Benchmarks", "benchmarks\PolylineAlgorithm.Benchmarks\PolylineAlgorithm.Benchmarks.csproj", "{9C7CBAD5-415B-4589-86E1-01C849F9C56C}"
1616
EndProject
17+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{02EA681E-C7D8-13C7-8484-4AC65E1B71E8}"
18+
EndProject
19+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PolylineAlgorithm.IO.Pipelines", "src\PolylineAlgorithm.IO.Pipelines\PolylineAlgorithm.IO.Pipelines.csproj", "{AC2D1BF7-B70C-461A-92FB-2C1E50D6C5FB}"
20+
EndProject
1721
Global
1822
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1923
Debug|Any CPU = Debug|Any CPU
@@ -32,6 +36,10 @@ Global
3236
{9C7CBAD5-415B-4589-86E1-01C849F9C56C}.Debug|Any CPU.Build.0 = Debug|Any CPU
3337
{9C7CBAD5-415B-4589-86E1-01C849F9C56C}.Release|Any CPU.ActiveCfg = Release|Any CPU
3438
{9C7CBAD5-415B-4589-86E1-01C849F9C56C}.Release|Any CPU.Build.0 = Release|Any CPU
39+
{AC2D1BF7-B70C-461A-92FB-2C1E50D6C5FB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
40+
{AC2D1BF7-B70C-461A-92FB-2C1E50D6C5FB}.Debug|Any CPU.Build.0 = Debug|Any CPU
41+
{AC2D1BF7-B70C-461A-92FB-2C1E50D6C5FB}.Release|Any CPU.ActiveCfg = Release|Any CPU
42+
{AC2D1BF7-B70C-461A-92FB-2C1E50D6C5FB}.Release|Any CPU.Build.0 = Release|Any CPU
3543
EndGlobalSection
3644
GlobalSection(SolutionProperties) = preSolution
3745
HideSolutionNode = FALSE
@@ -40,6 +48,7 @@ Global
4048
{882322A6-E758-4662-8D1C-7C555C8FC3F2} = {51C886AF-D610-48A4-9D73-2DEB38742801}
4149
{30324A08-AA42-425D-87DA-8F9C6AF60454} = {576FEFFC-B624-40C3-A8AF-4E5233802EA0}
4250
{9C7CBAD5-415B-4589-86E1-01C849F9C56C} = {33C03F16-4313-4579-87E6-65892AF21D7D}
51+
{AC2D1BF7-B70C-461A-92FB-2C1E50D6C5FB} = {51C886AF-D610-48A4-9D73-2DEB38742801}
4352
EndGlobalSection
4453
GlobalSection(ExtensibilityGlobals) = postSolution
4554
SolutionGuid = {93A268DC-0947-4FBB-B495-DDAD4B013D82}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
//namespace PolylineAlgorithm.Benchmarks {
2+
3+
// using BenchmarkDotNet.Attributes;
4+
// using PolylineAlgorithm.Benchmarks.Internal;
5+
// using System;
6+
// using System.Buffers;
7+
// using System.Collections.Generic;
8+
// using System.IO;
9+
// using System.Text.Json;
10+
// using System.Text.Json.Serialization.Metadata;
11+
12+
// [RankColumn]
13+
// [ShortRunJob]
14+
// public class AsyncReadWriteBenchmark {
15+
// [Params(/*1, 10, 100, 1_000, 10_000, 100_000, */1_000_000)]
16+
// public int N;
17+
18+
// public string Directory { get; set; }
19+
20+
// public Polyline Polyline { get; private set; }
21+
22+
// public IEnumerable<Coordinate> BlockingEnumeration { get; private set; }
23+
24+
// public IAsyncEnumerable<Coordinate> AsyncEnumeration { get; private set; }
25+
26+
// /// <summary>
27+
// /// The async polyline encoder instance.
28+
// /// </summary>
29+
// public AsyncPolylineEncoder AsyncEncoder = new();
30+
31+
// /// <summary>
32+
// /// The async polyline decoder instance.
33+
// /// </summary>
34+
// public AsyncPolylineDecoder AsyncDecoder = new();
35+
36+
37+
// /// <summary>
38+
// /// The async polyline encoder instance.
39+
// /// </summary>
40+
// public PolylineEncoder Encoder = new();
41+
42+
// /// <summary>
43+
// /// The async polyline decoder instance.
44+
// /// </summary>
45+
// public PolylineDecoder Decoder = new();
46+
47+
// /// <summary>
48+
// /// Sets up the data for the benchmarks.
49+
// /// </summary>
50+
// [GlobalSetup]
51+
// public void SetupData() {
52+
// Directory = $"C:\\temp\\benchmark\\{Guid.NewGuid()}";
53+
// System.IO.Directory.CreateDirectory(Directory);
54+
// WriteToFile(ValueProvider.GetCoordinates(N), $"{Directory}\\{N}-coordinates-original.json");
55+
// WriteToFile(ValueProvider.GetPolyline(N), $"{Directory}\\{N}-polyline-original.txt");
56+
57+
// BlockingEnumeration = ValueProvider.GetCoordinates(N);
58+
// AsyncEnumeration = GetCoordinates();
59+
// }
60+
61+
// /// <summary>
62+
// /// Benchmarks the encoding of a list of coordinates into a polyline.
63+
// /// </summary>
64+
// /// <returns>The encoded polyline.</returns>
65+
// [Benchmark]
66+
// public async Task ReadCoordinates_WritePolyline_Async() {
67+
// using StreamWriter writer = new($"{Directory}\\{N}-polyline-asynx-{Guid.NewGuid()}.txt");
68+
69+
// await foreach (var polyline in AsyncEncoder.EncodeAsync(GetCoordinates()).ConfigureAwait(false)) {
70+
// await writer
71+
// .WriteAsync(polyline.AsMemory());
72+
// await writer
73+
// .FlushAsync();
74+
// }
75+
// }
76+
77+
// /// <summary>
78+
// /// Benchmarks the encoding of a list of coordinates into a polyline.
79+
// /// </summary>
80+
// /// <returns>The encoded polyline.</returns>
81+
// [Benchmark]
82+
// public async Task ReadCoordinates_WritePolyline_Sync() {
83+
// using StreamWriter writer = new($"{Directory}\\{N}-polyline-blocking-{Guid.NewGuid()}.txt");
84+
85+
// var polyline = Encoder.Encode(BlockingEnumeration);
86+
87+
// await writer
88+
// .WriteAsync(polyline.AsMemory());
89+
// await writer
90+
// .FlushAsync();
91+
// }
92+
93+
// private void WriteToFile(IEnumerable<Coordinate> coordinates, string filename) {
94+
// using var file = File.OpenWrite(filename);
95+
96+
// file.SetLength(0);
97+
// file.Flush();
98+
99+
// JsonSerializer.Serialize(file, coordinates);
100+
// }
101+
102+
// private void WriteToFile(Polyline polyline, string filename) {
103+
// using var file = File.OpenWrite(filename);
104+
105+
// file.SetLength(0);
106+
// file.Flush();
107+
108+
// using StreamWriter writer = new(file);
109+
110+
// var reader = new SequenceReader<char>(new(polyline.AsMemory()));
111+
112+
// long index = 0;
113+
114+
// while (reader.TryRead(out char value) && index < reader.Length) {
115+
// writer.Write(value);
116+
// index++;
117+
// }
118+
119+
// writer.Flush();
120+
// file.Flush();
121+
// }
122+
123+
// private IAsyncEnumerable<Coordinate> GetCoordinates() {
124+
// var file = File.OpenRead($"{Directory}\\{N}-coordinates-original.json");
125+
// return JsonSerializer.DeserializeAsyncEnumerable<Coordinate>(file);
126+
// }
127+
// }
128+
//}

benchmarks/PolylineAlgorithm.Benchmarks/PolylineAlgorithm.Benchmarks.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
</PropertyGroup>
1616

1717
<ItemGroup>
18-
<PackageReference Include="BenchmarkDotNet" Version="0.*" />
19-
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="9.0.2" />
18+
<PackageReference Include="BenchmarkDotNet" Version="0.13.12" />
2019
</ItemGroup>
2120

2221
<ItemGroup>

benchmarks/PolylineAlgorithm.Benchmarks/PolylineDecoderBenchmark.cs

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ namespace PolylineAlgorithm.Benchmarks;
99
using BenchmarkDotNet.Engines;
1010
using PolylineAlgorithm;
1111
using PolylineAlgorithm.Benchmarks.Internal;
12-
using System.Diagnostics;
1312

1413
/// <summary>
1514
/// Benchmarks for the <see cref="PolylineDecoder"/> class.
1615
/// </summary>
1716
[RankColumn]
17+
[ShortRunJob]
1818
public class PolylineDecoderBenchmark {
1919
private readonly Consumer _consumer = new();
2020

@@ -46,7 +46,7 @@ public class PolylineDecoderBenchmark {
4646
/// <summary>
4747
/// The async polyline decoder instance.
4848
/// </summary>
49-
public AsyncPolylineDecoder AsyncDecoder = new();
49+
//public AsyncPolylineDecoder AsyncDecoder = new();
5050

5151
/// <summary>
5252
/// Sets up the data for the benchmarks.
@@ -94,45 +94,45 @@ public void PolylineDecoder_Decode_FromMemory() {
9494
.Consume(_consumer);
9595
}
9696

97-
/// <summary>
98-
/// Benchmarks the decoding of a polyline from read-only memory.
99-
/// </summary>
100-
[Benchmark]
101-
public async Task PolylineDecoder_DecodeAsync_FromString() {
102-
Polyline polyline = Polyline.FromString(StringValue);
103-
104-
var result = AsyncDecoder
105-
.DecodeAsync(polyline)
106-
.ConfigureAwait(false);
107-
108-
await foreach (var _ in result) { }
109-
}
110-
111-
/// <summary>
112-
/// Benchmarks the decoding of a polyline from read-only memory.
113-
/// </summary>
114-
[Benchmark]
115-
public async Task PolylineDecoder_DecodeAsync_CharArray() {
116-
Polyline polyline = Polyline.FromCharArray(CharArray);
117-
118-
var result = AsyncDecoder
119-
.DecodeAsync(polyline)
120-
.ConfigureAwait(false);
121-
122-
await foreach (var _ in result) { }
123-
}
124-
125-
/// <summary>
126-
/// Benchmarks the decoding of a polyline from read-only memory.
127-
/// </summary>
128-
[Benchmark]
129-
public async Task PolylineDecoder_DecodeAsync_FromMemory() {
130-
Polyline polyline = Polyline.FromMemory(Memory);
131-
132-
var result = AsyncDecoder
133-
.DecodeAsync(polyline)
134-
.ConfigureAwait(false);
135-
136-
await foreach (var _ in result) { }
137-
}
97+
///// <summary>
98+
///// Benchmarks the decoding of a polyline from read-only memory.
99+
///// </summary>
100+
//[Benchmark]
101+
//public async Task PolylineDecoder_DecodeAsync_FromString() {
102+
// Polyline polyline = Polyline.FromString(StringValue);
103+
104+
// var result = AsyncDecoder
105+
// .DecodeAsync(polyline)
106+
// .ConfigureAwait(false);
107+
108+
// await foreach (var _ in result) { }
109+
//}
110+
111+
///// <summary>
112+
///// Benchmarks the decoding of a polyline from read-only memory.
113+
///// </summary>
114+
//[Benchmark]
115+
//public async Task PolylineDecoder_DecodeAsync_CharArray() {
116+
// Polyline polyline = Polyline.FromCharArray(CharArray);
117+
118+
// var result = AsyncDecoder
119+
// .DecodeAsync(polyline)
120+
// .ConfigureAwait(false);
121+
122+
// await foreach (var _ in result) { }
123+
//}
124+
125+
///// <summary>
126+
///// Benchmarks the decoding of a polyline from read-only memory.
127+
///// </summary>
128+
//[Benchmark]
129+
//public async Task PolylineDecoder_DecodeAsync_FromMemory() {
130+
// Polyline polyline = Polyline.FromMemory(Memory);
131+
132+
// var result = AsyncDecoder
133+
// .DecodeAsync(polyline)
134+
// .ConfigureAwait(false);
135+
136+
// await foreach (var _ in result) { }
137+
//}
138138
}

benchmarks/PolylineAlgorithm.Benchmarks/PolylineEncoderBenchmark.cs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace PolylineAlgorithm.Benchmarks;
1515
/// Benchmarks for the <see cref="PolylineEncoder"/> class.
1616
/// </summary>
1717
[RankColumn]
18+
[ShortRunJob]
1819
public class PolylineEncoderBenchmark {
1920
[Params(1, 10, 100, 1_000, 10_000, 100_000, 1_000_000)]
2021
public int N;
@@ -41,15 +42,15 @@ public class PolylineEncoderBenchmark {
4142
/// <summary>
4243
/// The async polyline encoder instance.
4344
/// </summary>
44-
public AsyncPolylineEncoder AsyncEncoder = new();
45+
//public AsyncPolylineEncoder AsyncEncoder = new();
4546

4647
/// <summary>
4748
/// Sets up the data for the benchmarks.
4849
/// </summary>
4950
[GlobalSetup]
5051
public void SetupData() {
5152
Enumeration = ValueProvider.GetCoordinates(N);
52-
List = [.. Enumeration];
53+
List = [..Enumeration];
5354
AsyncEnumeration = GetAsyncEnumeration(Enumeration!);
5455
}
5556

@@ -65,7 +66,8 @@ private async IAsyncEnumerable<Coordinate> GetAsyncEnumeration(IEnumerable<Coord
6566
/// <returns>The encoded polyline.</returns>
6667
[Benchmark]
6768
public Polyline PolylineEncoder_Encode_List() {
68-
var polyline = Encoder.Encode(List!);
69+
var polyline = Encoder
70+
.Encode(List!);
6971

7072
return polyline;
7173
}
@@ -76,7 +78,8 @@ public Polyline PolylineEncoder_Encode_List() {
7678
/// <returns>The encoded polyline.</returns>
7779
[Benchmark]
7880
public Polyline PolylineEncoder_Encode_Enumerator() {
79-
var polyline = Encoder.Encode(Enumeration!);
81+
var polyline = Encoder
82+
.Encode(Enumeration!);
8083

8184
return polyline;
8285
}
@@ -85,17 +88,12 @@ public Polyline PolylineEncoder_Encode_Enumerator() {
8588
/// Benchmarks the encoding of an enumeration of coordinates into a polyline.
8689
/// </summary>
8790
/// <returns>The encoded polyline.</returns>
88-
[Benchmark]
89-
public async Task<Polyline> PolylineEncoder_EncodeAsync_String() {
90-
var result = AsyncEncoder
91-
.EncodeAsync(AsyncEnumeration!);
92-
93-
var polyline = new Polyline();
94-
95-
await foreach (var item in result.ConfigureAwait(false)) {
96-
polyline.Append(item);
97-
}
98-
99-
return polyline;
100-
}
91+
//[Benchmark]
92+
//public async Task<Polyline> PolylineEncoder_EncodeAsync_String() {
93+
// var polyline = await AsyncEncoder
94+
// .EncodeAsync(AsyncEnumeration!)
95+
// .ConfigureAwait(false);
96+
97+
// return polyline;
98+
//}
10199
}

benchmarks/PolylineAlgorithm.Benchmarks/Program.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ internal class Program {
1717
/// </summary>
1818
/// <param name="args">The command-line arguments.</param>
1919
static void Main(string[] args) {
20+
Directory.Delete("C:\\temp\\benchmark", true);
21+
2022
BenchmarkSwitcher
2123
.FromAssembly(typeof(Program).Assembly)
22-
.Run(args, DefaultConfig.Instance);
24+
.Run(args, new DebugInProcessConfig());
2325
}
2426
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.IO.Pipelines;
2+
3+
namespace PolylineAlgorithm.IO.Pipelines;
4+
5+
public abstract class CoordinateFormatter
6+
{
7+
public abstract bool TryRead(PipeReader reader, out Coordinate coordinate);
8+
9+
public abstract bool TryWrite(PipeWriter writer, Coordinate coordinate);
10+
11+
public abstract Task<bool> TryReadAsync(PipeReader reader, out Coordinate coordinate, CancellationToken cancellationToken = default);
12+
13+
public abstract Task<bool> TryWriteAsync(PipeWriter writer, Coordinate coordinate, CancellationToken cancellationToken = default);
14+
}

0 commit comments

Comments
 (0)