forked from JamesNK/SerializationBenchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerializers.cs
237 lines (197 loc) · 7.85 KB
/
Serializers.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
using System;
using System.Buffers;
using System.Text;
using System.Text.Json;
using Google.Protobuf;
using K4os.Compression.LZ4;
using MessagePack;
using Newtonsoft.Json;
namespace SerializationBenchmark
{
internal static class Serializers
{
private static readonly BufferWriterPool<byte> _pool = new();
private static readonly MessagePackSerializerOptions _options = MessagePackSerializerOptions.Standard
.WithOmitAssemblyVersion(true);
private static readonly MessagePackSerializerOptions _compressingOptions = MessagePackSerializerOptions.Standard
.WithOmitAssemblyVersion(true)
.WithCompression(MessagePackCompression.Lz4BlockArray);
public static BufferWriter<byte> MessagePack_SerializePlain<T>(T data)
{
var br = _pool.Rent();
MessagePackSerializer.Serialize(br, data);
return br;
}
public static T MessagePack_DeserializePlain<T>(ReadOnlyMemory<byte> data)
{
return MessagePackSerializer.Deserialize<T>(data, _options);
}
public static BufferWriter<byte> MessagePack_SerializePickled<T>(T data)
{
var br = _pool.Rent();
var br2 = _pool.Rent();
MessagePackSerializer.Serialize(br, data, _options);
LZ4Pickler.Pickle(br.WrittenSpan, br2);
_pool.Return(br);
return br2;
}
public static T MessagePack_DeserializePickled<T>(ReadOnlySpan<byte> data)
{
var br = _pool.Rent();
LZ4Pickler.Unpickle(data, br);
var result = MessagePackSerializer.Deserialize<T>(br.WrittenMemory, _options);
_pool.Return(br);
return result;
}
public static BufferWriter<byte> MessagePack_SerializeCompressed<T>(T data)
{
var br = _pool.Rent();
MessagePackSerializer.Serialize(br, data, _compressingOptions);
return br;
}
public static T MessagePack_DeserializeCompressed<T>(ReadOnlyMemory<byte> data)
{
return MessagePackSerializer.Deserialize<T>(data, _compressingOptions);
}
public static BufferWriter<byte> ProtobufNet_SerializePlain<T>(T data)
{
var br = _pool.Rent();
ProtoBuf.Serializer.Serialize(br, data);
return br;
}
public static T ProtobufNet_DeserializePlain<T>(ReadOnlySpan<byte> data)
{
return ProtoBuf.Serializer.Deserialize<T>(data);
}
public static BufferWriter<byte> ProtobufNet_SerializePickled<T>(T data)
{
var br = _pool.Rent();
var br2 = _pool.Rent();
ProtoBuf.Serializer.Serialize(br, data);
LZ4Pickler.Pickle(br.WrittenSpan, br2);
_pool.Return(br);
return br2;
}
public static T ProtobufNet_DeserializePickled<T>(ReadOnlySpan<byte> data)
{
var br = _pool.Rent();
LZ4Pickler.Unpickle(data, br);
var result = ProtoBuf.Serializer.Deserialize<T>(br.WrittenMemory);
_pool.Return(br);
return result;
}
public static BufferWriter<byte> Protobuf_SerializePlain(IBufferMessage data)
{
var br = _pool.Rent();
data.WriteTo(br);
return br;
}
// Supplies type-specific MessageParser<T> instances without the need for dictionary lookups.
private static class Parser<T>
where T : IMessage<T>, new()
{
public static MessageParser<T> Instance { get; } = new MessageParser<T>(() => new T());
}
// Supplies type-specific MessageParser instances without the need for dictionar lookups.
// This is needed for cases where T is not known statically to be an IMessage.
private static class ParserHolder<T>
where T : notnull, new()
{
static ParserHolder()
{
var constructed = typeof(MessageParser<>).MakeGenericType(new[] { typeof(T) });
Func<T> factory = () => new T();
var inst = Activator.CreateInstance(constructed, new object[] { factory }) as MessageParser;
Instance = inst!;
}
public static MessageParser Instance { get; private set; }
}
public static T Protobuf_DeserializePlain<T>(ReadOnlyMemory<byte> data, IMessage target)
where T : IMessage<T>, new()
{
return Parser<T>.Instance.ParseFrom(new ReadOnlySequence<byte>(data));
}
public static BufferWriter<byte> Protobuf_SerializePickled(IBufferMessage data)
{
var br = _pool.Rent();
var br2 = _pool.Rent();
data.WriteTo(br);
LZ4Pickler.Pickle(br.WrittenSpan, br2);
_pool.Return(br);
return br2;
}
public static T Protobuf_DeserializePickled<T>(ReadOnlySpan<byte> data, IMessage target)
where T : IMessage<T>, new()
{
var br = _pool.Rent();
LZ4Pickler.Unpickle(data, br);
var result = Parser<T>.Instance.ParseFrom(new ReadOnlySequence<byte>(br.WrittenMemory));
_pool.Return(br);
return result;
}
public static byte[] SystemTextJson_SerializePlain<T>(T data)
{
return System.Text.Json.JsonSerializer.SerializeToUtf8Bytes<T>(data);
}
public static T SystemTextJson_DeserializePlain<T>(ReadOnlySpan<byte> data)
{
return System.Text.Json.JsonSerializer.Deserialize<T>(data)!;
}
public static BufferWriter<byte> SystemTextJson_SerializePickled<T>(T data)
{
var br = _pool.Rent();
var br2 = _pool.Rent();
System.Text.Json.JsonSerializer.Serialize(new Utf8JsonWriter(br), data);
LZ4Pickler.Pickle(br.WrittenSpan, br2);
_pool.Return(br);
return br2;
}
public static T SystemTextJson_DeserializedPickled<T>(ReadOnlySpan<byte> data)
{
var br = _pool.Rent();
LZ4Pickler.Unpickle(data, br);
var result = System.Text.Json.JsonSerializer.Deserialize<T>(br.WrittenSpan)!;
_pool.Return(br);
return result;
}
public static BufferWriter<byte> Newtonsoft_SerializePlain<T>(T data)
{
var br = _pool.Rent();
var ser = JsonConvert.SerializeObject(data);
var count = UTF8Encoding.UTF8.GetByteCount(ser);
var span = br.GetSpan(count);
UTF8Encoding.UTF8.GetBytes(ser, span);
br.Advance(count);
return br;
}
public static T Newtonsoft_DeserializePlain<T>(ReadOnlySpan<byte> data)
{
return JsonConvert.DeserializeObject<T>(UTF8Encoding.UTF8.GetString(data))!;
}
public static BufferWriter<byte> Newtonsoft_SerializePickled<T>(T data)
{
var br = _pool.Rent();
var br2 = _pool.Rent();
var ser = JsonConvert.SerializeObject(data);
int count = UTF8Encoding.UTF8.GetByteCount(ser);
var span = br.GetSpan(count);
UTF8Encoding.UTF8.GetBytes(ser, span);
br.Advance(count);
LZ4Pickler.Pickle(br.WrittenSpan, br2);
_pool.Return(br);
return br2;
}
public static T Newtonsoft_DeserializedPickled<T>(ReadOnlySpan<byte> data)
{
var br = _pool.Rent();
LZ4Pickler.Unpickle(data, br);
var result = JsonConvert.DeserializeObject<T>(UTF8Encoding.UTF8.GetString(br.WrittenSpan))!;
_pool.Return(br);
return result;
}
public static void Return(BufferWriter<byte> writer)
{
_pool.Return(writer);
}
}
}