Fix perf of DateTime.Now on Unix
authorEric Erhardt <eric.erhardt@microsoft.com>
Thu, 13 Apr 2017 20:21:58 +0000 (15:21 -0500)
committerEric Erhardt <eric.erhardt@microsoft.com>
Thu, 13 Apr 2017 20:21:58 +0000 (15:21 -0500)
Profiling DateTime.Now on Unix shows that a lot of time is spent in GetPreviousAdjustmentRule and AdjustmentRule::Equals. Instead, it is safe to use ReferenceEquals, which increases the performance of DateTime.Now and other time zone operations on Unix.

src/mscorlib/src/System/TimeZoneInfo.cs

index 29ea33a..0176c75 100644 (file)
@@ -284,10 +284,16 @@ namespace System
         /// </summary>
         private AdjustmentRule GetPreviousAdjustmentRule(AdjustmentRule rule)
         {
+            Debug.Assert(rule.NoDaylightTransitions, "GetPreviousAdjustmentRule should only be used with NoDaylightTransitions rules.");
+
             AdjustmentRule result = rule;
             for (int i = 1; i < _adjustmentRules.Length; i++)
             {
-                if (rule.Equals(_adjustmentRules[i]))
+                // use ReferenceEquals here instead of AdjustmentRule.Equals because 
+                // ReferenceEquals is much faster. This is safe because all the callers
+                // of GetPreviousAdjustmentRule pass in a rule that was retrieved from
+                // _adjustmentRules.  A different approach will be needed if this ever changes.
+                if (ReferenceEquals(rule, _adjustmentRules[i]))
                 {
                     result = _adjustmentRules[i - 1];
                     break;