TimeZoneInfo: Reduce intermediate allocations in GetTimeZoneIds on Unix (dotnet/corec...
authorJustin Van Patten <jvp@justinvp.com>
Tue, 3 Jan 2017 04:41:04 +0000 (20:41 -0800)
committerJan Kotas <jkotas@microsoft.com>
Tue, 3 Jan 2017 04:41:04 +0000 (20:41 -0800)
Avoid intermediate underlying array allocations as items are added to
the resulting List<string> by specifying the capacity. Also, change
the signature of the private method to return List<string> instead of
IEnumerable<string> to avoid the enumerator allocation when enumerating
the ids.

Commit migrated from https://github.com/dotnet/coreclr/commit/0bca3c16e17f199bd8734aa1e2579e8d07ffe560

src/coreclr/src/mscorlib/src/System/TimeZoneInfo.Unix.cs

index 1be080d..b94c8b7 100644 (file)
@@ -230,7 +230,7 @@ namespace System
         /// <remarks>
         /// Lines that start with # are comments and are skipped.
         /// </remarks>
-        private static IEnumerable<string> GetTimeZoneIds(string timeZoneDirectory)
+        private static List<string> GetTimeZoneIds(string timeZoneDirectory)
         {
             string[] zoneTabFileLines = null;
             try
@@ -240,38 +240,41 @@ namespace System
             catch (IOException) { }
             catch (UnauthorizedAccessException) { }
 
-            List<string> timeZoneIds = new List<string>();
-            if (zoneTabFileLines != null)
+            if (zoneTabFileLines == null)
             {
-                foreach (string zoneTabFileLine in zoneTabFileLines)
+                return new List<string>();
+            }
+
+            List<string> timeZoneIds = new List<string>(zoneTabFileLines.Length);
+
+            foreach (string zoneTabFileLine in zoneTabFileLines)
+            {
+                if (!string.IsNullOrEmpty(zoneTabFileLine) && zoneTabFileLine[0] != '#')
                 {
-                    if (!string.IsNullOrEmpty(zoneTabFileLine) && !zoneTabFileLine.StartsWith("#"))
-                    {
-                        // the format of the line is "country-code \t coordinates \t TimeZone Id \t comments"
+                    // the format of the line is "country-code \t coordinates \t TimeZone Id \t comments"
 
-                        int firstTabIndex = zoneTabFileLine.IndexOf('\t');
-                        if (firstTabIndex != -1)
+                    int firstTabIndex = zoneTabFileLine.IndexOf('\t');
+                    if (firstTabIndex != -1)
+                    {
+                        int secondTabIndex = zoneTabFileLine.IndexOf('\t', firstTabIndex + 1);
+                        if (secondTabIndex != -1)
                         {
-                            int secondTabIndex = zoneTabFileLine.IndexOf('\t', firstTabIndex + 1);
-                            if (secondTabIndex != -1)
+                            string timeZoneId;
+                            int startIndex = secondTabIndex + 1;
+                            int thirdTabIndex = zoneTabFileLine.IndexOf('\t', startIndex);
+                            if (thirdTabIndex != -1)
                             {
-                                string timeZoneId;
-                                int startIndex = secondTabIndex + 1;
-                                int thirdTabIndex = zoneTabFileLine.IndexOf('\t', startIndex);
-                                if (thirdTabIndex != -1)
-                                {
-                                    int length = thirdTabIndex - startIndex;
-                                    timeZoneId = zoneTabFileLine.Substring(startIndex, length);
-                                }
-                                else
-                                {
-                                    timeZoneId = zoneTabFileLine.Substring(startIndex);
-                                }
+                                int length = thirdTabIndex - startIndex;
+                                timeZoneId = zoneTabFileLine.Substring(startIndex, length);
+                            }
+                            else
+                            {
+                                timeZoneId = zoneTabFileLine.Substring(startIndex);
+                            }
 
-                                if (!string.IsNullOrEmpty(timeZoneId))
-                                {
-                                    timeZoneIds.Add(timeZoneId);
-                                }
+                            if (!string.IsNullOrEmpty(timeZoneId))
+                            {
+                                timeZoneIds.Add(timeZoneId);
                             }
                         }
                     }