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

Commit fc3af1e

Browse files
justinvpTratcher
authored andcommitted
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.
1 parent 2cafa43 commit fc3af1e

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

src/Microsoft.Net.Http.Headers/HttpRuleParser.cs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Microsoft.Net.Http.Headers
1010
{
1111
internal static class HttpRuleParser
1212
{
13-
private static readonly bool[] TokenChars;
13+
private static readonly bool[] TokenChars = CreateTokenChars();
1414
private const int MaxNestedCount = 5;
1515
private static readonly string[] DateFormats = new string[] {
1616
// "r", // RFC 1123, required output format but too strict for input
@@ -43,36 +43,38 @@ internal static class HttpRuleParser
4343
// iso-8859-1, Western European (ISO)
4444
internal static readonly Encoding DefaultHttpEncoding = Encoding.GetEncoding("iso-8859-1");
4545

46-
static HttpRuleParser()
46+
private static bool[] CreateTokenChars()
4747
{
4848
// token = 1*<any CHAR except CTLs or separators>
4949
// CTL = <any US-ASCII control character (octets 0 - 31) and DEL (127)>
5050

51-
TokenChars = new bool[128]; // everything is false
51+
var tokenChars = new bool[128]; // everything is false
5252

5353
for (int i = 33; i < 127; i++) // skip Space (32) & DEL (127)
5454
{
55-
TokenChars[i] = true;
55+
tokenChars[i] = true;
5656
}
5757

5858
// remove separators: these are not valid token characters
59-
TokenChars[(byte)'('] = false;
60-
TokenChars[(byte)')'] = false;
61-
TokenChars[(byte)'<'] = false;
62-
TokenChars[(byte)'>'] = false;
63-
TokenChars[(byte)'@'] = false;
64-
TokenChars[(byte)','] = false;
65-
TokenChars[(byte)';'] = false;
66-
TokenChars[(byte)':'] = false;
67-
TokenChars[(byte)'\\'] = false;
68-
TokenChars[(byte)'"'] = false;
69-
TokenChars[(byte)'/'] = false;
70-
TokenChars[(byte)'['] = false;
71-
TokenChars[(byte)']'] = false;
72-
TokenChars[(byte)'?'] = false;
73-
TokenChars[(byte)'='] = false;
74-
TokenChars[(byte)'{'] = false;
75-
TokenChars[(byte)'}'] = false;
59+
tokenChars[(byte)'('] = false;
60+
tokenChars[(byte)')'] = false;
61+
tokenChars[(byte)'<'] = false;
62+
tokenChars[(byte)'>'] = false;
63+
tokenChars[(byte)'@'] = false;
64+
tokenChars[(byte)','] = false;
65+
tokenChars[(byte)';'] = false;
66+
tokenChars[(byte)':'] = false;
67+
tokenChars[(byte)'\\'] = false;
68+
tokenChars[(byte)'"'] = false;
69+
tokenChars[(byte)'/'] = false;
70+
tokenChars[(byte)'['] = false;
71+
tokenChars[(byte)']'] = false;
72+
tokenChars[(byte)'?'] = false;
73+
tokenChars[(byte)'='] = false;
74+
tokenChars[(byte)'{'] = false;
75+
tokenChars[(byte)'}'] = false;
76+
77+
return tokenChars;
7678
}
7779

7880
internal static bool IsTokenChar(char character)

0 commit comments

Comments
 (0)