Skip to content

Commit 0cb85c7

Browse files
committed
optimiyed
1 parent c516e56 commit 0cb85c7

10 files changed

Lines changed: 85 additions & 156 deletions

File tree

benchmarks/PolylineAlgorithm.Benchmarks/PolylineAlgorithm.Benchmarks.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,19 @@
1010
<NeutralLanguage>en</NeutralLanguage>
1111
</PropertyGroup>
1212

13+
<PropertyGroup>
14+
<DebugType>pdbonly</DebugType>
15+
<DebugSymbols>true</DebugSymbols>
16+
</PropertyGroup>
17+
1318
<PropertyGroup>
1419
<IsPackable>false</IsPackable>
1520
</PropertyGroup>
1621

1722
<ItemGroup>
1823
<PackageReference Include="BenchmarkDotNet" Version="0.15.*" />
24+
<PackageReference Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.15.8" />
25+
<PackageReference Include="Microsoft.VisualStudio.DiagnosticsHub.BenchmarkDotNetDiagnosers" Version="18.3.36812.1" />
1926
</ItemGroup>
2027

2128
<ItemGroup>

benchmarks/PolylineAlgorithm.Benchmarks/PolylineBenchmark.cs

Lines changed: 26 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace PolylineAlgorithm.Benchmarks;
77

88
using BenchmarkDotNet.Attributes;
99
using BenchmarkDotNet.Engines;
10+
using Microsoft.VSDiagnostics;
1011
using PolylineAlgorithm;
1112
using PolylineAlgorithm.Utility;
1213

@@ -19,9 +20,6 @@ public class PolylineBenchmark {
1920
[Params(1, 100, 1_000)]
2021
public int Count;
2122

22-
[Params(100)]
23-
public int Iterations;
24-
2523
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
2624
/// <summary>
2725
/// Gets the character array representing the encoded polyline.
@@ -73,12 +71,10 @@ public void SetupData() {
7371
/// <returns>The encoded polyline.</returns>
7472
[Benchmark]
7573
public void Polyline_FromString() {
76-
for (int i = 0; i < Iterations; i++) {
77-
var polyline = Polyline
78-
.FromString(StringValue);
74+
var polyline = Polyline
75+
.FromString(StringValue);
7976

80-
_consumer.Consume(polyline);
81-
}
77+
_consumer.Consume(polyline);
8278
}
8379

8480
/// <summary>
@@ -87,12 +83,10 @@ public void Polyline_FromString() {
8783
/// <returns>The encoded polyline.</returns>
8884
[Benchmark]
8985
public void Polyline_FromCharArray() {
90-
for (int i = 0; i < Iterations; i++) {
91-
var polyline = Polyline
92-
.FromCharArray(CharArrayValue);
86+
var polyline = Polyline
87+
.FromCharArray(CharArrayValue);
9388

94-
_consumer.Consume(polyline);
95-
}
89+
_consumer.Consume(polyline);
9690
}
9791

9892
/// <summary>
@@ -101,12 +95,10 @@ public void Polyline_FromCharArray() {
10195
/// <returns>The encoded polyline.</returns>
10296
[Benchmark]
10397
public void Polyline_FromMemory() {
104-
for (int i = 0; i < Iterations; i++) {
105-
var polyline = Polyline
106-
.FromMemory(MemoryValue);
98+
var polyline = Polyline
99+
.FromMemory(MemoryValue);
107100

108-
_consumer.Consume(polyline);
109-
}
101+
_consumer.Consume(polyline);
110102
}
111103

112104
/// <summary>
@@ -115,12 +107,10 @@ public void Polyline_FromMemory() {
115107
/// <returns>The encoded polyline.</returns>
116108
[Benchmark]
117109
public void Polyline_ToString() {
118-
for (int i = 0; i < Iterations; i++) {
119-
var stringValue = PolylineValue
120-
.ToString();
110+
var stringValue = PolylineValue
111+
.ToString();
121112

122-
_consumer.Consume(stringValue);
123-
}
113+
_consumer.Consume(stringValue);
124114
}
125115

126116

@@ -130,13 +120,11 @@ public void Polyline_ToString() {
130120
/// <returns>The encoded polyline.</returns>
131121
[Benchmark]
132122
public void Polyline_CopyTo() {
133-
for (int i = 0; i < Iterations; i++) {
134-
PolylineValue
135-
.CopyTo(CopyToDestination);
123+
PolylineValue
124+
.CopyTo(CopyToDestination);
136125

137-
CopyToDestination
138-
.Consume(_consumer);
139-
}
126+
CopyToDestination
127+
.Consume(_consumer);
140128
}
141129

142130
/// <summary>
@@ -145,12 +133,10 @@ public void Polyline_CopyTo() {
145133
/// <returns>The encoded polyline.</returns>
146134
[Benchmark]
147135
public void Polyline_Equals_SameValue() {
148-
for (int i = 0; i < Iterations; i++) {
149-
var equals = PolylineValue
150-
.Equals(PolylineValue);
136+
var equals = PolylineValue
137+
.Equals(PolylineValue);
151138

152-
_consumer.Consume(equals);
153-
}
139+
_consumer.Consume(equals);
154140
}
155141

156142
/// <summary>
@@ -159,12 +145,10 @@ public void Polyline_Equals_SameValue() {
159145
/// <returns>The encoded polyline.</returns>
160146
[Benchmark]
161147
public void Polyline_Equals_DifferentValue() {
162-
for (int i = 0; i < Iterations; i++) {
163-
var equals = PolylineValue
164-
.Equals(PolylineNotEqualValue);
148+
var equals = PolylineValue
149+
.Equals(PolylineNotEqualValue);
165150

166-
_consumer.Consume(equals);
167-
}
151+
_consumer.Consume(equals);
168152
}
169153

170154

@@ -174,11 +158,9 @@ public void Polyline_Equals_DifferentValue() {
174158
/// <returns>The encoded polyline.</returns>
175159
[Benchmark]
176160
public void Polyline_Equals_DifferentType() {
177-
for (int i = 0; i < Iterations; i++) {
178-
var equals = PolylineValue
179-
.Equals(StringValue);
161+
var equals = PolylineValue
162+
.Equals(StringValue);
180163

181-
_consumer.Consume(equals);
182-
}
164+
_consumer.Consume(equals);
183165
}
184166
}

benchmarks/PolylineAlgorithm.Benchmarks/PolylineDecoderBenchmark.cs

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace PolylineAlgorithm.Benchmarks;
77

88
using BenchmarkDotNet.Attributes;
99
using BenchmarkDotNet.Engines;
10+
using Microsoft.VSDiagnostics;
1011
using PolylineAlgorithm;
1112
using PolylineAlgorithm.Extensions;
1213
using PolylineAlgorithm.Utility;
@@ -20,9 +21,6 @@ public class PolylineDecoderBenchmark {
2021
[Params(1, 100, 1_000)]
2122
public int Count;
2223

23-
[Params(100)]
24-
public int Iterations;
25-
2624
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
2725
/// <summary>
2826
/// Gets the string value representing the encoded polyline.
@@ -67,46 +65,38 @@ public void SetupData() {
6765
/// </summary>
6866
[Benchmark]
6967
public void PolylineDecoder_Decode_Polyline() {
70-
for (int i = 0; i < Iterations; i++) {
71-
Decoder
72-
.Decode(Polyline)
73-
.Consume(_consumer);
74-
}
68+
Decoder
69+
.Decode(Polyline)
70+
.Consume(_consumer);
7571
}
7672

7773
/// <summary>
7874
/// Benchmarks the decoding of a polyline from a string.
7975
/// </summary>
8076
[Benchmark]
8177
public void PolylineDecoder_Decode_String() {
82-
for (int i = 0; i < Iterations; i++) {
83-
Decoder
84-
.Decode(String)
85-
.Consume(_consumer);
86-
}
78+
Decoder
79+
.Decode(String)
80+
.Consume(_consumer);
8781
}
8882

8983
/// <summary>
9084
/// Benchmarks the decoding of a polyline from a string.
9185
/// </summary>
9286
[Benchmark]
9387
public void PolylineDecoder_Decode_CharArray() {
94-
for (int i = 0; i < Iterations; i++) {
95-
Decoder
96-
.Decode(CharArray)
97-
.Consume(_consumer);
98-
}
88+
Decoder
89+
.Decode(CharArray)
90+
.Consume(_consumer);
9991
}
10092

10193
/// <summary>
10294
/// Benchmarks the decoding of a polyline from a string.
10395
/// </summary>
10496
[Benchmark]
10597
public void PolylineDecoder_Decode_Memory() {
106-
for (int i = 0; i < Iterations; i++) {
107-
Decoder
108-
.Decode(Memory)
109-
.Consume(_consumer);
110-
}
98+
Decoder
99+
.Decode(Memory)
100+
.Consume(_consumer);
111101
}
112102
}

benchmarks/PolylineAlgorithm.Benchmarks/PolylineEncoderBenchmark.cs

Lines changed: 13 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace PolylineAlgorithm.Benchmarks;
77

88
using BenchmarkDotNet.Attributes;
99
using BenchmarkDotNet.Engines;
10+
using Microsoft.VSDiagnostics;
1011
using PolylineAlgorithm;
1112
using PolylineAlgorithm.Extensions;
1213
using PolylineAlgorithm.Utility;
@@ -21,15 +22,7 @@ public class PolylineEncoderBenchmark {
2122
[Params(1, 100, 1_000)]
2223
public int Count;
2324

24-
[Params(100)]
25-
public int Iterations;
26-
2725
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
28-
/// <summary>
29-
/// Gets the enumeration of coordinates to be encoded.
30-
/// </summary>
31-
public IEnumerable<Coordinate> Enumeration { get; private set; }
32-
3326
/// <summary>
3427
/// Gets the list of coordinates to be encoded.
3528
/// </summary>
@@ -57,10 +50,9 @@ public class PolylineEncoderBenchmark {
5750
/// </summary>
5851
[GlobalSetup]
5952
public void SetupData() {
60-
Enumeration = RandomValueProvider.GetCoordinates(Count).Select(c => new Coordinate(c.Latitude, c.Longitude));
61-
List = [.. Enumeration];
62-
Array = [.. Enumeration];
63-
Memory = Array.AsMemory();
53+
List = RandomValueProvider.GetCoordinates(Count).Select(c => new Coordinate(c.Latitude, c.Longitude)).ToList();
54+
Array = [.. List];
55+
Memory = Array.AsMemory();
6456
}
6557

6658
/// <summary>
@@ -69,12 +61,10 @@ public void SetupData() {
6961
/// <returns>The encoded polyline.</returns>
7062
[Benchmark]
7163
public void PolylineEncoder_Encode_Span() {
72-
for (int i = 0; i < Iterations; i++) {
73-
var polyline = Encoder
74-
.Encode(Memory.Span!);
64+
var polyline = Encoder
65+
.Encode(Memory.Span!);
7566

76-
_consumer.Consume(polyline);
77-
}
67+
_consumer.Consume(polyline);
7868
}
7969

8070
/// <summary>
@@ -83,12 +73,10 @@ public void PolylineEncoder_Encode_Span() {
8373
/// <returns>The encoded polyline.</returns>
8474
[Benchmark]
8575
public void PolylineEncoder_Encode_Array() {
86-
for (int i = 0; i < Iterations; i++) {
87-
var polyline = Encoder
88-
.Encode(Array!);
76+
var polyline = Encoder
77+
.Encode(Array!);
8978

90-
_consumer.Consume(polyline);
91-
}
79+
_consumer.Consume(polyline);
9280
}
9381

9482
/// <summary>
@@ -97,25 +85,9 @@ public void PolylineEncoder_Encode_Array() {
9785
/// <returns>The encoded polyline.</returns>
9886
[Benchmark]
9987
public void PolylineEncoder_Encode_List() {
100-
for (int i = 0; i < Iterations; i++) {
101-
var polyline = Encoder
102-
.Encode(List!);
103-
104-
_consumer.Consume(polyline);
105-
}
106-
}
107-
108-
/// <summary>
109-
/// Benchmarks the encoding of an enumeration of coordinates into a polyline.
110-
/// </summary>
111-
/// <returns>The encoded polyline.</returns>
112-
[Benchmark]
113-
public void PolylineEncoder_Encode_Enumerator() {
114-
for (int i = 0; i < Iterations; i++) {
115-
var polyline = Encoder
116-
.Encode(Enumeration!);
88+
var polyline = Encoder
89+
.Encode(List!);
11790

118-
_consumer.Consume(polyline);
119-
}
91+
_consumer.Consume(polyline);
12092
}
12193
}

benchmarks/PolylineAlgorithm.Benchmarks/Program.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
namespace PolylineAlgorithm.Benchmarks;
77

8+
using BenchmarkDotNet.Configs;
9+
using BenchmarkDotNet.Diagnosers;
10+
using BenchmarkDotNet.Diagnostics.Windows;
811
using BenchmarkDotNet.Running;
912

1013
/// <summary>

src/PolylineAlgorithm/Abstraction/AbstractPolylineDecoder.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace PolylineAlgorithm.Abstraction;
1111
using PolylineAlgorithm.Internal.Logging;
1212
using PolylineAlgorithm.Properties;
1313
using System;
14+
using System.Runtime.CompilerServices;
1415

1516
/// <summary>
1617
/// Decodes encoded polyline strings into sequences of geographic coordinates.
@@ -104,7 +105,7 @@ public IEnumerable<TCoordinate> Decode(TPolyline polyline) {
104105
logger
105106
.LogOperationFinishedInfo(nameof(Decode));
106107

107-
108+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
108109
static void ValidateNullPolyline(TPolyline polyline, ILogger logger) {
109110
if (polyline is null) {
110111
logger
@@ -114,6 +115,7 @@ static void ValidateNullPolyline(TPolyline polyline, ILogger logger) {
114115
}
115116
}
116117

118+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
117119
static void ValidateEmptySequence(ILogger logger, ReadOnlyMemory<char> sequence) {
118120
if (sequence.Length < Defaults.Polyline.Block.Length.Min) {
119121
logger
@@ -135,6 +137,7 @@ static void ValidateEmptySequence(ILogger logger, ReadOnlyMemory<char> sequence)
135137
/// <returns>
136138
/// A <see cref="ReadOnlyMemory{T}"/> representing the encoded polyline data.
137139
/// </returns>
140+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
138141
protected abstract ReadOnlyMemory<char> GetReadOnlyMemory(TPolyline polyline);
139142

140143
/// <summary>
@@ -149,5 +152,6 @@ static void ValidateEmptySequence(ILogger logger, ReadOnlyMemory<char> sequence)
149152
/// <returns>
150153
/// A coordinate instance of type <typeparamref name="TCoordinate"/>.
151154
/// </returns>
155+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
152156
protected abstract TCoordinate CreateCoordinate(double latitude, double longitude);
153157
}

0 commit comments

Comments
 (0)