From: Eric Erhardt Date: Thu, 13 Apr 2017 20:21:58 +0000 (-0500) Subject: Fix perf of DateTime.Now on Unix X-Git-Tag: submit/tizen/20210909.063632~11030^2~7215^2~2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=4cfbfdc580521753ec31b444317fba59ebbee0ae;p=platform%2Fupstream%2Fdotnet%2Fruntime.git 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 --- 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;