Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 3aec163

Browse files
committed
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.
1 parent 3df008e commit 3aec163

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/mscorlib/src/System/TimeZoneInfo.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,16 @@ private AdjustmentRule GetAdjustmentRuleForAmbiguousOffsets(DateTime adjustedTim
284284
/// </summary>
285285
private AdjustmentRule GetPreviousAdjustmentRule(AdjustmentRule rule)
286286
{
287+
Debug.Assert(rule.NoDaylightTransitions, "GetPreviousAdjustmentRule should only be used with NoDaylightTransitions rules.");
288+
287289
AdjustmentRule result = rule;
288290
for (int i = 1; i < _adjustmentRules.Length; i++)
289291
{
290-
if (rule.Equals(_adjustmentRules[i]))
292+
// use ReferenceEquals here instead of AdjustmentRule.Equals because
293+
// ReferenceEquals is much faster. This is safe because all the callers
294+
// of GetPreviousAdjustmentRule pass in a rule that was retrieved from
295+
// _adjustmentRules. A different approach will be needed if this ever changes.
296+
if (ReferenceEquals(rule, _adjustmentRules[i]))
291297
{
292298
result = _adjustmentRules[i - 1];
293299
break;

0 commit comments

Comments
 (0)