From 4cfbfdc580521753ec31b444317fba59ebbee0ae Mon Sep 17 00:00:00 2001 From: Eric Erhardt Date: Thu, 13 Apr 2017 15:21:58 -0500 Subject: [PATCH] Fix perf of DateTime.Now on Unix 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. Commit migrated from https://github.com/dotnet/coreclr/commit/3aec163d4a1431b89f6e1ec22d9b88c13b399a66 --- src/coreclr/src/mscorlib/src/System/TimeZoneInfo.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/coreclr/src/mscorlib/src/System/TimeZoneInfo.cs b/src/coreclr/src/mscorlib/src/System/TimeZoneInfo.cs index 29ea33a..0176c75 100644 --- a/src/coreclr/src/mscorlib/src/System/TimeZoneInfo.cs +++ b/src/coreclr/src/mscorlib/src/System/TimeZoneInfo.cs @@ -284,10 +284,16 @@ namespace System /// 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; -- 2.7.4