Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Commit cfa19d5

Browse files
committed
Pre init all func init'd readonly statics in consts
1 parent 06c1869 commit cfa19d5

File tree

3 files changed

+221
-152
lines changed

3 files changed

+221
-152
lines changed

src/Microsoft.AspNet.Server.Kestrel/Http/Frame.cs

Lines changed: 22 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System.Linq;
88
using System.Net;
99
using System.Numerics;
10-
using System.Text;
1110
using System.Threading;
1211
using System.Threading.Tasks;
1312
using Microsoft.AspNet.Http;
@@ -22,24 +21,6 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
2221
{
2322
public abstract partial class Frame : FrameContext, IFrameControl
2423
{
25-
private static readonly Encoding _ascii = Encoding.ASCII;
26-
private static readonly ArraySegment<byte> _endChunkBytes = CreateAsciiByteArraySegment("\r\n");
27-
private static readonly ArraySegment<byte> _endChunkedResponseBytes = CreateAsciiByteArraySegment("0\r\n\r\n");
28-
private static readonly ArraySegment<byte> _continueBytes = CreateAsciiByteArraySegment("HTTP/1.1 100 Continue\r\n\r\n");
29-
private static readonly ArraySegment<byte> _emptyData = new ArraySegment<byte>(new byte[0]);
30-
private static readonly byte[] _hex = Encoding.ASCII.GetBytes("0123456789abcdef");
31-
32-
private static readonly byte[] _bytesConnectionClose = Encoding.ASCII.GetBytes("\r\nConnection: close");
33-
private static readonly byte[] _bytesConnectionKeepAlive = Encoding.ASCII.GetBytes("\r\nConnection: keep-alive");
34-
private static readonly byte[] _bytesTransferEncodingChunked = Encoding.ASCII.GetBytes("\r\nTransfer-Encoding: chunked");
35-
private static readonly byte[] _bytesHttpVersion1_0 = Encoding.ASCII.GetBytes("HTTP/1.0 ");
36-
private static readonly byte[] _bytesHttpVersion1_1 = Encoding.ASCII.GetBytes("HTTP/1.1 ");
37-
private static readonly byte[] _bytesContentLengthZero = Encoding.ASCII.GetBytes("\r\nContent-Length: 0");
38-
private static readonly byte[] _bytesSpace = Encoding.ASCII.GetBytes(" ");
39-
private static readonly byte[] _bytesServer = Encoding.ASCII.GetBytes("\r\nServer: Kestrel");
40-
private static readonly byte[] _bytesDate = Encoding.ASCII.GetBytes("Date: ");
41-
private static readonly byte[] _bytesEndHeaders = Encoding.ASCII.GetBytes("\r\n\r\n");
42-
4324
private static Vector<byte> _vectorCRs = new Vector<byte>((byte)'\r');
4425
private static Vector<byte> _vectorColons = new Vector<byte>((byte)':');
4526
private static Vector<byte> _vectorSpaces = new Vector<byte>((byte)' ');
@@ -258,7 +239,7 @@ public void ResetResponseHeaders()
258239
DateHeaderValueManager.GetDateHeaderValueBytes());
259240
_responseHeaders.SetRawServer(
260241
"Kestrel",
261-
_bytesServer);
242+
Constants.HeaderBytesServer);
262243
}
263244

264245
/// <summary>
@@ -404,13 +385,13 @@ protected async Task FireOnCompleted()
404385
public void Flush()
405386
{
406387
ProduceStartAndFireOnStarting(immediate: false).GetAwaiter().GetResult();
407-
SocketOutput.Write(_emptyData, immediate: true);
388+
SocketOutput.Write(new ArraySegment<byte>(Constants.HeaderEmptyDataBytes), immediate: true);
408389
}
409390

410391
public async Task FlushAsync(CancellationToken cancellationToken)
411392
{
412393
await ProduceStartAndFireOnStarting(immediate: false);
413-
await SocketOutput.WriteAsync(_emptyData, immediate: true, cancellationToken: cancellationToken);
394+
await SocketOutput.WriteAsync(new ArraySegment<byte>(Constants.HeaderEmptyDataBytes), immediate: true, cancellationToken: cancellationToken);
414395
}
415396

416397
public void Write(ArraySegment<byte> data)
@@ -474,28 +455,28 @@ private void WriteChunked(ArraySegment<byte> data)
474455
{
475456
SocketOutput.Write(BeginChunkBytes(data.Count), immediate: false);
476457
SocketOutput.Write(data, immediate: false);
477-
SocketOutput.Write(_endChunkBytes, immediate: true);
458+
SocketOutput.Write(new ArraySegment<byte>(Constants.HeaderEndChunkBytes), immediate: true);
478459
}
479460

480461
private async Task WriteChunkedAsync(ArraySegment<byte> data, CancellationToken cancellationToken)
481462
{
482463
await SocketOutput.WriteAsync(BeginChunkBytes(data.Count), immediate: false, cancellationToken: cancellationToken);
483464
await SocketOutput.WriteAsync(data, immediate: false, cancellationToken: cancellationToken);
484-
await SocketOutput.WriteAsync(_endChunkBytes, immediate: true, cancellationToken: cancellationToken);
465+
await SocketOutput.WriteAsync(new ArraySegment<byte>(Constants.HeaderEndChunkBytes), immediate: true, cancellationToken: cancellationToken);
485466
}
486467

487468
public static ArraySegment<byte> BeginChunkBytes(int dataCount)
488469
{
489470
var bytes = new byte[10]
490471
{
491-
_hex[((dataCount >> 0x1c) & 0x0f)],
492-
_hex[((dataCount >> 0x18) & 0x0f)],
493-
_hex[((dataCount >> 0x14) & 0x0f)],
494-
_hex[((dataCount >> 0x10) & 0x0f)],
495-
_hex[((dataCount >> 0x0c) & 0x0f)],
496-
_hex[((dataCount >> 0x08) & 0x0f)],
497-
_hex[((dataCount >> 0x04) & 0x0f)],
498-
_hex[((dataCount >> 0x00) & 0x0f)],
472+
Constants.HexBytes[((dataCount >> 0x1c) & 0x0f)],
473+
Constants.HexBytes[((dataCount >> 0x18) & 0x0f)],
474+
Constants.HexBytes[((dataCount >> 0x14) & 0x0f)],
475+
Constants.HexBytes[((dataCount >> 0x10) & 0x0f)],
476+
Constants.HexBytes[((dataCount >> 0x0c) & 0x0f)],
477+
Constants.HexBytes[((dataCount >> 0x08) & 0x0f)],
478+
Constants.HexBytes[((dataCount >> 0x04) & 0x0f)],
479+
Constants.HexBytes[((dataCount >> 0x00) & 0x0f)],
499480
(byte)'\r',
500481
(byte)'\n',
501482
};
@@ -515,13 +496,7 @@ public static ArraySegment<byte> BeginChunkBytes(int dataCount)
515496

516497
private void WriteChunkedResponseSuffix()
517498
{
518-
SocketOutput.Write(_endChunkedResponseBytes, immediate: true);
519-
}
520-
521-
private static ArraySegment<byte> CreateAsciiByteArraySegment(string text)
522-
{
523-
var bytes = Encoding.ASCII.GetBytes(text);
524-
return new ArraySegment<byte>(bytes);
499+
SocketOutput.Write(new ArraySegment<byte>(Constants.HeaderEndChunkedResponseBytes), immediate: true);
525500
}
526501

527502
public void ProduceContinue()
@@ -533,7 +508,7 @@ public void ProduceContinue()
533508
RequestHeaders.TryGetValue("Expect", out expect) &&
534509
(expect.FirstOrDefault() ?? "").Equals("100-continue", StringComparison.OrdinalIgnoreCase))
535510
{
536-
SocketOutput.Write(_continueBytes);
511+
SocketOutput.Write(new ArraySegment<byte>(Constants.HeaderContinueBytes));
537512
}
538513
}
539514

@@ -596,7 +571,7 @@ protected Task ProduceEnd()
596571
ReasonPhrase = null;
597572

598573
ResetResponseHeaders();
599-
_responseHeaders.SetRawContentLength("0", _bytesContentLengthZero);
574+
_responseHeaders.SetRawContentLength("0", Constants.HeaderBytesContentLengthZero);
600575
}
601576
}
602577

@@ -660,15 +635,15 @@ private Task CreateResponseHeader(
660635
{
661636
// Since the app has completed and we are only now generating
662637
// the headers we can safely set the Content-Length to 0.
663-
_responseHeaders.SetRawContentLength("0", _bytesContentLengthZero);
638+
_responseHeaders.SetRawContentLength("0", Constants.HeaderBytesContentLengthZero);
664639
}
665640
}
666641
else
667642
{
668643
if (_httpVersion == HttpVersionType.Http1_1)
669644
{
670645
_autoChunk = true;
671-
_responseHeaders.SetRawTransferEncoding("chunked", _bytesTransferEncodingChunked);
646+
_responseHeaders.SetRawTransferEncoding("chunked", Constants.HeaderBytesTransferEncodingChunked);
672647
}
673648
else
674649
{
@@ -679,17 +654,17 @@ private Task CreateResponseHeader(
679654

680655
if (_keepAlive == false && _responseHeaders.HasConnection == false && _httpVersion == HttpVersionType.Http1_1)
681656
{
682-
_responseHeaders.SetRawConnection("close", _bytesConnectionClose);
657+
_responseHeaders.SetRawConnection("close", Constants.HeaderBytesConnectionClose);
683658
}
684659
else if (_keepAlive && _responseHeaders.HasConnection == false && _httpVersion == HttpVersionType.Http1_0)
685660
{
686-
_responseHeaders.SetRawConnection("keep-alive", _bytesConnectionKeepAlive);
661+
_responseHeaders.SetRawConnection("keep-alive", Constants.HeaderBytesConnectionKeepAlive);
687662
}
688663

689-
end.CopyFrom(_httpVersion == HttpVersionType.Http1_1 ? _bytesHttpVersion1_1 : _bytesHttpVersion1_0);
664+
end.CopyFrom(_httpVersion == HttpVersionType.Http1_1 ? Constants.HeaderBytesHttpVersion1_1 : Constants.HeaderBytesHttpVersion1_0);
690665
end.CopyFrom(statusBytes);
691666
_responseHeaders.CopyTo(ref end);
692-
end.CopyFrom(_bytesEndHeaders, 0, _bytesEndHeaders.Length);
667+
end.CopyFrom(Constants.HeaderBytesEndHeaders, 0, Constants.HeaderBytesEndHeaders.Length);
693668

694669
SocketOutput.ProducingComplete(end);
695670

0 commit comments

Comments
 (0)