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

Commit

Permalink
TimeZoneInfo: Avoid cloning privately-created ArgumentRule[] arrays (#…
Browse files Browse the repository at this point in the history
…8575)

TimeZoneInfo currently always creates a defensive copy of the specified
ArgumentRule[] array when created. This makes sense for the public
static factory methods. However, there's no need to create a defensive
copy of arrays created privately as part of its implementation (e.g.
reading the rules from the registry/disk). This change avoids the
unnecessary cloning.
  • Loading branch information
justinvp authored and tarekgh committed Dec 10, 2016
1 parent ff38a7f commit cfea883
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/mscorlib/src/System/TimeZoneInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1262,16 +1262,13 @@ private void GetDisplayName(Interop.GlobalizationInterop.TimeZoneDisplayNameType
Boolean adjustmentRulesSupportDst;
ValidateTimeZoneInfo(id, baseUtcOffset, adjustmentRules, out adjustmentRulesSupportDst);

if (!disableDaylightSavingTime && adjustmentRules != null && adjustmentRules.Length > 0) {
m_adjustmentRules = (AdjustmentRule[])adjustmentRules.Clone();
}

m_id = id;
m_baseUtcOffset = baseUtcOffset;
m_displayName = displayName;
m_standardDisplayName = standardDisplayName;
m_daylightDisplayName = (disableDaylightSavingTime ? null : daylightDisplayName);
m_supportsDaylightSavingTime = adjustmentRulesSupportDst && !disableDaylightSavingTime;
m_adjustmentRules = adjustmentRules;
}

// -------- SECTION: factory methods -----------------*
Expand Down Expand Up @@ -1312,7 +1309,7 @@ private void GetDisplayName(Interop.GlobalizationInterop.TimeZoneDisplayNameType
String daylightDisplayName,
AdjustmentRule [] adjustmentRules) {

return new TimeZoneInfo(
return CreateCustomTimeZone(
id,
baseUtcOffset,
displayName,
Expand Down Expand Up @@ -1341,7 +1338,11 @@ private void GetDisplayName(Interop.GlobalizationInterop.TimeZoneDisplayNameType
AdjustmentRule [] adjustmentRules,
Boolean disableDaylightSavingTime) {

return new TimeZoneInfo(
if (!disableDaylightSavingTime && adjustmentRules?.Length > 0) {
adjustmentRules = (AdjustmentRule[])adjustmentRules.Clone();
}

return new TimeZoneInfo(
id,
baseUtcOffset,
displayName,
Expand Down Expand Up @@ -5073,7 +5074,7 @@ private enum State {
AdjustmentRule[] rules = s.GetNextAdjustmentRuleArrayValue(false);

try {
return TimeZoneInfo.CreateCustomTimeZone(id, baseUtcOffset, displayName, standardName, daylightName, rules);
return new TimeZoneInfo(id, baseUtcOffset, displayName, standardName, daylightName, rules, disableDaylightSavingTime: false);
}
catch (ArgumentException ex) {
throw new SerializationException(Environment.GetResourceString("Serialization_InvalidData"), ex);
Expand Down

0 comments on commit cfea883

Please sign in to comment.