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
|
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
namespace Impostor.Benchmarks.Data
{
public class MessageReader_Bytes_Pooled
{
private static ConcurrentQueue<MessageReader_Bytes_Pooled> _readers;
static MessageReader_Bytes_Pooled()
{
var instances = new List<MessageReader_Bytes_Pooled>();
for (var i = 0; i < 10000; i++)
{
instances.Add(new MessageReader_Bytes_Pooled());
}
_readers = new ConcurrentQueue<MessageReader_Bytes_Pooled>(instances);
}
public byte Tag { get; set; }
public byte[] Buffer { get; set; }
public int Position { get; set; }
public int Length { get; set; }
public int BytesRemaining => this.Length - this.Position;
public void Update(byte[] buffer, int position = 0, int length = 0)
{
Tag = byte.MaxValue;
Buffer = buffer;
Position = position;
Length = length;
}
public void Update(byte tag, byte[] buffer, int position = 0, int length = 0)
{
Tag = tag;
Buffer = buffer;
Position = position;
Length = length;
}
public MessageReader_Bytes_Pooled ReadMessage()
{
var length = ReadUInt16();
var tag = FastByte();
var pos = Position;
Position += length;
if (!_readers.TryDequeue(out var result))
{
throw new Exception("Failed to get pooled instance");
}
result.Update(tag, Buffer, pos, length);
return result;
}
public bool ReadBoolean()
{
byte val = FastByte();
return val != 0;
}
public sbyte ReadSByte()
{
return (sbyte)FastByte();
}
public byte ReadByte()
{
return FastByte();
}
public ushort ReadUInt16()
{
return (ushort)(this.FastByte() |
this.FastByte() << 8);
}
public short ReadInt16()
{
return (short)(this.FastByte() |
this.FastByte() << 8);
}
public uint ReadUInt32()
{
return this.FastByte()
| (uint)this.FastByte() << 8
| (uint)this.FastByte() << 16
| (uint)this.FastByte() << 24;
}
public int ReadInt32()
{
return this.FastByte()
| this.FastByte() << 8
| this.FastByte() << 16
| this.FastByte() << 24;
}
public unsafe float ReadSingle()
{
float output = 0;
fixed (byte* bufPtr = &this.Buffer[Position])
{
byte* outPtr = (byte*)&output;
*outPtr = *bufPtr;
*(outPtr + 1) = *(bufPtr + 1);
*(outPtr + 2) = *(bufPtr + 2);
*(outPtr + 3) = *(bufPtr + 3);
}
this.Position += 4;
return output;
}
public string ReadString()
{
var len = this.ReadPackedInt32();
if (this.BytesRemaining < len)
{
throw new InvalidDataException($"Read length is longer than message length: {len} of {this.BytesRemaining}");
}
var output = Encoding.UTF8.GetString(this.Buffer, Position, len);
this.Position += len;
return output;
}
public Span<byte> ReadBytesAndSize()
{
var len = ReadPackedInt32();
return ReadBytes(len);
}
public Span<byte> ReadBytes(int length)
{
var output = Buffer.AsSpan(Position, length);
Position += length;
return output;
}
public int ReadPackedInt32()
{
return (int)ReadPackedUInt32();
}
public uint ReadPackedUInt32()
{
bool readMore = true;
int shift = 0;
uint output = 0;
while (readMore)
{
byte b = FastByte();
if (b >= 0x80)
{
readMore = true;
b ^= 0x80;
}
else
{
readMore = false;
}
output |= (uint)(b << shift);
shift += 7;
}
return output;
}
public MessageReader_Bytes_Pooled Slice(int start, int length)
{
if (!_readers.TryDequeue(out var result))
{
throw new Exception("Failed to get pooled instance");
}
result.Update(Tag, Buffer, start, length);
return result;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private byte FastByte()
{
return Buffer[Position++];
}
public static MessageReader_Bytes_Pooled Get(byte[] data)
{
if (!_readers.TryDequeue(out var result))
{
throw new Exception("Failed to get pooled instance");
}
result.Update(data);
return result;
}
public static void Return(MessageReader_Bytes_Pooled instance)
{
_readers.Enqueue(instance);
}
}
}
|