-
Notifications
You must be signed in to change notification settings - Fork 646
Expand file tree
/
Copy pathSessionParseState.cs
More file actions
448 lines (389 loc) · 15.4 KB
/
SessionParseState.cs
File metadata and controls
448 lines (389 loc) · 15.4 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Garnet.common;
using Garnet.common.Parsing;
using Tsavorite.core;
namespace Garnet.server
{
/// <summary>
/// Wrapper to hold parse state for a RESP session.
/// </summary>
public unsafe struct SessionParseState
{
/// <summary>
/// Initial number of arguments parsed for a command
/// </summary>
const int MinParams = 5; // 5 * 20 = 60; around one cache line of 64 bytes
/// <summary>
/// Count of accessible arguments for the command
/// </summary>
public int Count;
/// <summary>
/// Get the allocated capacity of the argument buffer
/// </summary>
public int Capacity { get; }
/// <summary>
/// Get a Span of the parsed parameters in the form an PinnedSpanByte
/// </summary>
public ReadOnlySpan<PinnedSpanByte> Parameters => new(bufferPtr, Count);
/// <summary>
/// Pointer to the slice of <see cref="rootBuffer"/> (which is always pinned) that is accessible within the range of this instance's arguments.
/// </summary>
PinnedSpanByte* bufferPtr;
/// <summary>
/// Count of arguments in the original buffer
/// </summary>
int rootCount;
/// <summary>
/// Arguments original buffer (always pinned)
/// </summary>
PinnedSpanByte[] rootBuffer;
private SessionParseState(ref PinnedSpanByte[] rootBuffer, int rootCount, ref PinnedSpanByte* bufferPtr, int count) : this()
{
this.rootBuffer = rootBuffer;
this.rootCount = rootCount;
this.bufferPtr = bufferPtr;
this.Count = count;
this.Capacity = rootBuffer.Length;
}
/// <summary>
/// Initialize the parse state at the start of a session
/// </summary>
public void Initialize()
{
Count = 0;
rootCount = 0;
rootBuffer = GC.AllocateArray<PinnedSpanByte>(MinParams, true);
bufferPtr = (PinnedSpanByte*)Unsafe.AsPointer(ref rootBuffer[0]);
}
/// <summary>
/// Initialize the parse state with a given count of arguments
/// </summary>
/// <param name="count">Size of argument array to allocate</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Initialize(int count)
{
Count = count;
rootCount = count;
if (rootBuffer != null && (count <= MinParams || count <= rootBuffer.Length))
return;
rootBuffer = GC.AllocateArray<PinnedSpanByte>(count <= MinParams ? MinParams : count, true);
bufferPtr = (PinnedSpanByte*)Unsafe.AsPointer(ref rootBuffer[0]);
}
/// <summary>
/// Initialize the parse state with one argument
/// </summary>
/// <param name="arg">Argument to initialize buffer with</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void InitializeWithArgument(PinnedSpanByte arg)
{
Initialize(1);
*bufferPtr = arg;
}
/// <summary>
/// Initialize the parse state with two arguments
/// </summary>
/// <param name="arg1">First argument</param>
/// <param name="arg2">Second argument</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void InitializeWithArguments(PinnedSpanByte arg1, PinnedSpanByte arg2)
{
Initialize(2);
*bufferPtr = arg1;
*(bufferPtr + 1) = arg2;
}
/// <summary>
/// Initialize the parse state with three arguments
/// </summary>
/// <param name="arg1">First argument</param>
/// <param name="arg2">Second argument</param>
/// <param name="arg3">Third argument</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void InitializeWithArguments(PinnedSpanByte arg1, PinnedSpanByte arg2, PinnedSpanByte arg3)
{
Initialize(3);
*bufferPtr = arg1;
*(bufferPtr + 1) = arg2;
*(bufferPtr + 2) = arg3;
}
/// <summary>
/// Initialize the parse state with four arguments
/// </summary>
/// <param name="arg1">First argument</param>
/// <param name="arg2">Second argument</param>
/// <param name="arg3">Third argument</param>
/// <param name="arg4">Fourth argument</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void InitializeWithArguments(PinnedSpanByte arg1, PinnedSpanByte arg2, PinnedSpanByte arg3, PinnedSpanByte arg4)
{
Initialize(4);
*bufferPtr = arg1;
*(bufferPtr + 1) = arg2;
*(bufferPtr + 2) = arg3;
*(bufferPtr + 3) = arg4;
}
/// <summary>
/// Initialize the parse state with four arguments
/// </summary>
/// <param name="arg1">First argument</param>
/// <param name="arg2">Second argument</param>
/// <param name="arg3">Third argument</param>
/// <param name="arg4">Fourth argument</param>
/// <param name="arg5">Fifth argument</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void InitializeWithArguments(PinnedSpanByte arg1, PinnedSpanByte arg2, PinnedSpanByte arg3, PinnedSpanByte arg4, PinnedSpanByte arg5)
{
Initialize(5);
*bufferPtr = arg1;
*(bufferPtr + 1) = arg2;
*(bufferPtr + 2) = arg3;
*(bufferPtr + 3) = arg4;
*(bufferPtr + 4) = arg5;
}
/// <summary>
/// Initialize the parse state with a given set of arguments
/// </summary>
/// <param name="args">Set of arguments to initialize buffer with</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void InitializeWithArguments(PinnedSpanByte[] args)
{
Initialize(args.Length);
for (var i = 0; i < args.Length; i++)
{
*(bufferPtr + i) = args[i];
}
}
/// <summary>
/// Limit access to the argument buffer to start at a specified index.
/// </summary>
/// <param name="idxOffset">Offset value to the underlying buffer</param>
public SessionParseState Slice(int idxOffset)
{
Debug.Assert(idxOffset - 1 < rootCount);
var count = rootCount - idxOffset;
var offsetBuffer = bufferPtr + idxOffset;
return new SessionParseState(ref rootBuffer, rootCount, ref offsetBuffer, count);
}
/// <summary>
/// Limit access to the argument buffer to start at a specified index
/// and end after a specified number of arguments.
/// </summary>
/// <param name="idxOffset">Offset value to the underlying buffer</param>
/// <param name="count">Argument count</param>
public SessionParseState Slice(int idxOffset, int count)
{
Debug.Assert(idxOffset + count - 1 < rootCount);
var offsetBuffer = bufferPtr + idxOffset;
return new SessionParseState(ref rootBuffer, rootCount, ref offsetBuffer, count);
}
/// <summary>
/// Initialize the parse state with a given set of arguments
/// </summary>
/// <param name="args">Set of arguments to initialize buffer with</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void InitializeWithArguments(ReadOnlySpan<PinnedSpanByte> args)
{
Initialize(args.Length);
for (var i = 0; i < args.Length; i++)
{
*(bufferPtr + i) = args[i];
}
}
/// <summary>
/// Set argument at a specific index
/// </summary>
/// <param name="i">Index of buffer at which to set argument</param>
/// <param name="arg">Argument to set</param>
public void SetArgument(int i, PinnedSpanByte arg)
{
Debug.Assert(i < Count);
*(bufferPtr + i) = arg;
}
/// <summary>
/// Set arguments starting at a specific index
/// </summary>
/// <param name="i">Index of buffer at which to start setting arguments</param>
/// <param name="args">Arguments to set</param>
public void SetArguments(int i, params ReadOnlySpan<PinnedSpanByte> args)
{
Debug.Assert(i + args.Length - 1 < Count);
for (var j = 0; j < args.Length; j++)
*(bufferPtr + i + j) = args[j];
}
/// <summary>
/// Get serialized length of parse state
/// </summary>
/// <returns>The serialized length</returns>
public int GetSerializedLength()
{
var serializedLength = sizeof(int);
for (var i = 0; i < Count; i++)
serializedLength += (*(bufferPtr + i)).TotalSize;
return serializedLength;
}
/// <summary>
/// Serialize parse state to memory buffer
/// when arguments are only serialized starting at a specified index
/// </summary>
/// <param name="dest">The memory buffer to serialize into (of size at least SerializedLength(firstIdx) bytes)</param>
/// <param name="length">Length of buffer to serialize into.</param>
/// <returns>Total serialized bytes</returns>
public int SerializeTo(byte* dest, int length)
{
var curr = dest;
// Serialize argument count
*(int*)curr = Count;
curr += sizeof(int);
// Serialize arguments
for (var i = 0; i < Count; i++)
{
var argument = *(bufferPtr + i);
argument.SerializeTo(curr);
curr += argument.TotalSize;
}
return (int)(dest - curr);
}
/// <summary>
/// Deserialize parse state from memory buffer into current struct
/// </summary>
/// <param name="src">Memory buffer to deserialize from</param>
/// <returns>Number of deserialized bytes</returns>
public unsafe int DeserializeFrom(byte* src)
{
var curr = src;
var argCount = *(int*)curr;
curr += sizeof(int);
Initialize(argCount);
for (var i = 0; i < argCount; i++)
{
var argument = PinnedSpanByte.FromLengthPrefixedPinnedPointer(curr);
*(bufferPtr + i) = argument;
curr += argument.TotalSize;
}
return (int)(src - curr);
}
/// <summary>
/// Read the next argument from the input buffer
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Read(int i, ref byte* ptr, byte* end)
{
Debug.Assert(i < Count);
ref var slice = ref Unsafe.AsRef<PinnedSpanByte>(bufferPtr + i);
// Parse RESP string header
if (!RespReadUtils.TryReadUnsignedLengthHeader(out var length, ref ptr, end))
return false;
slice.Set(ptr, length);
// Parse content: ensure that input contains key + '\r\n'
ptr += slice.Length + 2;
if (ptr > end)
return false;
if (*(ushort*)(ptr - 2) != MemoryMarshal.Read<ushort>("\r\n"u8))
RespParsingException.ThrowUnexpectedToken(*(ptr - 2));
return true;
}
/// <summary>
/// Get the argument at the given index
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ref PinnedSpanByte GetArgSliceByRef(int i)
{
Debug.Assert(i < Count);
return ref Unsafe.AsRef<PinnedSpanByte>(bufferPtr + i);
}
/// <summary>
/// Get int argument at the given index
/// </summary>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int GetInt(int i)
{
Debug.Assert(i < Count);
return ParseUtils.ReadInt(*(bufferPtr + i));
}
/// <summary>
/// Try to get int argument at the given index
/// </summary>
/// <returns>True if integer parsed successfully</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryGetInt(int i, out int value)
{
Debug.Assert(i < Count);
return ParseUtils.TryReadInt(*(bufferPtr + i), out value);
}
/// <summary>
/// Get long argument at the given index
/// </summary>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public long GetLong(int i)
{
Debug.Assert(i < Count);
return ParseUtils.ReadLong(*(bufferPtr + i));
}
/// <summary>
/// Try to get long argument at the given index
/// </summary>
/// <returns>True if long parsed successfully</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryGetLong(int i, out long value)
{
Debug.Assert(i < Count);
return ParseUtils.TryReadLong(*(bufferPtr + i), out value);
}
/// <summary>
/// Get double argument at the given index
/// </summary>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double GetDouble(int i, bool canBeInfinite = true)
{
Debug.Assert(i < Count);
return ParseUtils.ReadDouble(Unsafe.AsRef<PinnedSpanByte>(bufferPtr + i), canBeInfinite);
}
/// <summary>
/// Try to get double argument at the given index
/// </summary>
/// <returns>True if double parsed successfully</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryGetDouble(int i, out double value, bool canBeInfinite = true)
{
Debug.Assert(i < Count);
return ParseUtils.TryReadDouble(Unsafe.AsRef<PinnedSpanByte>(bufferPtr + i), out value, canBeInfinite);
}
/// <summary>
/// Get ASCII string argument at the given index
/// </summary>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string GetString(int i)
{
Debug.Assert(i < Count);
return ParseUtils.ReadString(*(bufferPtr + i));
}
/// <summary>
/// Get boolean argument at the given index
/// </summary>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool GetBool(int i)
{
Debug.Assert(i < Count);
return ParseUtils.ReadBool(*(bufferPtr + i));
}
/// <summary>
/// Try to get boolean argument at the given index
/// </summary>
/// <returns>True if boolean parsed successfully</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryGetBool(int i, out bool value)
{
Debug.Assert(i < Count);
return ParseUtils.TryReadBool(*(bufferPtr + i), out value);
}
}
}