@@ -21,12 +21,83 @@ public class DataTypeSerialization
21
21
public virtual void SetUp ( ) { }
22
22
}
23
23
24
- public class DateTypeArraySerialization : DataTypeSerialization
24
+ public class DataTypeFieldSerialization : DataTypeSerialization
25
25
{
26
- readonly List < object > _emptyArray = new List < object > ( ) ;
27
- Memory < byte > _emptyArrayBuffer ;
28
- Memory < byte > _populatedArrayBuffer ;
29
- List < object > _array ;
26
+ private readonly object _intObject = 5 ;
27
+ private readonly string _shortString = new string ( 'x' , 100 ) ;
28
+ private readonly byte [ ] _byteArray = new byte [ 0 ] ;
29
+ private readonly Dictionary < string , object > _emptyDictionary = new Dictionary < string , object > ( ) ;
30
+ private readonly BinaryTableValue _binaryTableValue = new BinaryTableValue ( new byte [ 0 ] ) ;
31
+
32
+ private Memory < byte > _fieldStringBuffer ;
33
+ private Memory < byte > _fieldIntBuffer ;
34
+ private Memory < byte > _fieldNullBuffer ;
35
+ private Memory < byte > _fieldArrayBuffer ;
36
+ private Memory < byte > _fieldDictBuffer ;
37
+ private Memory < byte > _fieldBinaryTableValueBuffer ;
38
+
39
+ public override void SetUp ( )
40
+ {
41
+ _fieldNullBuffer = new byte [ WireFormatting . GetFieldValueByteCount ( null ) ] ;
42
+ WireFormatting . WriteFieldValue ( _fieldNullBuffer . Span , null ) ;
43
+ _fieldIntBuffer = new byte [ WireFormatting . GetFieldValueByteCount ( _intObject ) ] ;
44
+ WireFormatting . WriteFieldValue ( _fieldIntBuffer . Span , _intObject ) ;
45
+ _fieldStringBuffer = new byte [ WireFormatting . GetFieldValueByteCount ( _shortString ) ] ;
46
+ WireFormatting . WriteFieldValue ( _fieldStringBuffer . Span , _shortString ) ;
47
+ _fieldArrayBuffer = new byte [ WireFormatting . GetFieldValueByteCount ( _byteArray ) ] ;
48
+ WireFormatting . WriteFieldValue ( _fieldArrayBuffer . Span , _byteArray ) ;
49
+ _fieldDictBuffer = new byte [ WireFormatting . GetFieldValueByteCount ( _emptyDictionary ) ] ;
50
+ WireFormatting . WriteFieldValue ( _fieldDictBuffer . Span , _emptyDictionary ) ;
51
+ _fieldBinaryTableValueBuffer = new byte [ WireFormatting . GetFieldValueByteCount ( _binaryTableValue ) ] ;
52
+ WireFormatting . WriteFieldValue ( _fieldBinaryTableValueBuffer . Span , _binaryTableValue ) ;
53
+ }
54
+
55
+ [ Benchmark ]
56
+ public object NullRead ( ) => WireFormatting . ReadFieldValue ( _fieldNullBuffer . Span , out int _ ) ;
57
+ [ Benchmark ]
58
+ public object IntRead ( ) => WireFormatting . ReadFieldValue ( _fieldIntBuffer . Span , out int _ ) ;
59
+ [ Benchmark ]
60
+ public object StringRead ( ) => WireFormatting . ReadFieldValue ( _fieldStringBuffer . Span , out int _ ) ;
61
+ [ Benchmark ]
62
+ public object ArrayRead ( ) => WireFormatting . ReadFieldValue ( _fieldArrayBuffer . Span , out int _ ) ;
63
+ [ Benchmark ]
64
+ public object DictRead ( ) => WireFormatting . ReadFieldValue ( _fieldDictBuffer . Span , out int _ ) ;
65
+ [ Benchmark ]
66
+ public object BinaryTableValueRead ( ) => WireFormatting . ReadFieldValue ( _fieldBinaryTableValueBuffer . Span , out int _ ) ;
67
+
68
+ [ Benchmark ]
69
+ public int NullWrite ( ) => WireFormatting . WriteFieldValue ( _buffer . Span , null ) ;
70
+ [ Benchmark ]
71
+ public int IntWrite ( ) => WireFormatting . WriteFieldValue ( _buffer . Span , _intObject ) ;
72
+ [ Benchmark ]
73
+ public int StringWrite ( ) => WireFormatting . WriteFieldValue ( _buffer . Span , _shortString ) ;
74
+ [ Benchmark ]
75
+ public int ArrayWrite ( ) => WireFormatting . WriteFieldValue ( _buffer . Span , _byteArray ) ;
76
+ [ Benchmark ]
77
+ public int DictWrite ( ) => WireFormatting . WriteFieldValue ( _buffer . Span , _emptyDictionary ) ;
78
+ [ Benchmark ]
79
+ public int BinaryTableValueWrite ( ) => WireFormatting . WriteFieldValue ( _buffer . Span , _binaryTableValue ) ;
80
+
81
+ [ Benchmark ]
82
+ public int NullGetSize ( ) => WireFormatting . GetFieldValueByteCount ( null ) ;
83
+ [ Benchmark ]
84
+ public int IntGetSize ( ) => WireFormatting . GetFieldValueByteCount ( _intObject ) ;
85
+ [ Benchmark ]
86
+ public int StringGetSize ( ) => WireFormatting . GetFieldValueByteCount ( _shortString ) ;
87
+ [ Benchmark ]
88
+ public int ArrayGetSize ( ) => WireFormatting . GetFieldValueByteCount ( _byteArray ) ;
89
+ [ Benchmark ]
90
+ public int DictGetSize ( ) => WireFormatting . GetFieldValueByteCount ( _emptyDictionary ) ;
91
+ [ Benchmark ]
92
+ public int BinaryTableValueGetSize ( ) => WireFormatting . GetFieldValueByteCount ( _binaryTableValue ) ;
93
+ }
94
+
95
+ public class DataTypeArraySerialization : DataTypeSerialization
96
+ {
97
+ private readonly List < object > _emptyArray = new List < object > ( ) ;
98
+ private Memory < byte > _emptyArrayBuffer ;
99
+ private Memory < byte > _populatedArrayBuffer ;
100
+ private List < object > _array ;
30
101
31
102
public override void SetUp ( )
32
103
{
@@ -49,14 +120,20 @@ public override void SetUp()
49
120
50
121
[ Benchmark ]
51
122
public int ArrayWritePopulated ( ) => WireFormatting . WriteArray ( _buffer . Span , _array ) ;
123
+
124
+ [ Benchmark ]
125
+ public int ArrayGetSizeEmpty ( ) => WireFormatting . GetArrayByteCount ( _emptyArray ) ;
126
+
127
+ [ Benchmark ]
128
+ public int ArrayGetSizePopulated ( ) => WireFormatting . GetArrayByteCount ( _array ) ;
52
129
}
53
130
54
131
public class DataTypeTableSerialization : DataTypeSerialization
55
132
{
56
- IDictionary < string , object > _emptyDict = new Dictionary < string , object > ( ) ;
57
- IDictionary < string , object > _populatedDict ;
58
- Memory < byte > _emptyDictionaryBuffer ;
59
- Memory < byte > _populatedDictionaryBuffer ;
133
+ private IDictionary < string , object > _emptyDict = new Dictionary < string , object > ( ) ;
134
+ private IDictionary < string , object > _populatedDict ;
135
+ private Memory < byte > _emptyDictionaryBuffer ;
136
+ private Memory < byte > _populatedDictionaryBuffer ;
60
137
61
138
public override void SetUp ( )
62
139
{
@@ -67,8 +144,8 @@ public override void SetUp()
67
144
{ "uint" , 1234u } ,
68
145
{ "decimal" , 12.34m } ,
69
146
{ "timestamp" , _timestamp } ,
70
- { "fieldtable" , new Dictionary < string , object > ( ) { { "test" , "test" } } } ,
71
- { "fieldarray" , new List < object > { "longstring" , 1234 , 12.34m , _timestamp } }
147
+ { "fieldtable" , new Dictionary < string , object > { { "test" , "test" } } } , //190 / +70
148
+ { "fieldarray" , new List < object > { "longstring" , 1234 , 12.34m , _timestamp } } //183 / +77
72
149
} ;
73
150
74
151
_emptyDictionaryBuffer = new byte [ WireFormatting . GetTableByteCount ( _emptyDict ) ] ;
@@ -89,13 +166,19 @@ public override void SetUp()
89
166
90
167
[ Benchmark ]
91
168
public int TableWritePopulated ( ) => WireFormatting . WriteTable ( _buffer . Span , _populatedDict ) ;
169
+
170
+ [ Benchmark ]
171
+ public int TableGetSizeEmpty ( ) => WireFormatting . GetTableByteCount ( _emptyDict ) ;
172
+
173
+ [ Benchmark ]
174
+ public int TableGetSizePopulated ( ) => WireFormatting . GetTableByteCount ( _populatedDict ) ;
92
175
}
93
176
94
177
public class DataTypeLongStringSerialization : DataTypeSerialization
95
178
{
96
- readonly string _longString = new string ( 'X' , 4096 ) ;
97
- readonly Memory < byte > _emptyLongStringBuffer = GenerateLongStringBuffer ( string . Empty ) ;
98
- readonly Memory < byte > _populatedLongStringBuffer = GenerateLongStringBuffer ( new string ( 'X' , 4096 ) ) ;
179
+ private readonly string _longString = new string ( 'X' , 4096 ) ;
180
+ private readonly Memory < byte > _emptyLongStringBuffer = GenerateLongStringBuffer ( string . Empty ) ;
181
+ private readonly Memory < byte > _populatedLongStringBuffer = GenerateLongStringBuffer ( new string ( 'X' , 4096 ) ) ;
99
182
100
183
[ Benchmark ]
101
184
public int LongstrReadEmpty ( ) => WireFormatting . ReadLongstr ( _emptyLongStringBuffer . Span , out _ ) ;
@@ -109,6 +192,12 @@ public class DataTypeLongStringSerialization : DataTypeSerialization
109
192
[ Benchmark ]
110
193
public int LongstrWritePopulated ( ) => WireFormatting . WriteLongstr ( _buffer . Span , _longString ) ;
111
194
195
+ [ Benchmark ]
196
+ public int LongstrGetSizeEmpty ( ) => WireFormatting . GetFieldValueByteCount ( string . Empty ) ;
197
+
198
+ [ Benchmark ]
199
+ public int LongstrGetSizePopulated ( ) => WireFormatting . GetFieldValueByteCount ( _longString ) ;
200
+
112
201
private static byte [ ] GenerateLongStringBuffer ( string val )
113
202
{
114
203
byte [ ] _buffer = new byte [ 5 + Encoding . UTF8 . GetByteCount ( val ) ] ;
@@ -119,9 +208,9 @@ private static byte[] GenerateLongStringBuffer(string val)
119
208
120
209
public class DataTypeShortStringSerialization : DataTypeSerialization
121
210
{
122
- readonly string _shortString = new string ( 'X' , 255 ) ;
123
- readonly Memory < byte > _emptyShortStringBuffer = GenerateStringBuffer ( string . Empty ) ;
124
- readonly Memory < byte > _populatedShortStringBuffer = GenerateStringBuffer ( new string ( 'X' , 255 ) ) ;
211
+ private readonly string _shortString = new string ( 'X' , 255 ) ;
212
+ private readonly Memory < byte > _emptyShortStringBuffer = GenerateStringBuffer ( string . Empty ) ;
213
+ private readonly Memory < byte > _populatedShortStringBuffer = GenerateStringBuffer ( new string ( 'X' , 255 ) ) ;
125
214
126
215
[ Benchmark ]
127
216
public int ShortstrReadEmpty ( ) => WireFormatting . ReadShortstr ( _emptyShortStringBuffer . Span , out _ ) ;
@@ -135,6 +224,12 @@ public class DataTypeShortStringSerialization : DataTypeSerialization
135
224
[ Benchmark ]
136
225
public int ShortstrWritePopulated ( ) => WireFormatting . WriteShortstr ( _buffer . Span , _shortString ) ;
137
226
227
+ [ Benchmark ]
228
+ public int ShortstrGetSizeEmpty ( ) => WireFormatting . GetByteCount ( string . Empty ) ;
229
+
230
+ [ Benchmark ]
231
+ public int ShortstrGetSizePopulated ( ) => WireFormatting . GetByteCount ( _shortString ) ;
232
+
138
233
private static byte [ ] GenerateStringBuffer ( string val )
139
234
{
140
235
byte [ ] _buffer = new byte [ 2 + Encoding . UTF8 . GetByteCount ( val ) ] ;
0 commit comments