From fc38c39b2eb534917edeab99a1fe8fc057f66af2 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Fri, 21 Jun 2019 11:56:13 -0400 Subject: [PATCH] Remove several unnecessary string allocations (#25305) --- .../System/Diagnostics/Tracing/ActivityTracker.cs | 21 +++++++++++++++------ .../System/Globalization/DateTimeFormatInfo.cs | 10 +++------- .../shared/System/Reflection/TypeInfo.cs | 10 ++++++++++ .../shared/System/TimeZoneInfo.Unix.cs | 2 +- .../src/System/AppContext.CoreCLR.cs | 2 +- .../src/System/Reflection/Emit/EnumBuilder.cs | 16 +--------------- .../Reflection/Emit/GenericTypeParameterBuilder.cs | 16 +--------------- .../src/System/Reflection/Emit/SymbolType.cs | 16 +--------------- .../src/System/Reflection/Emit/TypeBuilder.cs | 16 +--------------- .../Reflection/Emit/TypeBuilderInstantiation.cs | 7 +++---- 10 files changed, 37 insertions(+), 79 deletions(-) diff --git a/src/System.Private.CoreLib/shared/System/Diagnostics/Tracing/ActivityTracker.cs b/src/System.Private.CoreLib/shared/System/Diagnostics/Tracing/ActivityTracker.cs index 10c4add..254df1a 100644 --- a/src/System.Private.CoreLib/shared/System/Diagnostics/Tracing/ActivityTracker.cs +++ b/src/System.Private.CoreLib/shared/System/Diagnostics/Tracing/ActivityTracker.cs @@ -269,15 +269,24 @@ namespace System.Diagnostics.Tracing /// private string NormalizeActivityName(string providerName, string activityName, int task) { + // We use provider name to distinguish between activities from different providers. + if (activityName.EndsWith(EventSource.s_ActivityStartSuffix, StringComparison.Ordinal)) - activityName = activityName.Substring(0, activityName.Length - EventSource.s_ActivityStartSuffix.Length); + { + return string.Concat(providerName, activityName.AsSpan(0, activityName.Length - EventSource.s_ActivityStartSuffix.Length)); + } else if (activityName.EndsWith(EventSource.s_ActivityStopSuffix, StringComparison.Ordinal)) - activityName = activityName.Substring(0, activityName.Length - EventSource.s_ActivityStopSuffix.Length); + { + return string.Concat(providerName, activityName.AsSpan(0, activityName.Length - EventSource.s_ActivityStopSuffix.Length)); + } else if (task != 0) - activityName = "task" + task.ToString(); - - // We use provider name to distinguish between activities from different providers. - return providerName + activityName; + { + return providerName + "task" + task.ToString(); + } + else + { + return providerName + activityName; + } } // ******************************************************************************* diff --git a/src/System.Private.CoreLib/shared/System/Globalization/DateTimeFormatInfo.cs b/src/System.Private.CoreLib/shared/System/Globalization/DateTimeFormatInfo.cs index b091f7a..b8167d9 100644 --- a/src/System.Private.CoreLib/shared/System/Globalization/DateTimeFormatInfo.cs +++ b/src/System.Private.CoreLib/shared/System/Globalization/DateTimeFormatInfo.cs @@ -1046,8 +1046,6 @@ namespace System.Globalization { if (dateTimeOffsetPattern == null) { - string dateTimePattern = ShortDatePattern + " " + LongTimePattern; - /* LongTimePattern might contain a "z" as part of the format string in which case we don't want to append a time zone offset */ bool foundZ = false; @@ -1088,12 +1086,10 @@ namespace System.Globalization } } - if (!foundZ) - { - dateTimePattern = dateTimePattern + " zzz"; - } + dateTimeOffsetPattern = foundZ ? + ShortDatePattern + " " + LongTimePattern : + ShortDatePattern + " " + LongTimePattern + " zzz"; - dateTimeOffsetPattern = dateTimePattern; } return dateTimeOffsetPattern; } diff --git a/src/System.Private.CoreLib/shared/System/Reflection/TypeInfo.cs b/src/System.Private.CoreLib/shared/System/Reflection/TypeInfo.cs index e28087f..d841507 100644 --- a/src/System.Private.CoreLib/shared/System/Reflection/TypeInfo.cs +++ b/src/System.Private.CoreLib/shared/System/Reflection/TypeInfo.cs @@ -79,6 +79,16 @@ namespace System.Reflection return false; } + internal static string GetRankString(int rank) + { + if (rank <= 0) + throw new IndexOutOfRangeException(); + + return rank == 1 ? + "[*]" : + "[" + new string(',', rank - 1) + "]"; + } + private const BindingFlags DeclaredOnlyLookup = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly; } } diff --git a/src/System.Private.CoreLib/shared/System/TimeZoneInfo.Unix.cs b/src/System.Private.CoreLib/shared/System/TimeZoneInfo.Unix.cs index 88c8893..f5faca7 100644 --- a/src/System.Private.CoreLib/shared/System/TimeZoneInfo.Unix.cs +++ b/src/System.Private.CoreLib/shared/System/TimeZoneInfo.Unix.cs @@ -660,7 +660,7 @@ namespace System } else if (!tzDirectory.EndsWith(Path.DirectorySeparatorChar)) { - tzDirectory += Path.DirectorySeparatorChar; + tzDirectory += PathInternal.DirectorySeparatorCharAsString; } return tzDirectory; diff --git a/src/System.Private.CoreLib/src/System/AppContext.CoreCLR.cs b/src/System.Private.CoreLib/src/System/AppContext.CoreCLR.cs index 572653e..3956bdf 100644 --- a/src/System.Private.CoreLib/src/System/AppContext.CoreCLR.cs +++ b/src/System.Private.CoreLib/src/System/AppContext.CoreCLR.cs @@ -22,7 +22,7 @@ namespace System // Fallback path for hosts that do not set APP_CONTEXT_BASE_DIRECTORY explicitly string? directory = Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location); if (directory != null && !Path.EndsInDirectorySeparator(directory)) - directory += Path.DirectorySeparatorChar; + directory += PathInternal.DirectorySeparatorCharAsString; return directory ?? string.Empty; } } diff --git a/src/System.Private.CoreLib/src/System/Reflection/Emit/EnumBuilder.cs b/src/System.Private.CoreLib/src/System/Reflection/Emit/EnumBuilder.cs index c80df28..4249f28 100644 --- a/src/System.Private.CoreLib/src/System/Reflection/Emit/EnumBuilder.cs +++ b/src/System.Private.CoreLib/src/System/Reflection/Emit/EnumBuilder.cs @@ -364,21 +364,7 @@ namespace System.Reflection.Emit public override Type MakeArrayType(int rank) { - if (rank <= 0) - throw new IndexOutOfRangeException(); - - string szrank = ""; - if (rank == 1) - { - szrank = "*"; - } - else - { - for (int i = 1; i < rank; i++) - szrank += ","; - } - - string s = string.Format(CultureInfo.InvariantCulture, "[{0}]", szrank); // [,,] + string s = GetRankString(rank); return SymbolType.FormCompoundType(s, this, 0)!; } diff --git a/src/System.Private.CoreLib/src/System/Reflection/Emit/GenericTypeParameterBuilder.cs b/src/System.Private.CoreLib/src/System/Reflection/Emit/GenericTypeParameterBuilder.cs index eb8035a..2360d60 100644 --- a/src/System.Private.CoreLib/src/System/Reflection/Emit/GenericTypeParameterBuilder.cs +++ b/src/System.Private.CoreLib/src/System/Reflection/Emit/GenericTypeParameterBuilder.cs @@ -73,21 +73,7 @@ namespace System.Reflection.Emit public override Type MakeArrayType(int rank) { - if (rank <= 0) - throw new IndexOutOfRangeException(); - - string szrank = ""; - if (rank == 1) - { - szrank = "*"; - } - else - { - for (int i = 1; i < rank; i++) - szrank += ","; - } - - string s = string.Format(CultureInfo.InvariantCulture, "[{0}]", szrank); // [,,] + string s = GetRankString(rank); SymbolType? st = SymbolType.FormCompoundType(s, this, 0) as SymbolType; return st!; } diff --git a/src/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs b/src/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs index 63b4132..d462047 100644 --- a/src/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs +++ b/src/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs @@ -285,21 +285,7 @@ namespace System.Reflection.Emit public override Type MakeArrayType(int rank) { - if (rank <= 0) - throw new IndexOutOfRangeException(); - - string szrank = ""; - if (rank == 1) - { - szrank = "*"; - } - else - { - for (int i = 1; i < rank; i++) - szrank += ","; - } - - string s = string.Format(CultureInfo.InvariantCulture, "[{0}]", szrank); // [,,] + string s = GetRankString(rank); SymbolType? st = SymbolType.FormCompoundType(m_format + s, m_baseType, 0) as SymbolType; return st!; } diff --git a/src/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.cs b/src/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.cs index 806308f..bc29bd6 100644 --- a/src/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.cs +++ b/src/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.cs @@ -1140,21 +1140,7 @@ namespace System.Reflection.Emit public override Type MakeArrayType(int rank) { - if (rank <= 0) - throw new IndexOutOfRangeException(); - - string szrank = ""; - if (rank == 1) - { - szrank = "*"; - } - else - { - for (int i = 1; i < rank; i++) - szrank += ","; - } - - string s = string.Format(CultureInfo.InvariantCulture, "[{0}]", szrank); // [,,] + string s = GetRankString(rank); return SymbolType.FormCompoundType(s, this, 0)!; } diff --git a/src/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs b/src/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs index 8a555a9..19fa480 100644 --- a/src/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs +++ b/src/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs @@ -90,11 +90,10 @@ namespace System.Reflection.Emit if (rank <= 0) throw new IndexOutOfRangeException(); - string comma = ""; - for (int i = 1; i < rank; i++) - comma += ","; + string s = rank == 1 ? + "[]" : + "[" + new string(',', rank - 1) + "]"; - string s = string.Format(CultureInfo.InvariantCulture, "[{0}]", comma); return SymbolType.FormCompoundType(s, this, 0)!; } public override Guid GUID { get { throw new NotSupportedException(); } } -- 2.7.4