Skip to content

Commit 1c69372

Browse files
committed
Use Span<byte> and stackalloc to remove unsafe/fixed
1 parent f67d7fd commit 1c69372

File tree

2 files changed

+44
-64
lines changed

2 files changed

+44
-64
lines changed

Enyim.Caching/Enyim.Caching.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<RepositoryType>git</RepositoryType>
1414
<RepositoryUrl>https://github.com/cnblogs/EnyimMemcachedCore</RepositoryUrl>
1515
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
16+
<LangVersion>latest</LangVersion>
1617
</PropertyGroup>
1718

1819
<ItemGroup>

Enyim.Caching/Memcached/Protocol/Binary/BinaryRequest.cs

Lines changed: 43 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ public BinaryRequest(byte commandCode)
2525
this.CorrelationId = Interlocked.Increment(ref InstanceCounter);
2626
}
2727

28-
public unsafe IList<ArraySegment<byte>> CreateBuffer()
28+
public IList<ArraySegment<byte>> CreateBuffer()
2929
{
3030
return CreateBuffer(null);
3131
}
3232

33-
public unsafe IList<ArraySegment<byte>> CreateBuffer(IList<ArraySegment<byte>> appendTo)
33+
public IList<ArraySegment<byte>> CreateBuffer(IList<ArraySegment<byte>> appendTo)
3434
{
3535
// key size
3636
byte[] keyData = BinaryConverter.EncodeKey(this.Key);
@@ -51,81 +51,60 @@ public unsafe IList<ArraySegment<byte>> CreateBuffer(IList<ArraySegment<byte>> a
5151
int totalLength = extraLength + keyLength + bodyLength;
5252

5353
//build the header
54-
byte[] header = new byte[24];
54+
Span<byte> header = stackalloc byte[24];
5555

56-
fixed (byte* buffer = header)
56+
header[0x00] = 0x80; // magic
57+
header[0x01] = this.Operation;
58+
59+
// key length
60+
header[0x02] = (byte)(keyLength >> 8);
61+
header[0x03] = (byte)(keyLength & 255);
62+
63+
// extra length
64+
header[0x04] = (byte)(extraLength);
65+
66+
// 5 -- data type, 0 (RAW)
67+
// 6,7 -- reserved, always 0
68+
69+
header[0x06] = (byte)(this.Reserved >> 8);
70+
header[0x07] = (byte)(this.Reserved & 255);
71+
72+
// body length
73+
header[0x08] = (byte)(totalLength >> 24);
74+
header[0x09] = (byte)(totalLength >> 16);
75+
header[0x0a] = (byte)(totalLength >> 8);
76+
header[0x0b] = (byte)(totalLength & 255);
77+
78+
header[0x0c] = (byte)(this.CorrelationId >> 24);
79+
header[0x0d] = (byte)(this.CorrelationId >> 16);
80+
header[0x0e] = (byte)(this.CorrelationId >> 8);
81+
header[0x0f] = (byte)(this.CorrelationId & 255);
82+
83+
ulong cas = this.Cas;
84+
// CAS
85+
if (cas > 0)
5786
{
58-
buffer[0x00] = 0x80; // magic
59-
buffer[0x01] = this.Operation;
60-
61-
// key length
62-
buffer[0x02] = (byte)(keyLength >> 8);
63-
buffer[0x03] = (byte)(keyLength & 255);
64-
65-
// extra length
66-
buffer[0x04] = (byte)(extraLength);
67-
68-
// 5 -- data type, 0 (RAW)
69-
// 6,7 -- reserved, always 0
70-
71-
buffer[0x06] = (byte)(this.Reserved >> 8);
72-
buffer[0x07] = (byte)(this.Reserved & 255);
73-
74-
// body length
75-
buffer[0x08] = (byte)(totalLength >> 24);
76-
buffer[0x09] = (byte)(totalLength >> 16);
77-
buffer[0x0a] = (byte)(totalLength >> 8);
78-
buffer[0x0b] = (byte)(totalLength & 255);
79-
80-
buffer[0x0c] = (byte)(this.CorrelationId >> 24);
81-
buffer[0x0d] = (byte)(this.CorrelationId >> 16);
82-
buffer[0x0e] = (byte)(this.CorrelationId >> 8);
83-
buffer[0x0f] = (byte)(this.CorrelationId & 255);
84-
85-
ulong cas = this.Cas;
86-
// CAS
87-
if (cas > 0)
88-
{
89-
// skip this if no cas is specfied
90-
buffer[0x10] = (byte)(cas >> 56);
91-
buffer[0x11] = (byte)(cas >> 48);
92-
buffer[0x12] = (byte)(cas >> 40);
93-
buffer[0x13] = (byte)(cas >> 32);
94-
buffer[0x14] = (byte)(cas >> 24);
95-
buffer[0x15] = (byte)(cas >> 16);
96-
buffer[0x16] = (byte)(cas >> 8);
97-
buffer[0x17] = (byte)(cas & 255);
98-
}
87+
// skip this if no cas is specfied
88+
header[0x10] = (byte)(cas >> 56);
89+
header[0x11] = (byte)(cas >> 48);
90+
header[0x12] = (byte)(cas >> 40);
91+
header[0x13] = (byte)(cas >> 32);
92+
header[0x14] = (byte)(cas >> 24);
93+
header[0x15] = (byte)(cas >> 16);
94+
header[0x16] = (byte)(cas >> 8);
95+
header[0x17] = (byte)(cas & 255);
9996
}
10097

10198
var retval = appendTo ?? new List<ArraySegment<byte>>(4);
10299

103-
retval.Add(new ArraySegment<byte>(header));
100+
retval.Add(new ArraySegment<byte>(header.ToArray()));
104101

105102
if (extraLength > 0) retval.Add(extras);
106103

107104
// NOTE key must be already encoded and should not contain any invalid characters which are not allowed by the protocol
108105
if (keyLength > 0) retval.Add(new ArraySegment<byte>(keyData));
109106
if (bodyLength > 0) retval.Add(body);
110107

111-
#if DEBUG_PROTOCOL
112-
if (log.IsDebugEnabled)
113-
{
114-
log.Debug("Building binary request");
115-
StringBuilder sb = new StringBuilder(128).AppendLine();
116-
117-
for (int i = 0; i < header.Length; i++)
118-
{
119-
byte value = header[i];
120-
sb.Append(value < 16 ? "0x0" : "0x").Append(value.ToString("X"));
121-
122-
if (i % 4 == 3) sb.AppendLine(); else sb.Append(" ");
123-
}
124-
125-
log.Debug(sb.ToString());
126-
}
127-
#endif
128-
129108
return retval;
130109
}
131110

0 commit comments

Comments
 (0)