Skip to content

Commit d122daa

Browse files
authored
Use "..."u8 to simplify some ReadOnlySpan<byte> constructions (#68334)
I've focused only on src and only on non-allocating use.
1 parent 5d47558 commit d122daa

File tree

20 files changed

+187
-286
lines changed

20 files changed

+187
-286
lines changed

src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.OpenSsl.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,10 @@ internal static unsafe SafeSslContextHandle AllocateSslContext(SafeFreeSslCreden
157157
}
158158
}
159159

160-
byte[]? cipherList =
161-
CipherSuitesPolicyPal.GetOpenSslCipherList(sslAuthenticationOptions.CipherSuitesPolicy, protocols, sslAuthenticationOptions.EncryptionPolicy);
162-
163-
Debug.Assert(cipherList == null || (cipherList.Length >= 1 && cipherList[cipherList.Length - 1] == 0));
164-
165-
byte[]? cipherSuites =
166-
CipherSuitesPolicyPal.GetOpenSslCipherSuites(sslAuthenticationOptions.CipherSuitesPolicy, protocols, sslAuthenticationOptions.EncryptionPolicy);
160+
ReadOnlySpan<byte> cipherList = CipherSuitesPolicyPal.GetOpenSslCipherList(sslAuthenticationOptions.CipherSuitesPolicy, protocols, sslAuthenticationOptions.EncryptionPolicy);
161+
Debug.Assert(cipherList.IsEmpty || cipherList[^1] == 0);
167162

163+
byte[]? cipherSuites = CipherSuitesPolicyPal.GetOpenSslCipherSuites(sslAuthenticationOptions.CipherSuitesPolicy, protocols, sslAuthenticationOptions.EncryptionPolicy);
168164
Debug.Assert(cipherSuites == null || (cipherSuites.Length >= 1 && cipherSuites[cipherSuites.Length - 1] == 0));
169165

170166
fixed (byte* cipherListStr = cipherList)

src/libraries/Common/src/System/Net/Http/aspnetcore/Http2/Hpack/StatusCodes.cs

Lines changed: 62 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -8,207 +8,138 @@ namespace System.Net.Http.HPack
88
{
99
internal static partial class StatusCodes
1010
{
11-
// This uses C# compiler's ability to refer to static data directly. For more information see https://vcsjones.dev/2019/02/01/csharp-readonly-span-bytes-static
12-
13-
private static ReadOnlySpan<byte> BytesStatus100 => new byte[] { (byte)'1', (byte)'0', (byte)'0' };
14-
private static ReadOnlySpan<byte> BytesStatus101 => new byte[] { (byte)'1', (byte)'0', (byte)'1' };
15-
private static ReadOnlySpan<byte> BytesStatus102 => new byte[] { (byte)'1', (byte)'0', (byte)'2' };
16-
17-
private static ReadOnlySpan<byte> BytesStatus200 => new byte[] { (byte)'2', (byte)'0', (byte)'0' };
18-
private static ReadOnlySpan<byte> BytesStatus201 => new byte[] { (byte)'2', (byte)'0', (byte)'1' };
19-
private static ReadOnlySpan<byte> BytesStatus202 => new byte[] { (byte)'2', (byte)'0', (byte)'2' };
20-
private static ReadOnlySpan<byte> BytesStatus203 => new byte[] { (byte)'2', (byte)'0', (byte)'3' };
21-
private static ReadOnlySpan<byte> BytesStatus204 => new byte[] { (byte)'2', (byte)'0', (byte)'4' };
22-
private static ReadOnlySpan<byte> BytesStatus205 => new byte[] { (byte)'2', (byte)'0', (byte)'5' };
23-
private static ReadOnlySpan<byte> BytesStatus206 => new byte[] { (byte)'2', (byte)'0', (byte)'6' };
24-
private static ReadOnlySpan<byte> BytesStatus207 => new byte[] { (byte)'2', (byte)'0', (byte)'7' };
25-
private static ReadOnlySpan<byte> BytesStatus208 => new byte[] { (byte)'2', (byte)'0', (byte)'8' };
26-
private static ReadOnlySpan<byte> BytesStatus226 => new byte[] { (byte)'2', (byte)'2', (byte)'6' };
27-
28-
private static ReadOnlySpan<byte> BytesStatus300 => new byte[] { (byte)'3', (byte)'0', (byte)'0' };
29-
private static ReadOnlySpan<byte> BytesStatus301 => new byte[] { (byte)'3', (byte)'0', (byte)'1' };
30-
private static ReadOnlySpan<byte> BytesStatus302 => new byte[] { (byte)'3', (byte)'0', (byte)'2' };
31-
private static ReadOnlySpan<byte> BytesStatus303 => new byte[] { (byte)'3', (byte)'0', (byte)'3' };
32-
private static ReadOnlySpan<byte> BytesStatus304 => new byte[] { (byte)'3', (byte)'0', (byte)'4' };
33-
private static ReadOnlySpan<byte> BytesStatus305 => new byte[] { (byte)'3', (byte)'0', (byte)'5' };
34-
private static ReadOnlySpan<byte> BytesStatus306 => new byte[] { (byte)'3', (byte)'0', (byte)'6' };
35-
private static ReadOnlySpan<byte> BytesStatus307 => new byte[] { (byte)'3', (byte)'0', (byte)'7' };
36-
private static ReadOnlySpan<byte> BytesStatus308 => new byte[] { (byte)'3', (byte)'0', (byte)'8' };
37-
38-
private static ReadOnlySpan<byte> BytesStatus400 => new byte[] { (byte)'4', (byte)'0', (byte)'0' };
39-
private static ReadOnlySpan<byte> BytesStatus401 => new byte[] { (byte)'4', (byte)'0', (byte)'1' };
40-
private static ReadOnlySpan<byte> BytesStatus402 => new byte[] { (byte)'4', (byte)'0', (byte)'2' };
41-
private static ReadOnlySpan<byte> BytesStatus403 => new byte[] { (byte)'4', (byte)'0', (byte)'3' };
42-
private static ReadOnlySpan<byte> BytesStatus404 => new byte[] { (byte)'4', (byte)'0', (byte)'4' };
43-
private static ReadOnlySpan<byte> BytesStatus405 => new byte[] { (byte)'4', (byte)'0', (byte)'5' };
44-
private static ReadOnlySpan<byte> BytesStatus406 => new byte[] { (byte)'4', (byte)'0', (byte)'6' };
45-
private static ReadOnlySpan<byte> BytesStatus407 => new byte[] { (byte)'4', (byte)'0', (byte)'7' };
46-
private static ReadOnlySpan<byte> BytesStatus408 => new byte[] { (byte)'4', (byte)'0', (byte)'8' };
47-
private static ReadOnlySpan<byte> BytesStatus409 => new byte[] { (byte)'4', (byte)'0', (byte)'9' };
48-
private static ReadOnlySpan<byte> BytesStatus410 => new byte[] { (byte)'4', (byte)'1', (byte)'0' };
49-
private static ReadOnlySpan<byte> BytesStatus411 => new byte[] { (byte)'4', (byte)'1', (byte)'1' };
50-
private static ReadOnlySpan<byte> BytesStatus412 => new byte[] { (byte)'4', (byte)'1', (byte)'2' };
51-
private static ReadOnlySpan<byte> BytesStatus413 => new byte[] { (byte)'4', (byte)'1', (byte)'3' };
52-
private static ReadOnlySpan<byte> BytesStatus414 => new byte[] { (byte)'4', (byte)'1', (byte)'4' };
53-
private static ReadOnlySpan<byte> BytesStatus415 => new byte[] { (byte)'4', (byte)'1', (byte)'5' };
54-
private static ReadOnlySpan<byte> BytesStatus416 => new byte[] { (byte)'4', (byte)'1', (byte)'6' };
55-
private static ReadOnlySpan<byte> BytesStatus417 => new byte[] { (byte)'4', (byte)'1', (byte)'7' };
56-
private static ReadOnlySpan<byte> BytesStatus418 => new byte[] { (byte)'4', (byte)'1', (byte)'8' };
57-
private static ReadOnlySpan<byte> BytesStatus419 => new byte[] { (byte)'4', (byte)'1', (byte)'9' };
58-
private static ReadOnlySpan<byte> BytesStatus421 => new byte[] { (byte)'4', (byte)'2', (byte)'1' };
59-
private static ReadOnlySpan<byte> BytesStatus422 => new byte[] { (byte)'4', (byte)'2', (byte)'2' };
60-
private static ReadOnlySpan<byte> BytesStatus423 => new byte[] { (byte)'4', (byte)'2', (byte)'3' };
61-
private static ReadOnlySpan<byte> BytesStatus424 => new byte[] { (byte)'4', (byte)'2', (byte)'4' };
62-
private static ReadOnlySpan<byte> BytesStatus426 => new byte[] { (byte)'4', (byte)'2', (byte)'6' };
63-
private static ReadOnlySpan<byte> BytesStatus428 => new byte[] { (byte)'4', (byte)'2', (byte)'8' };
64-
private static ReadOnlySpan<byte> BytesStatus429 => new byte[] { (byte)'4', (byte)'2', (byte)'9' };
65-
private static ReadOnlySpan<byte> BytesStatus431 => new byte[] { (byte)'4', (byte)'3', (byte)'1' };
66-
private static ReadOnlySpan<byte> BytesStatus451 => new byte[] { (byte)'4', (byte)'5', (byte)'1' };
67-
68-
private static ReadOnlySpan<byte> BytesStatus500 => new byte[] { (byte)'5', (byte)'0', (byte)'0' };
69-
private static ReadOnlySpan<byte> BytesStatus501 => new byte[] { (byte)'5', (byte)'0', (byte)'1' };
70-
private static ReadOnlySpan<byte> BytesStatus502 => new byte[] { (byte)'5', (byte)'0', (byte)'2' };
71-
private static ReadOnlySpan<byte> BytesStatus503 => new byte[] { (byte)'5', (byte)'0', (byte)'3' };
72-
private static ReadOnlySpan<byte> BytesStatus504 => new byte[] { (byte)'5', (byte)'0', (byte)'4' };
73-
private static ReadOnlySpan<byte> BytesStatus505 => new byte[] { (byte)'5', (byte)'0', (byte)'5' };
74-
private static ReadOnlySpan<byte> BytesStatus506 => new byte[] { (byte)'5', (byte)'0', (byte)'6' };
75-
private static ReadOnlySpan<byte> BytesStatus507 => new byte[] { (byte)'5', (byte)'0', (byte)'7' };
76-
private static ReadOnlySpan<byte> BytesStatus508 => new byte[] { (byte)'5', (byte)'0', (byte)'8' };
77-
private static ReadOnlySpan<byte> BytesStatus510 => new byte[] { (byte)'5', (byte)'1', (byte)'0' };
78-
private static ReadOnlySpan<byte> BytesStatus511 => new byte[] { (byte)'5', (byte)'1', (byte)'1' };
79-
8011
public static ReadOnlySpan<byte> ToStatusBytes(int statusCode)
8112
{
8213
switch (statusCode)
8314
{
8415
case (int)HttpStatusCode.Continue:
85-
return BytesStatus100;
16+
return "100"u8;
8617
case (int)HttpStatusCode.SwitchingProtocols:
87-
return BytesStatus101;
18+
return "101"u8;
8819
case (int)HttpStatusCode.Processing:
89-
return BytesStatus102;
20+
return "102"u8;
9021

9122
case (int)HttpStatusCode.OK:
92-
return BytesStatus200;
23+
return "200"u8;
9324
case (int)HttpStatusCode.Created:
94-
return BytesStatus201;
25+
return "201"u8;
9526
case (int)HttpStatusCode.Accepted:
96-
return BytesStatus202;
27+
return "202"u8;
9728
case (int)HttpStatusCode.NonAuthoritativeInformation:
98-
return BytesStatus203;
29+
return "203"u8;
9930
case (int)HttpStatusCode.NoContent:
100-
return BytesStatus204;
31+
return "204"u8;
10132
case (int)HttpStatusCode.ResetContent:
102-
return BytesStatus205;
33+
return "205"u8;
10334
case (int)HttpStatusCode.PartialContent:
104-
return BytesStatus206;
35+
return "206"u8;
10536
case (int)HttpStatusCode.MultiStatus:
106-
return BytesStatus207;
37+
return "207"u8;
10738
case (int)HttpStatusCode.AlreadyReported:
108-
return BytesStatus208;
39+
return "208"u8;
10940
case (int)HttpStatusCode.IMUsed:
110-
return BytesStatus226;
41+
return "226"u8;
11142

11243
case (int)HttpStatusCode.MultipleChoices:
113-
return BytesStatus300;
44+
return "300"u8;
11445
case (int)HttpStatusCode.MovedPermanently:
115-
return BytesStatus301;
46+
return "301"u8;
11647
case (int)HttpStatusCode.Found:
117-
return BytesStatus302;
48+
return "302"u8;
11849
case (int)HttpStatusCode.SeeOther:
119-
return BytesStatus303;
50+
return "303"u8;
12051
case (int)HttpStatusCode.NotModified:
121-
return BytesStatus304;
52+
return "304"u8;
12253
case (int)HttpStatusCode.UseProxy:
123-
return BytesStatus305;
54+
return "305"u8;
12455
case (int)HttpStatusCode.Unused:
125-
return BytesStatus306;
56+
return "306"u8;
12657
case (int)HttpStatusCode.TemporaryRedirect:
127-
return BytesStatus307;
58+
return "307"u8;
12859
case (int)HttpStatusCode.PermanentRedirect:
129-
return BytesStatus308;
60+
return "308"u8;
13061

13162
case (int)HttpStatusCode.BadRequest:
132-
return BytesStatus400;
63+
return "400"u8;
13364
case (int)HttpStatusCode.Unauthorized:
134-
return BytesStatus401;
65+
return "401"u8;
13566
case (int)HttpStatusCode.PaymentRequired:
136-
return BytesStatus402;
67+
return "402"u8;
13768
case (int)HttpStatusCode.Forbidden:
138-
return BytesStatus403;
69+
return "403"u8;
13970
case (int)HttpStatusCode.NotFound:
140-
return BytesStatus404;
71+
return "404"u8;
14172
case (int)HttpStatusCode.MethodNotAllowed:
142-
return BytesStatus405;
73+
return "405"u8;
14374
case (int)HttpStatusCode.NotAcceptable:
144-
return BytesStatus406;
75+
return "406"u8;
14576
case (int)HttpStatusCode.ProxyAuthenticationRequired:
146-
return BytesStatus407;
77+
return "407"u8;
14778
case (int)HttpStatusCode.RequestTimeout:
148-
return BytesStatus408;
79+
return "408"u8;
14980
case (int)HttpStatusCode.Conflict:
150-
return BytesStatus409;
81+
return "409"u8;
15182
case (int)HttpStatusCode.Gone:
152-
return BytesStatus410;
83+
return "410"u8;
15384
case (int)HttpStatusCode.LengthRequired:
154-
return BytesStatus411;
85+
return "411"u8;
15586
case (int)HttpStatusCode.PreconditionFailed:
156-
return BytesStatus412;
87+
return "412"u8;
15788
case (int)HttpStatusCode.RequestEntityTooLarge:
158-
return BytesStatus413;
89+
return "413"u8;
15990
case (int)HttpStatusCode.RequestUriTooLong:
160-
return BytesStatus414;
91+
return "414"u8;
16192
case (int)HttpStatusCode.UnsupportedMediaType:
162-
return BytesStatus415;
93+
return "415"u8;
16394
case (int)HttpStatusCode.RequestedRangeNotSatisfiable:
164-
return BytesStatus416;
95+
return "416"u8;
16596
case (int)HttpStatusCode.ExpectationFailed:
166-
return BytesStatus417;
97+
return "417"u8;
16798
case (int)418:
168-
return BytesStatus418;
99+
return "418"u8;
169100
case (int)419:
170-
return BytesStatus419;
101+
return "419"u8;
171102
case (int)HttpStatusCode.MisdirectedRequest:
172-
return BytesStatus421;
103+
return "421"u8;
173104
case (int)HttpStatusCode.UnprocessableEntity:
174-
return BytesStatus422;
105+
return "422"u8;
175106
case (int)HttpStatusCode.Locked:
176-
return BytesStatus423;
107+
return "423"u8;
177108
case (int)HttpStatusCode.FailedDependency:
178-
return BytesStatus424;
109+
return "424"u8;
179110
case (int)HttpStatusCode.UpgradeRequired:
180-
return BytesStatus426;
111+
return "426"u8;
181112
case (int)HttpStatusCode.PreconditionRequired:
182-
return BytesStatus428;
113+
return "428"u8;
183114
case (int)HttpStatusCode.TooManyRequests:
184-
return BytesStatus429;
115+
return "429"u8;
185116
case (int)HttpStatusCode.RequestHeaderFieldsTooLarge:
186-
return BytesStatus431;
117+
return "431"u8;
187118
case (int)HttpStatusCode.UnavailableForLegalReasons:
188-
return BytesStatus451;
119+
return "451"u8;
189120

190121
case (int)HttpStatusCode.InternalServerError:
191-
return BytesStatus500;
122+
return "500"u8;
192123
case (int)HttpStatusCode.NotImplemented:
193-
return BytesStatus501;
124+
return "501"u8;
194125
case (int)HttpStatusCode.BadGateway:
195-
return BytesStatus502;
126+
return "502"u8;
196127
case (int)HttpStatusCode.ServiceUnavailable:
197-
return BytesStatus503;
128+
return "503"u8;
198129
case (int)HttpStatusCode.GatewayTimeout:
199-
return BytesStatus504;
130+
return "504"u8;
200131
case (int)HttpStatusCode.HttpVersionNotSupported:
201-
return BytesStatus505;
132+
return "505"u8;
202133
case (int)HttpStatusCode.VariantAlsoNegotiates:
203-
return BytesStatus506;
134+
return "506"u8;
204135
case (int)HttpStatusCode.InsufficientStorage:
205-
return BytesStatus507;
136+
return "507"u8;
206137
case (int)HttpStatusCode.LoopDetected:
207-
return BytesStatus508;
138+
return "508"u8;
208139
case (int)HttpStatusCode.NotExtended:
209-
return BytesStatus510;
140+
return "510"u8;
210141
case (int)HttpStatusCode.NetworkAuthenticationRequired:
211-
return BytesStatus511;
142+
return "511"u8;
212143

213144
default:
214145
return Encoding.ASCII.GetBytes(statusCode.ToString(CultureInfo.InvariantCulture));

src/libraries/Microsoft.Extensions.Hosting.Systemd/src/SystemdHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ private static bool GetIsSystemdService()
5454
int parentPid = Interop.libc.GetParentPid();
5555
string ppidString = parentPid.ToString(NumberFormatInfo.InvariantInfo);
5656
byte[] comm = File.ReadAllBytes("/proc/" + ppidString + "/comm");
57-
return comm.AsSpan().SequenceEqual(Encoding.ASCII.GetBytes("systemd\n"));
57+
return comm.AsSpan().SequenceEqual("systemd\n"u8);
5858
}
5959
catch
6060
{

src/libraries/System.Drawing.Common/src/System/Drawing/ImageConverter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ namespace System.Drawing
1515
{
1616
public class ImageConverter : TypeConverter
1717
{
18-
private static ReadOnlySpan<byte> PBrush => new byte[] { (byte)'P', (byte)'B', (byte)'r', (byte)'u', (byte)'s', (byte)'h' };
18+
private static ReadOnlySpan<byte> PBrush => "PBrush"u8;
1919

20-
private static ReadOnlySpan<byte> BMBytes => new byte[] { (byte)'B', (byte)'M' };
20+
private static ReadOnlySpan<byte> BMBytes => "BM"u8;
2121

2222
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type? sourceType)
2323
{

src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http2Connection.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ internal sealed partial class Http2Connection : HttpConnectionBase
7575
// Temporary workaround for request burst handling on connection start.
7676
private const int InitialMaxConcurrentStreams = 100;
7777

78-
private static readonly byte[] s_http2ConnectionPreface = Encoding.ASCII.GetBytes("PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n");
78+
private static ReadOnlySpan<byte> Http2ConnectionPreface => "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"u8;
7979

8080
#if DEBUG
8181
// In debug builds, start with a very small buffer to induce buffer growing logic.
@@ -178,13 +178,13 @@ public async ValueTask SetupAsync()
178178
{
179179
try
180180
{
181-
_outgoingBuffer.EnsureAvailableSpace(s_http2ConnectionPreface.Length +
181+
_outgoingBuffer.EnsureAvailableSpace(Http2ConnectionPreface.Length +
182182
FrameHeader.Size + FrameHeader.SettingLength +
183183
FrameHeader.Size + FrameHeader.WindowUpdateLength);
184184

185185
// Send connection preface
186-
s_http2ConnectionPreface.AsSpan().CopyTo(_outgoingBuffer.AvailableSpan);
187-
_outgoingBuffer.Commit(s_http2ConnectionPreface.Length);
186+
Http2ConnectionPreface.CopyTo(_outgoingBuffer.AvailableSpan);
187+
_outgoingBuffer.Commit(Http2ConnectionPreface.Length);
188188

189189
// Send SETTINGS frame. Disable push promise & set initial window size.
190190
FrameHeader.WriteTo(_outgoingBuffer.AvailableSpan, 2 * FrameHeader.SettingLength, FrameType.Settings, FrameFlags.None, streamId: 0);

src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http2Stream.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ private sealed class Http2Stream : IValueTaskSource, IHttpStreamHeadersHandler,
2727
1024;
2828
#endif
2929

30-
private static ReadOnlySpan<byte> StatusHeaderName => new byte[] { (byte)':', (byte)'s', (byte)'t', (byte)'a', (byte)'t', (byte)'u', (byte)'s' };
30+
private static ReadOnlySpan<byte> StatusHeaderName => ":status"u8;
3131

3232
private readonly Http2Connection _connection;
3333
private readonly HttpRequestMessage _request;

0 commit comments

Comments
 (0)