Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Remove a handful of explicit static cctors across corefx
Browse files Browse the repository at this point in the history
Explicit static cctors cause the C# compiler to not mark types as beforefieldinit, which in turn means that the backend compiler needs to add checks to static methods to ensure that the type has been initialized.  We have a bunch of such cctors across corefx that can be easily removed; this commit does so.  In a few places, it also presizes some collections to help avoid unnecessary memory pressure.
  • Loading branch information
stephentoub committed Feb 11, 2016
1 parent e037e4f commit 901825f
Show file tree
Hide file tree
Showing 22 changed files with 381 additions and 357 deletions.
25 changes: 7 additions & 18 deletions src/Common/src/Interop/Unix/libssl/StreamSizes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,16 @@ namespace System.Net
{
internal class StreamSizes
{
private static readonly int s_header;
private static readonly int s_trailer;
private static readonly int s_maximumMessage;

public int header;
public int trailer;
public int maximumMessage;

static StreamSizes()
{
Interop.Ssl.GetStreamSizes(
out s_header,
out s_trailer,
out s_maximumMessage);
}
public readonly int header;
public readonly int trailer;
public readonly int maximumMessage;

internal StreamSizes()
{
header = s_header;
trailer = s_trailer;
maximumMessage = s_maximumMessage;
Interop.Ssl.GetStreamSizes(
out header,
out trailer,
out maximumMessage);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -427,22 +427,6 @@ internal PredefinedTypeInfo(PredefinedType type, Type associatedSystemType, stri
}
}

static PredefinedTypeFacts()
{
#if DEBUG
for (int i = 0; i < (int)PredefinedType.PT_COUNT; i++)
{
System.Diagnostics.Debug.Assert(s_pdTypes[i].type == (PredefinedType)i);
}
#endif
for (int i = 0; i < (int)PredefinedType.PT_COUNT; i++)
{
s_pdTypeNames.Add(s_pdTypes[i].name, (PredefinedType)i);
}
}

private static readonly Dictionary<string, PredefinedType> s_pdTypeNames = new Dictionary<string, PredefinedType>();

private static readonly PredefinedTypeInfo[] s_pdTypes = new PredefinedTypeInfo[] {
new PredefinedTypeInfo(PredefinedType.PT_BYTE, typeof(System.Byte), "System.Byte", true, 0, AggKindEnum.Struct, FUNDTYPE.FT_U1, true),
new PredefinedTypeInfo(PredefinedType.PT_SHORT, typeof(System.Int16), "System.Int16", true, 0, AggKindEnum.Struct, FUNDTYPE.FT_I2, true),
Expand Down Expand Up @@ -571,5 +555,23 @@ static PredefinedTypeFacts()
new PredefinedTypeInfo(PredefinedType.PT_G_IREADONLYLIST, typeof(System.Collections.Generic.IReadOnlyList<>), "System.Collections.Generic.IReadOnlyList`1", false, 1, AggKindEnum.Interface, FUNDTYPE.FT_REF, false),
new PredefinedTypeInfo(PredefinedType.PT_G_IREADONLYCOLLECTION, typeof(System.Collections.Generic.IReadOnlyCollection<>), "System.Collections.Generic.IReadOnlyCollection`1", false, 1, AggKindEnum.Interface, FUNDTYPE.FT_REF, false),
};

private static readonly Dictionary<string, PredefinedType> s_pdTypeNames = CreatePredefinedTypeFacts();

private static Dictionary<string, PredefinedType> CreatePredefinedTypeFacts()
{
var pdTypeNames = new Dictionary<string, PredefinedType>((int)PredefinedType.PT_COUNT);
#if DEBUG
for (int i = 0; i < (int)PredefinedType.PT_COUNT; i++)
{
System.Diagnostics.Debug.Assert(s_pdTypes[i].type == (PredefinedType)i);
}
#endif
for (int i = 0; i < (int)PredefinedType.PT_COUNT; i++)
{
pdTypeNames.Add(s_pdTypes[i].name, (PredefinedType)i);
}
return pdTypeNames;
}
}
}
5 changes: 0 additions & 5 deletions src/Microsoft.Win32.Registry/src/Microsoft/Win32/Registry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ namespace Microsoft.Win32
//This class contains only static members and does not need to be serializable.
public static class Registry
{
[System.Security.SecuritySafeCritical]
static Registry()
{
}

/**
* Current User Key.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,9 @@ internal class MetaDataUtilsSmi

// Hash table to map from clr type object to ExtendedClrTypeCodeMap enum
// this HashTable should only be accessed from DetermineExtendedTypeCode and class ctor for setup.
private static readonly Hashtable s_typeToExtendedTypeCodeMap;
private static readonly Hashtable s_typeToExtendedTypeCodeMap = CreateTypeToExtendedTypeCodeMap();


static MetaDataUtilsSmi()
private static Hashtable CreateTypeToExtendedTypeCodeMap()
{
// Set up type mapping hash table
// Keep this initialization list in the same order as ExtendedClrTypeCode for ease in validating!
Expand Down Expand Up @@ -128,10 +127,9 @@ static MetaDataUtilsSmi()
ht.Add(typeof(IEnumerable<SqlDataRecord>), ExtendedClrTypeCode.IEnumerableOfSqlDataRecord);
ht.Add(typeof(System.TimeSpan), ExtendedClrTypeCode.TimeSpan);
ht.Add(typeof(System.DateTimeOffset), ExtendedClrTypeCode.DateTimeOffset);
s_typeToExtendedTypeCodeMap = ht;
return ht;
}


internal static bool IsCharOrXmlType(SqlDbType type)
{
return IsUnicodeType(type) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,20 @@ internal class SmiMetaDataPropertyCollection
private SmiMetaDataProperty[] _properties;
private bool _isReadOnly;

internal static readonly SmiMetaDataPropertyCollection EmptyInstance;
internal static readonly SmiMetaDataPropertyCollection EmptyInstance = CreateEmptyInstance();

private static SmiMetaDataPropertyCollection CreateEmptyInstance()
{
var emptyInstance = new SmiMetaDataPropertyCollection();
emptyInstance.SetReadOnly();
return emptyInstance;
}

// Singleton empty instances to ensure each property is always non-null
private static readonly SmiDefaultFieldsProperty s_emptyDefaultFields = new SmiDefaultFieldsProperty(new List<bool>());
private static readonly SmiOrderProperty s_emptySortOrder = new SmiOrderProperty(new List<SmiOrderProperty.SmiColumnOrder>());
private static readonly SmiUniqueKeyProperty s_emptyUniqueKey = new SmiUniqueKeyProperty(new List<bool>());

static SmiMetaDataPropertyCollection()
{
EmptyInstance = new SmiMetaDataPropertyCollection();
EmptyInstance.SetReadOnly();
}

internal SmiMetaDataPropertyCollection()
{
_properties = new SmiMetaDataProperty[SelectorCount];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,7 @@ namespace System.Data
/// </summary>
internal class LocaleMapper
{
private static readonly Dictionary<int, LocaleCodePage> _mapper;
public static string LcidToLocaleNameInternal(int lcid)
{
return _mapper[lcid].LocaleName;
}

public static int LocaleNameToAnsiCodePage(string localeName)
{
return _mapper.FirstOrDefault(t => t.Value.LocaleName == localeName).Value.CodePage;
}

public static int GetLcidForLocaleName(string localeName)
{
return _mapper.FirstOrDefault(t => t.Value.LocaleName == localeName).Key;
}

static LocaleMapper()
{
_mapper = new Dictionary<int, LocaleCodePage>
{
private static readonly Dictionary<int, LocaleCodePage> s_mapper = new Dictionary<int, LocaleCodePage>(431) {
#region <<Locale Mapper>>
{1, new LocaleCodePage("ar", 1256)},
{2, new LocaleCodePage("bg", 1251)},
Expand Down Expand Up @@ -466,6 +447,20 @@ static LocaleMapper()
{267268, new LocaleCodePage("zh-MO", 950)}
#endregion
};

public static string LcidToLocaleNameInternal(int lcid)
{
return s_mapper[lcid].LocaleName;
}

public static int LocaleNameToAnsiCodePage(string localeName)
{
return s_mapper.FirstOrDefault(t => t.Value.LocaleName == localeName).Value.CodePage;
}

public static int GetLcidForLocaleName(string localeName)
{
return s_mapper.FirstOrDefault(t => t.Value.LocaleName == localeName).Key;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ private enum Keywords
internal const int KeywordsCount = (int)Keywords.KeywordsCount;
internal const int DeprecatedKeywordsCount = 6;

private static readonly string[] s_validKeywords;
private static readonly Dictionary<string, Keywords> s_keywords;
private static readonly string[] s_validKeywords = CreateValidKeywords();
private static readonly Dictionary<string, Keywords> s_keywords = CreateKeywordsDictionary();

private ApplicationIntent _applicationIntent = DbConnectionStringDefaults.ApplicationIntent;
private string _applicationName = DbConnectionStringDefaults.ApplicationName;
Expand Down Expand Up @@ -101,8 +101,7 @@ private enum Keywords
private bool _replication = DbConnectionStringDefaults.Replication;
private bool _userInstance = DbConnectionStringDefaults.UserInstance;


static SqlConnectionStringBuilder()
private static string[] CreateValidKeywords()
{
string[] validKeywords = new string[KeywordsCount];
validKeywords[(int)Keywords.ApplicationIntent] = DbConnectionStringKeywords.ApplicationIntent;
Expand Down Expand Up @@ -133,8 +132,11 @@ static SqlConnectionStringBuilder()
validKeywords[(int)Keywords.WorkstationID] = DbConnectionStringKeywords.WorkstationID;
validKeywords[(int)Keywords.ConnectRetryCount] = DbConnectionStringKeywords.ConnectRetryCount;
validKeywords[(int)Keywords.ConnectRetryInterval] = DbConnectionStringKeywords.ConnectRetryInterval;
s_validKeywords = validKeywords;
return validKeywords;
}

private static Dictionary<string, Keywords> CreateKeywordsDictionary()
{
Dictionary<string, Keywords> hash = new Dictionary<string, Keywords>(KeywordsCount + SqlConnectionString.SynonymCount, StringComparer.OrdinalIgnoreCase);
hash.Add(DbConnectionStringKeywords.ApplicationIntent, Keywords.ApplicationIntent);
hash.Add(DbConnectionStringKeywords.ApplicationName, Keywords.ApplicationName);
Expand Down Expand Up @@ -184,7 +186,7 @@ static SqlConnectionStringBuilder()
hash.Add(DbConnectionStringSynonyms.User, Keywords.UserID);
hash.Add(DbConnectionStringSynonyms.WSID, Keywords.WorkstationID);
Debug.Assert((KeywordsCount + SqlConnectionString.SynonymCount) == hash.Count, "initial expected size is incorrect");
s_keywords = hash;
return hash;
}

public SqlConnectionStringBuilder() : this((string)null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,31 +225,28 @@ internal static class NtProcessManager
private const string PerfCounterQueryString = "230 232";
internal const int IdleProcessID = 0;

private static Dictionary<String, ValueId> s_valueIds;

static NtProcessManager()
private static readonly Dictionary<String, ValueId> s_valueIds = new Dictionary<string, ValueId>(19)
{
s_valueIds = new Dictionary<String, ValueId>();
s_valueIds.Add("Pool Paged Bytes", ValueId.PoolPagedBytes);
s_valueIds.Add("Pool Nonpaged Bytes", ValueId.PoolNonpagedBytes);
s_valueIds.Add("Elapsed Time", ValueId.ElapsedTime);
s_valueIds.Add("Virtual Bytes Peak", ValueId.VirtualBytesPeak);
s_valueIds.Add("Virtual Bytes", ValueId.VirtualBytes);
s_valueIds.Add("Private Bytes", ValueId.PrivateBytes);
s_valueIds.Add("Page File Bytes", ValueId.PageFileBytes);
s_valueIds.Add("Page File Bytes Peak", ValueId.PageFileBytesPeak);
s_valueIds.Add("Working Set Peak", ValueId.WorkingSetPeak);
s_valueIds.Add("Working Set", ValueId.WorkingSet);
s_valueIds.Add("ID Thread", ValueId.ThreadId);
s_valueIds.Add("ID Process", ValueId.ProcessId);
s_valueIds.Add("Priority Base", ValueId.BasePriority);
s_valueIds.Add("Priority Current", ValueId.CurrentPriority);
s_valueIds.Add("% User Time", ValueId.UserTime);
s_valueIds.Add("% Privileged Time", ValueId.PrivilegedTime);
s_valueIds.Add("Start Address", ValueId.StartAddress);
s_valueIds.Add("Thread State", ValueId.ThreadState);
s_valueIds.Add("Thread Wait Reason", ValueId.ThreadWaitReason);
}
{ "Pool Paged Bytes", ValueId.PoolPagedBytes },
{ "Pool Nonpaged Bytes", ValueId.PoolNonpagedBytes },
{ "Elapsed Time", ValueId.ElapsedTime },
{ "Virtual Bytes Peak", ValueId.VirtualBytesPeak },
{ "Virtual Bytes", ValueId.VirtualBytes },
{ "Private Bytes", ValueId.PrivateBytes },
{ "Page File Bytes", ValueId.PageFileBytes },
{ "Page File Bytes Peak", ValueId.PageFileBytesPeak },
{ "Working Set Peak", ValueId.WorkingSetPeak },
{ "Working Set", ValueId.WorkingSet },
{ "ID Thread", ValueId.ThreadId },
{ "ID Process", ValueId.ProcessId },
{ "Priority Base", ValueId.BasePriority },
{ "Priority Current", ValueId.CurrentPriority },
{ "% User Time", ValueId.UserTime },
{ "% Privileged Time", ValueId.PrivilegedTime },
{ "Start Address", ValueId.StartAddress },
{ "Thread State", ValueId.ThreadState },
{ "Thread Wait Reason", ValueId.ThreadWaitReason }
};

internal static int SystemProcessID
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ namespace System.Net.Http
internal static class WinHttpTraceHelper
{
private const string WinHtpTraceEnvironmentVariable = "WINHTTPHANDLER_TRACE";
private static bool s_TraceEnabled;
private static readonly bool s_traceEnabled = IsTraceEnabledViaEnvironmentVariable();

static WinHttpTraceHelper()
private static bool IsTraceEnabledViaEnvironmentVariable()
{
string env;
try
Expand All @@ -25,13 +25,13 @@ static WinHttpTraceHelper()
env = null;
}

s_TraceEnabled = !string.IsNullOrEmpty(env);
return !string.IsNullOrEmpty(env);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsTraceEnabled()
{
return s_TraceEnabled;
return s_traceEnabled;
}

public static void Trace(string message)
Expand Down
Loading

0 comments on commit 901825f

Please sign in to comment.