Remove several unnecessary string allocations (#25305)
authorStephen Toub <stoub@microsoft.com>
Fri, 21 Jun 2019 15:56:13 +0000 (11:56 -0400)
committerJan Kotas <jkotas@microsoft.com>
Fri, 21 Jun 2019 15:56:13 +0000 (08:56 -0700)
src/System.Private.CoreLib/shared/System/Diagnostics/Tracing/ActivityTracker.cs
src/System.Private.CoreLib/shared/System/Globalization/DateTimeFormatInfo.cs
src/System.Private.CoreLib/shared/System/Reflection/TypeInfo.cs
src/System.Private.CoreLib/shared/System/TimeZoneInfo.Unix.cs
src/System.Private.CoreLib/src/System/AppContext.CoreCLR.cs
src/System.Private.CoreLib/src/System/Reflection/Emit/EnumBuilder.cs
src/System.Private.CoreLib/src/System/Reflection/Emit/GenericTypeParameterBuilder.cs
src/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs
src/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.cs
src/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs

index 10c4add..254df1a 100644 (file)
@@ -269,15 +269,24 @@ namespace System.Diagnostics.Tracing
         /// </summary>
         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;
+            }
         }
 
         // *******************************************************************************
index b091f7a..b8167d9 100644 (file)
@@ -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;
             }
index e28087f..d841507 100644 (file)
@@ -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;
     }
 }
index 88c8893..f5faca7 100644 (file)
@@ -660,7 +660,7 @@ namespace System
             }
             else if (!tzDirectory.EndsWith(Path.DirectorySeparatorChar))
             {
-                tzDirectory += Path.DirectorySeparatorChar;
+                tzDirectory += PathInternal.DirectorySeparatorCharAsString;
             }
 
             return tzDirectory;
index 572653e..3956bdf 100644 (file)
@@ -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;
         }
     }
index c80df28..4249f28 100644 (file)
@@ -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)!;
         }
 
index eb8035a..2360d60 100644 (file)
@@ -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!;
         }
index 63b4132..d462047 100644 (file)
@@ -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!;
         }
index 806308f..bc29bd6 100644 (file)
@@ -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)!;
         }
 
index 8a555a9..19fa480 100644 (file)
@@ -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(); } }