@@ -7,8 +7,7 @@ namespace PolylineAlgorithm.Benchmarks;
77
88using BenchmarkDotNet . Attributes ;
99using BenchmarkDotNet . Engines ;
10- using PolylineAlgorithm . Gps ;
11- using PolylineAlgorithm . Gps . Extensions ;
10+ using PolylineAlgorithm . Abstraction ;
1211using PolylineAlgorithm . Utility ;
1312
1413/// <summary>
@@ -21,11 +20,6 @@ public class PolylineDecoderBenchmark {
2120 public int CoordinatesCount { get ; set ; }
2221
2322#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.
24- /// <summary>
25- /// Polyline instance for benchmarks.
26- /// </summary>
27- public Polyline Polyline { get ; private set ; }
28-
2923 /// <summary>
3024 /// Encoded polyline as string.
3125 /// </summary>
@@ -44,37 +38,36 @@ public class PolylineDecoderBenchmark {
4438#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.
4539
4640 /// <summary>
47- /// Polyline decoder instance.
41+ /// String polyline decoder instance.
42+ /// </summary>
43+ private readonly StringPolylineDecoder _stringDecoder = new ( ) ;
44+
45+ /// <summary>
46+ /// Char array polyline decoder instance.
4847 /// </summary>
49- private readonly PolylineDecoder _decoder = new ( ) ;
48+ private readonly CharArrayPolylineDecoder _charArrayDecoder = new ( ) ;
49+
50+ /// <summary>
51+ /// String polyline decoder instance.
52+ /// </summary>
53+ private readonly MemoryCharPolylineDecoder _memoryCharDecoder = new ( ) ;
5054
5155 /// <summary>
5256 /// Sets up benchmark data.
5357 /// </summary>
5458 [ GlobalSetup ]
5559 public void SetupData ( ) {
56- Polyline = Polyline . FromString ( RandomValueProvider . GetPolyline ( CoordinatesCount ) ) ;
5760 String = RandomValueProvider . GetPolyline ( CoordinatesCount ) ;
5861 CharArray = RandomValueProvider . GetPolyline ( CoordinatesCount ) . ToCharArray ( ) ;
5962 Memory = RandomValueProvider . GetPolyline ( CoordinatesCount ) . AsMemory ( ) ;
6063 }
6164
62- /// <summary>
63- /// Benchmark: decode polyline instance.
64- /// </summary>
65- [ Benchmark ]
66- public void PolylineDecoder_Decode_Polyline ( ) {
67- _decoder
68- . Decode ( Polyline )
69- . Consume ( _consumer ) ;
70- }
71-
7265 /// <summary>
7366 /// Benchmark: decode from string.
7467 /// </summary>
7568 [ Benchmark ]
7669 public void PolylineDecoder_Decode_String ( ) {
77- _decoder
70+ _stringDecoder
7871 . Decode ( String )
7972 . Consume ( _consumer ) ;
8073 }
@@ -84,7 +77,7 @@ public void PolylineDecoder_Decode_String() {
8477 /// </summary>
8578 [ Benchmark ]
8679 public void PolylineDecoder_Decode_CharArray ( ) {
87- _decoder
80+ _charArrayDecoder
8881 . Decode ( CharArray )
8982 . Consume ( _consumer ) ;
9083 }
@@ -94,8 +87,38 @@ public void PolylineDecoder_Decode_CharArray() {
9487 /// </summary>
9588 [ Benchmark ]
9689 public void PolylineDecoder_Decode_Memory ( ) {
97- _decoder
90+ _memoryCharDecoder
9891 . Decode ( Memory )
9992 . Consume ( _consumer ) ;
10093 }
94+
95+ private sealed class StringPolylineDecoder : AbstractPolylineDecoder < string , ( double Latitude , double Longitude ) > {
96+ protected override ( double Latitude , double Longitude ) CreateCoordinate ( double latitude , double longitude ) {
97+ return ( latitude , longitude ) ;
98+ }
99+
100+ protected override ReadOnlyMemory < char > GetReadOnlyMemory ( in string polyline ) {
101+ return polyline ? . AsMemory ( ) ?? Memory < char > . Empty ;
102+ }
103+ }
104+
105+ private sealed class CharArrayPolylineDecoder : AbstractPolylineDecoder < char [ ] , ( double Latitude , double Longitude ) > {
106+ protected override ( double Latitude , double Longitude ) CreateCoordinate ( double latitude , double longitude ) {
107+ return ( latitude , longitude ) ;
108+ }
109+
110+ protected override ReadOnlyMemory < char > GetReadOnlyMemory ( in char [ ] polyline ) {
111+ return polyline ? . AsMemory ( ) ?? Memory < char > . Empty ;
112+ }
113+ }
114+
115+ private sealed class MemoryCharPolylineDecoder : AbstractPolylineDecoder < ReadOnlyMemory < char > , ( double Latitude , double Longitude ) > {
116+ protected override ( double Latitude , double Longitude ) CreateCoordinate ( double latitude , double longitude ) {
117+ return ( latitude , longitude ) ;
118+ }
119+
120+ protected override ReadOnlyMemory < char > GetReadOnlyMemory ( in ReadOnlyMemory < char > polyline ) {
121+ return polyline ;
122+ }
123+ }
101124}
0 commit comments