Remove explicit static constructors (#1483)

Explicit static cctors cause the C# compiler to not mark types as
beforefieldinit, which means the JIT will add checks to each static
method and instance constructor of the type to make sure that the static
constructor was previously called.
This commit is contained in:
Justin Van Patten 2017-03-18 12:42:31 -07:00 committed by David Fowler
parent 5b814a55ac
commit 6d2001c67a
3 changed files with 87 additions and 73 deletions

View File

@ -41,29 +41,33 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure
private readonly static ulong _mask5Chars = GetMaskAsLong(new byte[] { 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00 });
private readonly static ulong _mask4Chars = GetMaskAsLong(new byte[] { 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 });
private readonly static Tuple<ulong, ulong, HttpMethod, int>[] _knownMethods = new Tuple<ulong, ulong, HttpMethod, int>[8];
private readonly static string[] _methodNames = new string[9];
static HttpUtilities()
private readonly static Tuple<ulong, ulong, HttpMethod, int>[] _knownMethods =
{
_knownMethods[0] = Tuple.Create(_mask4Chars, _httpPutMethodLong, HttpMethod.Put, 3);
_knownMethods[1] = Tuple.Create(_mask5Chars, _httpPostMethodLong, HttpMethod.Post, 4);
_knownMethods[2] = Tuple.Create(_mask5Chars, _httpHeadMethodLong, HttpMethod.Head, 4);
_knownMethods[3] = Tuple.Create(_mask6Chars, _httpTraceMethodLong, HttpMethod.Trace, 5);
_knownMethods[4] = Tuple.Create(_mask6Chars, _httpPatchMethodLong, HttpMethod.Patch, 5);
_knownMethods[5] = Tuple.Create(_mask7Chars, _httpDeleteMethodLong, HttpMethod.Delete, 6);
_knownMethods[6] = Tuple.Create(_mask8Chars, _httpConnectMethodLong, HttpMethod.Connect, 7);
_knownMethods[7] = Tuple.Create(_mask8Chars, _httpOptionsMethodLong, HttpMethod.Options, 7);
_methodNames[(byte)HttpMethod.Get] = HttpMethods.Get;
_methodNames[(byte)HttpMethod.Put] = HttpMethods.Put;
_methodNames[(byte)HttpMethod.Delete] = HttpMethods.Delete;
_methodNames[(byte)HttpMethod.Post] = HttpMethods.Post;
_methodNames[(byte)HttpMethod.Head] = HttpMethods.Head;
_methodNames[(byte)HttpMethod.Trace] = HttpMethods.Trace;
_methodNames[(byte)HttpMethod.Patch] = HttpMethods.Patch;
_methodNames[(byte)HttpMethod.Connect] = HttpMethods.Connect;
_methodNames[(byte)HttpMethod.Options] = HttpMethods.Options;
Tuple.Create(_mask4Chars, _httpPutMethodLong, HttpMethod.Put, 3),
Tuple.Create(_mask5Chars, _httpPostMethodLong, HttpMethod.Post, 4),
Tuple.Create(_mask5Chars, _httpHeadMethodLong, HttpMethod.Head, 4),
Tuple.Create(_mask6Chars, _httpTraceMethodLong, HttpMethod.Trace, 5),
Tuple.Create(_mask6Chars, _httpPatchMethodLong, HttpMethod.Patch, 5),
Tuple.Create(_mask7Chars, _httpDeleteMethodLong, HttpMethod.Delete, 6),
Tuple.Create(_mask8Chars, _httpConnectMethodLong, HttpMethod.Connect, 7),
Tuple.Create(_mask8Chars, _httpOptionsMethodLong, HttpMethod.Options, 7),
};
private readonly static string[] _methodNames = CreateMethodNames();
private static string[] CreateMethodNames()
{
var methodNames = new string[9];
methodNames[(byte)HttpMethod.Get] = HttpMethods.Get;
methodNames[(byte)HttpMethod.Put] = HttpMethods.Put;
methodNames[(byte)HttpMethod.Delete] = HttpMethods.Delete;
methodNames[(byte)HttpMethod.Post] = HttpMethods.Post;
methodNames[(byte)HttpMethod.Head] = HttpMethods.Head;
methodNames[(byte)HttpMethod.Trace] = HttpMethods.Trace;
methodNames[(byte)HttpMethod.Patch] = HttpMethods.Patch;
methodNames[(byte)HttpMethod.Connect] = HttpMethods.Connect;
methodNames[(byte)HttpMethod.Options] = HttpMethods.Options;
return methodNames;
}
private unsafe static ulong GetAsciiStringAsLong(string str)

View File

@ -12,52 +12,68 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal
/// </summary>
public class KestrelTrace : IKestrelTrace
{
private static readonly Action<ILogger, string, Exception> _connectionStart;
private static readonly Action<ILogger, string, Exception> _connectionStop;
private static readonly Action<ILogger, string, Exception> _connectionPause;
private static readonly Action<ILogger, string, Exception> _connectionResume;
private static readonly Action<ILogger, string, Exception> _connectionReadFin;
private static readonly Action<ILogger, string, Exception> _connectionWriteFin;
private static readonly Action<ILogger, string, int, Exception> _connectionWroteFin;
private static readonly Action<ILogger, string, Exception> _connectionKeepAlive;
private static readonly Action<ILogger, string, Exception> _connectionDisconnect;
private static readonly Action<ILogger, string, Exception> _applicationError;
private static readonly Action<ILogger, string, Exception> _connectionError;
private static readonly Action<ILogger, string, int, Exception> _connectionDisconnectedWrite;
private static readonly Action<ILogger, string, long, Exception> _connectionHeadResponseBodyWrite;
private static readonly Action<ILogger, Exception> _notAllConnectionsClosedGracefully;
private static readonly Action<ILogger, Exception> _notAllConnectionsAborted;
private static readonly Action<ILogger, string, string, Exception> _connectionBadRequest;
private static readonly Action<ILogger, string, Exception> _connectionReset;
private static readonly Action<ILogger, string, Exception> _requestProcessingError;
private static readonly Action<ILogger, string, Exception> _connectionStart =
LoggerMessage.Define<string>(LogLevel.Debug, 1, @"Connection id ""{ConnectionId}"" started.");
private static readonly Action<ILogger, string, Exception> _connectionStop =
LoggerMessage.Define<string>(LogLevel.Debug, 2, @"Connection id ""{ConnectionId}"" stopped.");
// ConnectionRead: Reserved: 3
private static readonly Action<ILogger, string, Exception> _connectionPause =
LoggerMessage.Define<string>(LogLevel.Debug, 4, @"Connection id ""{ConnectionId}"" paused.");
private static readonly Action<ILogger, string, Exception> _connectionResume =
LoggerMessage.Define<string>(LogLevel.Debug, 5, @"Connection id ""{ConnectionId}"" resumed.");
private static readonly Action<ILogger, string, Exception> _connectionReadFin =
LoggerMessage.Define<string>(LogLevel.Debug, 6, @"Connection id ""{ConnectionId}"" received FIN.");
private static readonly Action<ILogger, string, Exception> _connectionWriteFin =
LoggerMessage.Define<string>(LogLevel.Debug, 7, @"Connection id ""{ConnectionId}"" sending FIN.");
private static readonly Action<ILogger, string, int, Exception> _connectionWroteFin =
LoggerMessage.Define<string, int>(LogLevel.Debug, 8, @"Connection id ""{ConnectionId}"" sent FIN with status ""{Status}"".");
private static readonly Action<ILogger, string, Exception> _connectionKeepAlive =
LoggerMessage.Define<string>(LogLevel.Debug, 9, @"Connection id ""{ConnectionId}"" completed keep alive response.");
private static readonly Action<ILogger, string, Exception> _connectionDisconnect =
LoggerMessage.Define<string>(LogLevel.Debug, 10, @"Connection id ""{ConnectionId}"" disconnecting.");
// ConnectionWrite: Reserved: 11
// ConnectionWriteCallback: Reserved: 12
private static readonly Action<ILogger, string, Exception> _applicationError =
LoggerMessage.Define<string>(LogLevel.Error, 13, @"Connection id ""{ConnectionId}"": An unhandled exception was thrown by the application.");
private static readonly Action<ILogger, string, Exception> _connectionError =
LoggerMessage.Define<string>(LogLevel.Information, 14, @"Connection id ""{ConnectionId}"" communication error.");
private static readonly Action<ILogger, string, int, Exception> _connectionDisconnectedWrite =
LoggerMessage.Define<string, int>(LogLevel.Debug, 15, @"Connection id ""{ConnectionId}"" write of ""{count}"" bytes to disconnected client.");
private static readonly Action<ILogger, Exception> _notAllConnectionsClosedGracefully =
LoggerMessage.Define(LogLevel.Debug, 16, "Some connections failed to close gracefully during server shutdown.");
private static readonly Action<ILogger, string, string, Exception> _connectionBadRequest =
LoggerMessage.Define<string, string>(LogLevel.Information, 17, @"Connection id ""{ConnectionId}"" bad request data: ""{message}""");
private static readonly Action<ILogger, string, long, Exception> _connectionHeadResponseBodyWrite =
LoggerMessage.Define<string, long>(LogLevel.Debug, 18, @"Connection id ""{ConnectionId}"" write of ""{count}"" body bytes to non-body HEAD response.");
private static readonly Action<ILogger, string, Exception> _connectionReset =
LoggerMessage.Define<string>(LogLevel.Debug, 19, @"Connection id ""{ConnectionId}"" reset.");
private static readonly Action<ILogger, string, Exception> _requestProcessingError =
LoggerMessage.Define<string>(LogLevel.Information, 20, @"Connection id ""{ConnectionId}"" request processing ended abnormally.");
private static readonly Action<ILogger, Exception> _notAllConnectionsAborted =
LoggerMessage.Define(LogLevel.Debug, 21, "Some connections failed to abort during server shutdown.");
protected readonly ILogger _logger;
static KestrelTrace()
{
_connectionStart = LoggerMessage.Define<string>(LogLevel.Debug, 1, @"Connection id ""{ConnectionId}"" started.");
_connectionStop = LoggerMessage.Define<string>(LogLevel.Debug, 2, @"Connection id ""{ConnectionId}"" stopped.");
// ConnectionRead: Reserved: 3
_connectionPause = LoggerMessage.Define<string>(LogLevel.Debug, 4, @"Connection id ""{ConnectionId}"" paused.");
_connectionResume = LoggerMessage.Define<string>(LogLevel.Debug, 5, @"Connection id ""{ConnectionId}"" resumed.");
_connectionReadFin = LoggerMessage.Define<string>(LogLevel.Debug, 6, @"Connection id ""{ConnectionId}"" received FIN.");
_connectionWriteFin = LoggerMessage.Define<string>(LogLevel.Debug, 7, @"Connection id ""{ConnectionId}"" sending FIN.");
_connectionWroteFin = LoggerMessage.Define<string, int>(LogLevel.Debug, 8, @"Connection id ""{ConnectionId}"" sent FIN with status ""{Status}"".");
_connectionKeepAlive = LoggerMessage.Define<string>(LogLevel.Debug, 9, @"Connection id ""{ConnectionId}"" completed keep alive response.");
_connectionDisconnect = LoggerMessage.Define<string>(LogLevel.Debug, 10, @"Connection id ""{ConnectionId}"" disconnecting.");
// ConnectionWrite: Reserved: 11
// ConnectionWriteCallback: Reserved: 12
_applicationError = LoggerMessage.Define<string>(LogLevel.Error, 13, @"Connection id ""{ConnectionId}"": An unhandled exception was thrown by the application.");
_connectionError = LoggerMessage.Define<string>(LogLevel.Information, 14, @"Connection id ""{ConnectionId}"" communication error.");
_connectionDisconnectedWrite = LoggerMessage.Define<string, int>(LogLevel.Debug, 15, @"Connection id ""{ConnectionId}"" write of ""{count}"" bytes to disconnected client.");
_notAllConnectionsClosedGracefully = LoggerMessage.Define(LogLevel.Debug, 16, "Some connections failed to close gracefully during server shutdown.");
_connectionBadRequest = LoggerMessage.Define<string, string>(LogLevel.Information, 17, @"Connection id ""{ConnectionId}"" bad request data: ""{message}""");
_connectionHeadResponseBodyWrite = LoggerMessage.Define<string, long>(LogLevel.Debug, 18, @"Connection id ""{ConnectionId}"" write of ""{count}"" body bytes to non-body HEAD response.");
_connectionReset = LoggerMessage.Define<string>(LogLevel.Debug, 19, @"Connection id ""{ConnectionId}"" reset.");
_requestProcessingError = LoggerMessage.Define<string>(LogLevel.Information, 20, @"Connection id ""{ConnectionId}"" request processing ended abnormally.");
_notAllConnectionsAborted = LoggerMessage.Define(LogLevel.Debug, 21, "Some connections failed to abort during server shutdown.");
}
public KestrelTrace(ILogger logger)
{
_logger = logger;

View File

@ -10,15 +10,9 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Networking
{
public static class PlatformApis
{
static PlatformApis()
{
IsWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
IsDarwin = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
}
public static bool IsWindows { get; } = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
public static bool IsWindows { get; }
public static bool IsDarwin { get; }
public static bool IsDarwin { get; } = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long VolatileRead(ref long value)