Skip to content

Commit

Permalink
Fix TimeZoneInfo Perf (#85615)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarekgh authored May 4, 2023
1 parent 87189ec commit fbdf54c
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions src/libraries/System.Private.CoreLib/src/System/TimeZoneInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public DateTimeKind GetCorrespondingKind(TimeZoneInfo? timeZone)

public Dictionary<string, TimeZoneInfo>? _systemTimeZones;
public ReadOnlyCollection<TimeZoneInfo>? _readOnlySystemTimeZones;
public Dictionary<string, TimeZoneInfo>? _timeZonesUsingAlternativeIds;
public bool _allSystemTimeZonesRead;
}

Expand Down Expand Up @@ -1898,6 +1899,9 @@ private static TimeZoneInfoResult TryGetTimeZone(string id, bool dstDisabled, ou
}
}

cachedData._timeZonesUsingAlternativeIds ??= new Dictionary<string, TimeZoneInfo>(StringComparer.OrdinalIgnoreCase);
cachedData._timeZonesUsingAlternativeIds[id] = zone;

Debug.Assert(zone != null);
value = zone;
}
Expand All @@ -1917,17 +1921,26 @@ private static TimeZoneInfoResult TryGetTimeZoneUsingId(string id, bool dstDisab
// check the cache
if (cachedData._systemTimeZones != null)
{
if (cachedData._systemTimeZones.TryGetValue(id, out TimeZoneInfo? match))
if (cachedData._systemTimeZones.TryGetValue(id, out value))
{
if (dstDisabled && match._supportsDaylightSavingTime)
if (dstDisabled && value._supportsDaylightSavingTime)
{
// we found a cache hit but we want a time zone without DST and this one has DST data
value = CreateCustomTimeZone(match._id, match._baseUtcOffset, match._displayName, match._standardDisplayName);
value = CreateCustomTimeZone(value._id, value._baseUtcOffset, value._displayName, value._standardDisplayName);
}
else

return result;
}
}

if (cachedData._timeZonesUsingAlternativeIds != null)
{
if (cachedData._timeZonesUsingAlternativeIds.TryGetValue(id, out value))
{
if (dstDisabled && value._supportsDaylightSavingTime)
{
value = new TimeZoneInfo(match._id, match._baseUtcOffset, match._displayName, match._standardDisplayName,
match._daylightDisplayName, match._adjustmentRules, disableDaylightSavingTime: false, match.HasIanaId);
// we found a cache hit but we want a time zone without DST and this one has DST data
value = CreateCustomTimeZone(value._id, value._baseUtcOffset, value._displayName, value._standardDisplayName);
}

return result;
Expand Down Expand Up @@ -1958,7 +1971,7 @@ private static TimeZoneInfoResult TryGetTimeZoneFromLocalMachine(string id, bool
{
TimeZoneInfoResult result;

result = TryGetTimeZoneFromLocalMachine(id, out TimeZoneInfo? match, out e);
result = TryGetTimeZoneFromLocalMachine(id, out value, out e);

if (result == TimeZoneInfoResult.Success)
{
Expand All @@ -1971,24 +1984,15 @@ private static TimeZoneInfoResult TryGetTimeZoneFromLocalMachine(string id, bool
// uses reference equality with the Utc object.
if (!id.Equals(UtcId, StringComparison.OrdinalIgnoreCase))
{
cachedData._systemTimeZones.Add(id, match!);
cachedData._systemTimeZones.Add(id, value!);
}

if (dstDisabled && match!._supportsDaylightSavingTime)
if (dstDisabled && value!._supportsDaylightSavingTime)
{
// we found a cache hit but we want a time zone without DST and this one has DST data
value = CreateCustomTimeZone(match._id, match._baseUtcOffset, match._displayName, match._standardDisplayName);
}
else
{
value = new TimeZoneInfo(match!._id, match._baseUtcOffset, match._displayName, match._standardDisplayName,
match._daylightDisplayName, match._adjustmentRules, disableDaylightSavingTime: false, match.HasIanaId);
value = CreateCustomTimeZone(value._id, value._baseUtcOffset, value._displayName, value._standardDisplayName);
}
}
else
{
value = null;
}

return result;
}
Expand Down

0 comments on commit fbdf54c

Please sign in to comment.