Skip to content

Commit c877593

Browse files
committed
Address some nits
1 parent 6933d8f commit c877593

File tree

5 files changed

+242
-199
lines changed

5 files changed

+242
-199
lines changed

libs/server/ArgSlice/ArgSliceUtils.cs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the MIT license.
33

44
using System;
5-
using System.Text;
65
using Garnet.common;
76

87
namespace Garnet.server
@@ -12,6 +11,26 @@ namespace Garnet.server
1211
/// </summary>
1312
public static class ArgSliceUtils
1413
{
14+
private static ReadOnlySpan<byte> CharToHexLookup =>
15+
[
16+
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 15
17+
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 31
18+
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 47
19+
0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 63
20+
0xFF, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 79
21+
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 95
22+
0xFF, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 111
23+
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 127
24+
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 143
25+
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 159
26+
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 175
27+
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 191
28+
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 207
29+
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 223
30+
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 239
31+
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF // 255
32+
];
33+
1534
/// <summary>
1635
/// Compute hash slot of given ArgSlice
1736
/// </summary>
@@ -121,17 +140,16 @@ public static unsafe ArgSlice Unescape(ArgSlice slice, bool acceptUppercaseEscap
121140
case 'x':
122141
if (j + shift + 2 < slice.Span.Length)
123142
{
124-
try
143+
var val =
144+
16 * CharToHexLookup[slice.ReadOnlySpan[j + shift + 1]] +
145+
CharToHexLookup[slice.ReadOnlySpan[j + shift + 2]];
146+
147+
if (val < 0xFF)
125148
{
126-
var res = Convert.FromHexString(Encoding.ASCII.GetString(slice.Span.Slice(j + shift + 1, 2)));
127-
c = (char)res[0];
149+
c = (char)val;
128150
shift += 2;
129-
break;
130-
}
131-
catch
132-
{
133-
134151
}
152+
break;
135153
}
136154
break;
137155
default:
@@ -142,10 +160,7 @@ public static unsafe ArgSlice Unescape(ArgSlice slice, bool acceptUppercaseEscap
142160
}
143161

144162
// Zero unnecessary chars
145-
for (var l = slice.Span.Length - shift - start; l < slice.Span.Length - 1; ++l)
146-
{
147-
slice.Span[l] = 0;
148-
}
163+
slice.Span[(slice.Span.Length - shift - start)..(slice.Span.Length - 1)].Clear();
149164

150165
// The final cut must remove 1 from length to trim the end quote character.
151166
// (The starting quote character was already shifted out)

libs/server/Resp/Parser/RespCommand.cs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ enum RespCommandOption : byte
640640
/// </summary>
641641
internal sealed unsafe partial class RespServerSession : ServerSessionBase
642642
{
643-
private static readonly ushort crlf = MemoryMarshal.Read<ushort>("\r\n"u8);
643+
private static readonly ushort CrLf = MemoryMarshal.Read<ushort>("\r\n"u8);
644644

645645
/// <summary>
646646
/// Fast-parses for command type, starting at the current read head in the receive buffer
@@ -1402,47 +1402,47 @@ private RespCommand FastParseArrayCommand(ref int count, byte* ptr, int remainin
14021402
}
14031403
break;
14041404
case 8:
1405-
if (*(ulong*)(ptr + 4) == MemoryMarshal.Read<ulong>("ZREVRANK"u8) && *(ushort*)(ptr + 12) == crlf)
1405+
if (*(ulong*)(ptr + 4) == MemoryMarshal.Read<ulong>("ZREVRANK"u8) && *(ushort*)(ptr + 12) == CrLf)
14061406
{
14071407
return RespCommand.ZREVRANK;
14081408
}
1409-
else if (*(ulong*)(ptr + 4) == MemoryMarshal.Read<ulong>("SMEMBERS"u8) && *(ushort*)(ptr + 12) == crlf)
1409+
else if (*(ulong*)(ptr + 4) == MemoryMarshal.Read<ulong>("SMEMBERS"u8) && *(ushort*)(ptr + 12) == CrLf)
14101410
{
14111411
return RespCommand.SMEMBERS;
14121412
}
1413-
else if (*(ulong*)(ptr + 4) == MemoryMarshal.Read<ulong>("BITFIELD"u8) && *(ushort*)(ptr + 12) == crlf)
1413+
else if (*(ulong*)(ptr + 4) == MemoryMarshal.Read<ulong>("BITFIELD"u8) && *(ushort*)(ptr + 12) == CrLf)
14141414
{
14151415
return RespCommand.BITFIELD;
14161416
}
1417-
else if (*(ulong*)(ptr + 4) == MemoryMarshal.Read<ulong>("EXPIREAT"u8) && *(ushort*)(ptr + 12) == crlf)
1417+
else if (*(ulong*)(ptr + 4) == MemoryMarshal.Read<ulong>("EXPIREAT"u8) && *(ushort*)(ptr + 12) == CrLf)
14181418
{
14191419
return RespCommand.EXPIREAT;
14201420
}
1421-
else if (*(ulong*)(ptr + 4) == MemoryMarshal.Read<ulong>("HPEXPIRE"u8) && *(ushort*)(ptr + 12) == crlf)
1421+
else if (*(ulong*)(ptr + 4) == MemoryMarshal.Read<ulong>("HPEXPIRE"u8) && *(ushort*)(ptr + 12) == CrLf)
14221422
{
14231423
return RespCommand.HPEXPIRE;
14241424
}
1425-
else if (*(ulong*)(ptr + 4) == MemoryMarshal.Read<ulong>("HPERSIST"u8) && *(ushort*)(ptr + 12) == crlf)
1425+
else if (*(ulong*)(ptr + 4) == MemoryMarshal.Read<ulong>("HPERSIST"u8) && *(ushort*)(ptr + 12) == CrLf)
14261426
{
14271427
return RespCommand.HPERSIST;
14281428
}
1429-
else if (*(ulong*)(ptr + 4) == MemoryMarshal.Read<ulong>("ZPEXPIRE"u8) && *(ushort*)(ptr + 12) == crlf)
1429+
else if (*(ulong*)(ptr + 4) == MemoryMarshal.Read<ulong>("ZPEXPIRE"u8) && *(ushort*)(ptr + 12) == CrLf)
14301430
{
14311431
return RespCommand.ZPEXPIRE;
14321432
}
1433-
else if (*(ulong*)(ptr + 4) == MemoryMarshal.Read<ulong>("ZPERSIST"u8) && *(ushort*)(ptr + 12) == crlf)
1433+
else if (*(ulong*)(ptr + 4) == MemoryMarshal.Read<ulong>("ZPERSIST"u8) && *(ushort*)(ptr + 12) == CrLf)
14341434
{
14351435
return RespCommand.ZPERSIST;
14361436
}
1437-
else if (*(ulong*)(ptr + 4) == MemoryMarshal.Read<ulong>("BZPOPMAX"u8) && *(ushort*)(ptr + 12) == crlf)
1437+
else if (*(ulong*)(ptr + 4) == MemoryMarshal.Read<ulong>("BZPOPMAX"u8) && *(ushort*)(ptr + 12) == CrLf)
14381438
{
14391439
return RespCommand.BZPOPMAX;
14401440
}
1441-
else if (*(ulong*)(ptr + 4) == MemoryMarshal.Read<ulong>("BZPOPMIN"u8) && *(ushort*)(ptr + 12) == crlf)
1441+
else if (*(ulong*)(ptr + 4) == MemoryMarshal.Read<ulong>("BZPOPMIN"u8) && *(ushort*)(ptr + 12) == CrLf)
14421442
{
14431443
return RespCommand.BZPOPMIN;
14441444
}
1445-
else if (*(ulong*)(ptr + 4) == MemoryMarshal.Read<ulong>("SPUBLISH"u8) && *(ushort*)(ptr + 12) == crlf)
1445+
else if (*(ulong*)(ptr + 4) == MemoryMarshal.Read<ulong>("SPUBLISH"u8) && *(ushort*)(ptr + 12) == CrLf)
14461446
{
14471447
return RespCommand.SPUBLISH;
14481448
}
@@ -1649,22 +1649,22 @@ private RespCommand FastParseArrayCommand(ref int count, byte* ptr, int remainin
16491649
break;
16501650

16511651
case 14:
1652-
if (*(ulong*)(ptr + 3) == MemoryMarshal.Read<ulong>("\r\nZREMRA"u8) && *(ulong*)(ptr + 11) == MemoryMarshal.Read<ulong>("NGEBYLEX"u8) && *(ushort*)(ptr + 19) == crlf)
1652+
if (*(ulong*)(ptr + 3) == MemoryMarshal.Read<ulong>("\r\nZREMRA"u8) && *(ulong*)(ptr + 11) == MemoryMarshal.Read<ulong>("NGEBYLEX"u8) && *(ushort*)(ptr + 19) == CrLf)
16531653
{
16541654
return RespCommand.ZREMRANGEBYLEX;
16551655
}
1656-
else if (*(ulong*)(ptr + 3) == MemoryMarshal.Read<ulong>("\r\nGEOSEA"u8) && *(ulong*)(ptr + 11) == MemoryMarshal.Read<ulong>("RCHSTORE"u8) && *(ushort*)(ptr + 19) == crlf)
1656+
else if (*(ulong*)(ptr + 3) == MemoryMarshal.Read<ulong>("\r\nGEOSEA"u8) && *(ulong*)(ptr + 11) == MemoryMarshal.Read<ulong>("RCHSTORE"u8) && *(ushort*)(ptr + 19) == CrLf)
16571657
{
16581658
return RespCommand.GEOSEARCHSTORE;
16591659
}
1660-
else if (*(ulong*)(ptr + 3) == MemoryMarshal.Read<ulong>("\r\nZREVRA"u8) && *(ulong*)(ptr + 11) == MemoryMarshal.Read<ulong>("NGEBYLEX"u8) && *(ushort*)(ptr + 19) == crlf)
1660+
else if (*(ulong*)(ptr + 3) == MemoryMarshal.Read<ulong>("\r\nZREVRA"u8) && *(ulong*)(ptr + 11) == MemoryMarshal.Read<ulong>("NGEBYLEX"u8) && *(ushort*)(ptr + 19) == CrLf)
16611661
{
16621662
return RespCommand.ZREVRANGEBYLEX;
16631663
}
16641664
break;
16651665

16661666
case 15:
1667-
if (*(ulong*)(ptr + 4) == MemoryMarshal.Read<ulong>("\nZREMRAN"u8) && *(ulong*)(ptr + 12) == MemoryMarshal.Read<ulong>("GEBYRANK"u8) && *(ushort*)(ptr + 20) == crlf)
1667+
if (*(ulong*)(ptr + 4) == MemoryMarshal.Read<ulong>("\nZREMRAN"u8) && *(ulong*)(ptr + 12) == MemoryMarshal.Read<ulong>("GEBYRANK"u8) && *(ushort*)(ptr + 20) == CrLf)
16681668
{
16691669
return RespCommand.ZREMRANGEBYRANK;
16701670
}
@@ -2686,8 +2686,6 @@ private RespCommand ParseCommand(bool writeErrorOnFailure, out bool commandRecei
26862686

26872687
if (cmd != RespCommand.NONE)
26882688
{
2689-
// Eureka!
2690-
26912689
// Set up read pointer past the command.
26922690
ptr = recvBufferPtr + readHead;
26932691

@@ -2761,13 +2759,13 @@ private bool ParseInlineCommandline(ref SpanByteAndMemory spam,
27612759
// We priorize the normal processing, it's better to let inline parsing hack around it.
27622760
// We can exploit the fact that any such short command would be invalid to skip it.
27632761
var tptr = ptr;
2764-
for (var i = 0; i < MINIMUMPROCESSLENGTH - 1; ++i)
2762+
for (var i = 0; i < MinimumProcessLength - 1; ++i)
27652763
{
2766-
if (*(ushort*)tptr++ == crlf)
2764+
if (*(ushort*)tptr++ == CrLf)
27672765
{
27682766
ptr = tptr + 1;
27692767

2770-
if (bytesRead - i < MINIMUMPROCESSLENGTH)
2768+
if (bytesRead - i < MinimumProcessLength)
27712769
{
27722770
commandReceived = true;
27732771
readHead = i + 2;

libs/server/Resp/RespServerSession.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ internal sealed unsafe partial class RespServerSession : ServerSessionBase
9191
/// <summary>
9292
/// No redis command (including the terminator) is smaller than this length.
9393
/// </summary>
94-
private const int MINIMUMPROCESSLENGTH = 4;
94+
private const int MinimumProcessLength = 4;
9595

9696
internal byte* dcurr, dend;
9797
bool toDispose;
@@ -559,7 +559,7 @@ private void ProcessMessages()
559559

560560
var _origReadHead = readHead;
561561

562-
while (bytesRead - readHead >= MINIMUMPROCESSLENGTH)
562+
while (bytesRead - readHead >= MinimumProcessLength)
563563
{
564564
// First, parse the command, making sure we have the entire command available
565565
// We use endReadHead to track the end of the current command

0 commit comments

Comments
 (0)