Skip to content

Commit 4c1ce5d

Browse files
author
Pete Sramek
committedApr 11, 2025·
updated encoding
1 parent b0d8b54 commit 4c1ce5d

File tree

12 files changed

+67
-46
lines changed

12 files changed

+67
-46
lines changed
 

‎benchmarks/PolylineAlgorithm.Benchmarks/PolylineBenchmark.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ public class PolylineBenchmark {
5656
/// </summary>
5757
[GlobalSetup]
5858
public void SetupData() {
59-
PolylineValue = ValueProvider.GetPolyline(Count);
60-
PolylineNotEqualValue = ValueProvider.GetPolyline(Count + Random.Shared.Next(1, 101));
59+
PolylineValue = RandomValueProvider.GetPolyline(Count);
60+
PolylineNotEqualValue = RandomValueProvider.GetPolyline(Count + Random.Shared.Next(1, 101));
6161
StringValue = PolylineValue.ToString();
6262
CharArrayValue = [.. StringValue];
6363
MemoryValue = CharArrayValue.AsMemory();

‎benchmarks/PolylineAlgorithm.Benchmarks/PolylineBuilderBenchmark.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public class PolylineBuilderBenchmark {
5757
public void SetupData() {
5858
Builder = new PolylineBuilder();
5959

60-
var polyline = ValueProvider.GetPolyline(Count);
60+
var polyline = RandomValueProvider.GetPolyline(Count);
6161
StringValue = polyline.ToString();
6262
CharArrayValue = [.. StringValue];
6363
MemoryValue = CharArrayValue.AsMemory();

‎benchmarks/PolylineAlgorithm.Benchmarks/PolylineDecoderBenchmark.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class PolylineDecoderBenchmark {
3939
/// </summary>
4040
[GlobalSetup]
4141
public void SetupData() {
42-
Polyline = ValueProvider.GetPolyline(Count);
42+
Polyline = RandomValueProvider.GetPolyline(Count);
4343
}
4444

4545
/// <summary>

‎benchmarks/PolylineAlgorithm.Benchmarks/PolylineEncoderBenchmark.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class PolylineEncoderBenchmark {
4646
/// </summary>
4747
[GlobalSetup]
4848
public void SetupData() {
49-
Enumeration = ValueProvider.GetCoordinates(Count);
49+
Enumeration = RandomValueProvider.GetCoordinates(Count);
5050
List = [.. Enumeration];
5151
}
5252

‎benchmarks/PolylineAlgorithm.Comparison.Benchmarks/PolylineDecoderBenchmark.cs

+23-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace PolylineAlgorithm.Comparison.Benchmarks;
1212
using PolylineAlgorithm.Utility;
1313
using PolylinerNet;
1414
using PolylineEncoding = Cloudikka.PolylineAlgorithm.Encoding.PolylineEncoding;
15+
using PolylineAlgorithm.Extensions;
1516

1617
/// <summary>
1718
/// Benchmarks for the <see cref="PolylineAlgorithm.PolylineDecoder"/> class.
@@ -29,10 +30,12 @@ public class PolylineDecoderBenchmark {
2930
/// </summary>
3031
public string StringValue { get; private set; }
3132

33+
public char[] CharArrayValue { get; private set; }
34+
3235
/// <summary>
3336
/// Gets the character array representing the encoded polyline.
3437
/// </summary>
35-
//public Polyline PolylineValue { get; private set; }
38+
public Polyline PolylineValue { get; private set; }
3639

3740
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
3841

@@ -52,17 +55,32 @@ public class PolylineDecoderBenchmark {
5255
/// </summary>
5356
[GlobalSetup]
5457
public void SetupData() {
55-
//PolylineValue = ValueProvider.GetPolyline(Count);
56-
StringValue = ValueProvider.GetPolyline(Count).ToString();
58+
PolylineValue = RandomValueProvider.GetPolyline(Count);
59+
StringValue = PolylineValue.ToString();
60+
CharArrayValue = StringValue.ToCharArray();
5761
}
5862

5963
/// <summary>
6064
/// Benchmarks the decoding of a polyline from a string.
6165
/// </summary>
6266
[Benchmark(Baseline = true)]
63-
public void PolylineAlgorithm_Decode() {
67+
public void PolylineAlgorithm_Decode_String() {
68+
PolylineAlgorithm
69+
.Decode(StringValue)
70+
.Consume(_consumer);
71+
}
72+
73+
[Benchmark]
74+
public void PolylineAlgorithm_Decode_CharArray() {
75+
PolylineAlgorithm
76+
.Decode(CharArrayValue)
77+
.Consume(_consumer);
78+
}
79+
80+
[Benchmark]
81+
public void PolylineAlgorithm_Decode_Polyline() {
6482
PolylineAlgorithm
65-
.Decode(Polyline.FromString(StringValue))
83+
.Decode(PolylineValue)
6684
.Consume(_consumer);
6785
}
6886

‎benchmarks/PolylineAlgorithm.Comparison.Benchmarks/PolylineEncoderBenchmark.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public class PolylineEncoderBenchmark {
5656
/// </summary>
5757
[GlobalSetup]
5858
public void SetupData() {
59-
PolylineAlgorithmEnumeration = ValueProvider.GetCoordinates(Count);
59+
PolylineAlgorithmEnumeration = RandomValueProvider.GetCoordinates(Count);
6060
CloudikkaEnumeration = PolylineAlgorithmEnumeration.Select(c => (c.Latitude, c.Longitude));
6161
PolylinerEnumeration = PolylineAlgorithmEnumeration.Select(c => new PolylinePoint(c.Latitude, c.Longitude));
6262
PolylinesEnumeration = PolylineAlgorithmEnumeration.Select(c => new Polylines.PolylineCoordinate { Latitude = c.Latitude, Longitude = c.Longitude });

‎src/PolylineAlgorithm/PolylineEncoder.cs

+17-26
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ namespace PolylineAlgorithm;
99
using PolylineAlgorithm.Internal;
1010
using System;
1111
using System.Collections.Generic;
12-
using System.Diagnostics.CodeAnalysis;
1312
using System.Runtime.CompilerServices;
1413

1514
/// <summary>
@@ -43,18 +42,18 @@ public Polyline Encode(IEnumerable<Coordinate> coordinates) {
4342
int position = 0;
4443
int consumed = 0;
4544
int length = GetMaxLength(count);
46-
bool isMultiSegment = count == -1 || count > MaxCount;
45+
bool asMultiSegment = count == -1 || count > MaxCount;
4746
PolylineBuilder builder = new();
4847
Span<char> buffer = stackalloc char[length];
4948

5049
using var enumerator = coordinates.GetEnumerator();
5150

5251
while (enumerator.MoveNext()) {
5352
variance
54-
.Next(Normalize(enumerator.Current));
55-
56-
if (isMultiSegment
57-
&& buffer.Length - position < 12) {
53+
.Next((PolylineEncoding.Default.Normalize(enumerator.Current.Latitude), PolylineEncoding.Default.Normalize(enumerator.Current.Longitude)));
54+
55+
if (asMultiSegment
56+
&& buffer.Length - position < PolylineEncoding.Default.GetCharCount(variance.Latitude) + PolylineEncoding.Default.GetCharCount(variance.Longitude)) {
5857
builder
5958
.Append(buffer[..position].ToString().AsMemory());
6059

@@ -68,18 +67,11 @@ public Polyline Encode(IEnumerable<Coordinate> coordinates) {
6867
}
6968

7069
consumed++;
71-
72-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
73-
static (int Latitude, int Longitude) Normalize(Coordinate coordinate) {
74-
return (PolylineEncoding.Default.Normalize(coordinate.Latitude), PolylineEncoding.Default.Normalize(coordinate.Longitude));
75-
}
7670
}
7771

78-
#pragma warning disable CA1508 // Avoid dead conditional code
7972
if (consumed == 0) {
8073
return default;
8174
}
82-
#pragma warning restore CA1508 // Avoid dead conditional code
8375

8476
builder
8577
.Append(buffer[..position].ToString().AsMemory());
@@ -92,18 +84,17 @@ public Polyline Encode(IEnumerable<Coordinate> coordinates) {
9284
> 1 and < MaxCount => count * Defaults.Polyline.MaxEncodedCoordinateLength,
9385
_ => MaxChars
9486
};
95-
}
9687

97-
/// <summary>
98-
/// Gets the count of coordinates in the enumerable.
99-
/// </summary>
100-
/// <param name="coordinates">The enumerable of coordinates.</param>
101-
/// <returns>The count of coordinates.</returns>
102-
[ExcludeFromCodeCoverage]
103-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
104-
static int GetCount(IEnumerable<Coordinate> coordinates) => coordinates switch {
105-
ICollection<Coordinate> collection => collection.Count,
106-
IEnumerable<Coordinate> enumerable => enumerable.Count(),
107-
_ => -1,
108-
};
88+
/// <summary>
89+
/// Gets the count of coordinates in the enumerable.
90+
/// </summary>
91+
/// <param name="coordinates">The enumerable of coordinates.</param>
92+
/// <returns>The count of coordinates.</returns>
93+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
94+
static int GetCount(IEnumerable<Coordinate> coordinates) => coordinates switch {
95+
ICollection<Coordinate> collection => collection.Count,
96+
IEnumerable<Coordinate> enumerable => enumerable.Count(),
97+
_ => -1,
98+
};
99+
}
109100
}

‎tests/PolylineAlgorithm.Tests/PolylineDecoderTest.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ public void Decode_Empty_Input_ThrowsException() {
5757
[TestMethod]
5858
public void Decode_Valid_Input_Ok() {
5959
// Arrange
60-
IEnumerable<Coordinate> expected = ValueProvider.GetCoordinates(1);
61-
string value = ValueProvider.GetPolyline(1).ToString();
60+
IEnumerable<Coordinate> expected = RandomValueProvider.GetCoordinates(1);
61+
string value = RandomValueProvider.GetPolyline(1).ToString();
6262

6363
// Act
6464
var result = Decoder.Decode(value);

‎tests/PolylineAlgorithm.Tests/PolylineEncoderTest.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ void EncodeNullCoordinates() {
6262
[DynamicData(nameof(CoordinateCount))]
6363
public void Encode_ValidInput_Ok(int count) {
6464
// Arrange
65-
IEnumerable<Coordinate> valid = ValueProvider.GetCoordinates(count);
66-
Polyline expected = ValueProvider.GetPolyline(count);
65+
IEnumerable<Coordinate> valid = RandomValueProvider.GetCoordinates(count);
66+
Polyline expected = RandomValueProvider.GetPolyline(count);
6767

6868
// Act
6969
var result = Encoder.Encode(valid);

‎tests/PolylineAlgorithm.Tests/PolylineTest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void Constructor_Null_String_ArgumentNullException() {
6767
[DynamicData(nameof(LengthParameters))]
6868
public void Constructor_String_Parameter_Ok(int length) {
6969
// Arrange
70-
var value = ValueProvider.GetPolyline(length);
70+
var value = RandomValueProvider.GetPolyline(length);
7171

7272
// Act
7373
Polyline result = Polyline.FromString(value.ToString());

‎utilities/PolylineAlgorithm.Utility/PolylineAlgorithm.Utility.csproj

+15-3
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,20 @@
1919
<EnableNETAnalyzers>false</EnableNETAnalyzers>
2020
</PropertyGroup>
2121

22-
<ItemGroup>
23-
<ProjectReference Include="..\..\src\PolylineAlgorithm\PolylineAlgorithm.csproj" />
24-
</ItemGroup>
22+
<ItemGroup>
23+
<ProjectReference Include="..\..\src\PolylineAlgorithm\PolylineAlgorithm.csproj" />
24+
</ItemGroup>
25+
26+
<ItemGroup>
27+
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
28+
<_Parameter1>PolylineAlgorithm.Tests</_Parameter1>
29+
</AssemblyAttribute>
30+
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
31+
<_Parameter1>PolylineAlgorithm.Benchmarks</_Parameter1>
32+
</AssemblyAttribute>
33+
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
34+
<_Parameter1>PolylineAlgorithm.Comparison.Benchmarks</_Parameter1>
35+
</AssemblyAttribute>
36+
</ItemGroup>
2537

2638
</Project>

‎utilities/PolylineAlgorithm.Utility/ValueProvider.cs renamed to ‎utilities/PolylineAlgorithm.Utility/RandomValueProvider.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using System.Collections.Generic;
66
using System.Linq;
77

8-
public static class ValueProvider {
8+
internal static class RandomValueProvider {
99
private static readonly Random _random = new(DateTime.Now.Millisecond);
1010
private static readonly ConcurrentDictionary<int, CoordinatePair> _cache = new();
1111
private static readonly PolylineEncoder _encoder = new();

0 commit comments

Comments
 (0)
Please sign in to comment.