From fc3af1ecfec1c5cd3f6eb99584ae1793379e9a47 Mon Sep 17 00:00:00 2001 From: Justin Van Patten Date: Sun, 12 Mar 2017 16:29:09 -0700 Subject: [PATCH] Remove explicit static constructors 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 constructors of the type to make sure that the static constructor was previously called. This can be avoided by removing explicit static constructors and initializing static fields inline. --- .../HttpRuleParser.cs | 44 ++++++++++--------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/src/Microsoft.Net.Http.Headers/HttpRuleParser.cs b/src/Microsoft.Net.Http.Headers/HttpRuleParser.cs index 835a2f35bb..0ce5833600 100644 --- a/src/Microsoft.Net.Http.Headers/HttpRuleParser.cs +++ b/src/Microsoft.Net.Http.Headers/HttpRuleParser.cs @@ -10,7 +10,7 @@ namespace Microsoft.Net.Http.Headers { internal static class HttpRuleParser { - private static readonly bool[] TokenChars; + private static readonly bool[] TokenChars = CreateTokenChars(); private const int MaxNestedCount = 5; private static readonly string[] DateFormats = new string[] { // "r", // RFC 1123, required output format but too strict for input @@ -43,36 +43,38 @@ namespace Microsoft.Net.Http.Headers // iso-8859-1, Western European (ISO) internal static readonly Encoding DefaultHttpEncoding = Encoding.GetEncoding("iso-8859-1"); - static HttpRuleParser() + private static bool[] CreateTokenChars() { // token = 1* // CTL = - TokenChars = new bool[128]; // everything is false + var tokenChars = new bool[128]; // everything is false for (int i = 33; i < 127; i++) // skip Space (32) & DEL (127) { - TokenChars[i] = true; + tokenChars[i] = true; } // remove separators: these are not valid token characters - TokenChars[(byte)'('] = false; - TokenChars[(byte)')'] = false; - TokenChars[(byte)'<'] = false; - TokenChars[(byte)'>'] = false; - TokenChars[(byte)'@'] = false; - TokenChars[(byte)','] = false; - TokenChars[(byte)';'] = false; - TokenChars[(byte)':'] = false; - TokenChars[(byte)'\\'] = false; - TokenChars[(byte)'"'] = false; - TokenChars[(byte)'/'] = false; - TokenChars[(byte)'['] = false; - TokenChars[(byte)']'] = false; - TokenChars[(byte)'?'] = false; - TokenChars[(byte)'='] = false; - TokenChars[(byte)'{'] = false; - TokenChars[(byte)'}'] = false; + tokenChars[(byte)'('] = false; + tokenChars[(byte)')'] = false; + tokenChars[(byte)'<'] = false; + tokenChars[(byte)'>'] = false; + tokenChars[(byte)'@'] = false; + tokenChars[(byte)','] = false; + tokenChars[(byte)';'] = false; + tokenChars[(byte)':'] = false; + tokenChars[(byte)'\\'] = false; + tokenChars[(byte)'"'] = false; + tokenChars[(byte)'/'] = false; + tokenChars[(byte)'['] = false; + tokenChars[(byte)']'] = false; + tokenChars[(byte)'?'] = false; + tokenChars[(byte)'='] = false; + tokenChars[(byte)'{'] = false; + tokenChars[(byte)'}'] = false; + + return tokenChars; } internal static bool IsTokenChar(char character)