From 0bca3c16e17f199bd8734aa1e2579e8d07ffe560 Mon Sep 17 00:00:00 2001 From: Justin Van Patten Date: Mon, 2 Jan 2017 20:41:04 -0800 Subject: [PATCH] TimeZoneInfo: Reduce intermediate allocations in GetTimeZoneIds on Unix (#8769) Avoid intermediate underlying array allocations as items are added to the resulting List by specifying the capacity. Also, change the signature of the private method to return List instead of IEnumerable to avoid the enumerator allocation when enumerating the ids. --- src/mscorlib/src/System/TimeZoneInfo.Unix.cs | 57 +++++++++++++++------------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/src/mscorlib/src/System/TimeZoneInfo.Unix.cs b/src/mscorlib/src/System/TimeZoneInfo.Unix.cs index 1be080d..b94c8b7 100644 --- a/src/mscorlib/src/System/TimeZoneInfo.Unix.cs +++ b/src/mscorlib/src/System/TimeZoneInfo.Unix.cs @@ -230,7 +230,7 @@ namespace System /// /// Lines that start with # are comments and are skipped. /// - private static IEnumerable GetTimeZoneIds(string timeZoneDirectory) + private static List GetTimeZoneIds(string timeZoneDirectory) { string[] zoneTabFileLines = null; try @@ -240,38 +240,41 @@ namespace System catch (IOException) { } catch (UnauthorizedAccessException) { } - List timeZoneIds = new List(); - if (zoneTabFileLines != null) + if (zoneTabFileLines == null) { - foreach (string zoneTabFileLine in zoneTabFileLines) + return new List(); + } + + List timeZoneIds = new List(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); } } } -- 2.7.4