From 6d2001c67adea285a5e196438bad9b2546c8aa10 Mon Sep 17 00:00:00 2001 From: Justin Van Patten Date: Sat, 18 Mar 2017 12:42:31 -0700 Subject: [PATCH] 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. --- .../Internal/Infrastructure/HttpUtilities.cs | 48 +++++---- .../Internal/Infrastructure/KestrelTrace.cs | 102 ++++++++++-------- .../Internal/Networking/PlatformApis.cs | 10 +- 3 files changed, 87 insertions(+), 73 deletions(-) diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Infrastructure/HttpUtilities.cs b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Infrastructure/HttpUtilities.cs index 32481ef1b1..003c8ccc8e 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Infrastructure/HttpUtilities.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Infrastructure/HttpUtilities.cs @@ -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[] _knownMethods = new Tuple[8]; - - private readonly static string[] _methodNames = new string[9]; - - static HttpUtilities() + private readonly static Tuple[] _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) diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Infrastructure/KestrelTrace.cs b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Infrastructure/KestrelTrace.cs index 9dcd15eedc..94f85518b6 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Infrastructure/KestrelTrace.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Infrastructure/KestrelTrace.cs @@ -12,52 +12,68 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal /// public class KestrelTrace : IKestrelTrace { - private static readonly Action _connectionStart; - private static readonly Action _connectionStop; - private static readonly Action _connectionPause; - private static readonly Action _connectionResume; - private static readonly Action _connectionReadFin; - private static readonly Action _connectionWriteFin; - private static readonly Action _connectionWroteFin; - private static readonly Action _connectionKeepAlive; - private static readonly Action _connectionDisconnect; - private static readonly Action _applicationError; - private static readonly Action _connectionError; - private static readonly Action _connectionDisconnectedWrite; - private static readonly Action _connectionHeadResponseBodyWrite; - private static readonly Action _notAllConnectionsClosedGracefully; - private static readonly Action _notAllConnectionsAborted; - private static readonly Action _connectionBadRequest; - private static readonly Action _connectionReset; - private static readonly Action _requestProcessingError; + private static readonly Action _connectionStart = + LoggerMessage.Define(LogLevel.Debug, 1, @"Connection id ""{ConnectionId}"" started."); + + private static readonly Action _connectionStop = + LoggerMessage.Define(LogLevel.Debug, 2, @"Connection id ""{ConnectionId}"" stopped."); + + // ConnectionRead: Reserved: 3 + + private static readonly Action _connectionPause = + LoggerMessage.Define(LogLevel.Debug, 4, @"Connection id ""{ConnectionId}"" paused."); + + private static readonly Action _connectionResume = + LoggerMessage.Define(LogLevel.Debug, 5, @"Connection id ""{ConnectionId}"" resumed."); + + private static readonly Action _connectionReadFin = + LoggerMessage.Define(LogLevel.Debug, 6, @"Connection id ""{ConnectionId}"" received FIN."); + + private static readonly Action _connectionWriteFin = + LoggerMessage.Define(LogLevel.Debug, 7, @"Connection id ""{ConnectionId}"" sending FIN."); + + private static readonly Action _connectionWroteFin = + LoggerMessage.Define(LogLevel.Debug, 8, @"Connection id ""{ConnectionId}"" sent FIN with status ""{Status}""."); + + private static readonly Action _connectionKeepAlive = + LoggerMessage.Define(LogLevel.Debug, 9, @"Connection id ""{ConnectionId}"" completed keep alive response."); + + private static readonly Action _connectionDisconnect = + LoggerMessage.Define(LogLevel.Debug, 10, @"Connection id ""{ConnectionId}"" disconnecting."); + + // ConnectionWrite: Reserved: 11 + + // ConnectionWriteCallback: Reserved: 12 + + private static readonly Action _applicationError = + LoggerMessage.Define(LogLevel.Error, 13, @"Connection id ""{ConnectionId}"": An unhandled exception was thrown by the application."); + + private static readonly Action _connectionError = + LoggerMessage.Define(LogLevel.Information, 14, @"Connection id ""{ConnectionId}"" communication error."); + + private static readonly Action _connectionDisconnectedWrite = + LoggerMessage.Define(LogLevel.Debug, 15, @"Connection id ""{ConnectionId}"" write of ""{count}"" bytes to disconnected client."); + + private static readonly Action _notAllConnectionsClosedGracefully = + LoggerMessage.Define(LogLevel.Debug, 16, "Some connections failed to close gracefully during server shutdown."); + + private static readonly Action _connectionBadRequest = + LoggerMessage.Define(LogLevel.Information, 17, @"Connection id ""{ConnectionId}"" bad request data: ""{message}"""); + + private static readonly Action _connectionHeadResponseBodyWrite = + LoggerMessage.Define(LogLevel.Debug, 18, @"Connection id ""{ConnectionId}"" write of ""{count}"" body bytes to non-body HEAD response."); + + private static readonly Action _connectionReset = + LoggerMessage.Define(LogLevel.Debug, 19, @"Connection id ""{ConnectionId}"" reset."); + + private static readonly Action _requestProcessingError = + LoggerMessage.Define(LogLevel.Information, 20, @"Connection id ""{ConnectionId}"" request processing ended abnormally."); + + private static readonly Action _notAllConnectionsAborted = + LoggerMessage.Define(LogLevel.Debug, 21, "Some connections failed to abort during server shutdown."); protected readonly ILogger _logger; - static KestrelTrace() - { - _connectionStart = LoggerMessage.Define(LogLevel.Debug, 1, @"Connection id ""{ConnectionId}"" started."); - _connectionStop = LoggerMessage.Define(LogLevel.Debug, 2, @"Connection id ""{ConnectionId}"" stopped."); - // ConnectionRead: Reserved: 3 - _connectionPause = LoggerMessage.Define(LogLevel.Debug, 4, @"Connection id ""{ConnectionId}"" paused."); - _connectionResume = LoggerMessage.Define(LogLevel.Debug, 5, @"Connection id ""{ConnectionId}"" resumed."); - _connectionReadFin = LoggerMessage.Define(LogLevel.Debug, 6, @"Connection id ""{ConnectionId}"" received FIN."); - _connectionWriteFin = LoggerMessage.Define(LogLevel.Debug, 7, @"Connection id ""{ConnectionId}"" sending FIN."); - _connectionWroteFin = LoggerMessage.Define(LogLevel.Debug, 8, @"Connection id ""{ConnectionId}"" sent FIN with status ""{Status}""."); - _connectionKeepAlive = LoggerMessage.Define(LogLevel.Debug, 9, @"Connection id ""{ConnectionId}"" completed keep alive response."); - _connectionDisconnect = LoggerMessage.Define(LogLevel.Debug, 10, @"Connection id ""{ConnectionId}"" disconnecting."); - // ConnectionWrite: Reserved: 11 - // ConnectionWriteCallback: Reserved: 12 - _applicationError = LoggerMessage.Define(LogLevel.Error, 13, @"Connection id ""{ConnectionId}"": An unhandled exception was thrown by the application."); - _connectionError = LoggerMessage.Define(LogLevel.Information, 14, @"Connection id ""{ConnectionId}"" communication error."); - _connectionDisconnectedWrite = LoggerMessage.Define(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(LogLevel.Information, 17, @"Connection id ""{ConnectionId}"" bad request data: ""{message}"""); - _connectionHeadResponseBodyWrite = LoggerMessage.Define(LogLevel.Debug, 18, @"Connection id ""{ConnectionId}"" write of ""{count}"" body bytes to non-body HEAD response."); - _connectionReset = LoggerMessage.Define(LogLevel.Debug, 19, @"Connection id ""{ConnectionId}"" reset."); - _requestProcessingError = LoggerMessage.Define(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; diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Networking/PlatformApis.cs b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Networking/PlatformApis.cs index 6cd2341815..5bb37f60a9 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Networking/PlatformApis.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Networking/PlatformApis.cs @@ -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)