Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #85432
This change is addressing multiple performance issues:
The first problem arises when creating a TimeZoneInfo object with an alternative zone ID (like using IANA Id on Windows and Windows Id on Linux) using the FindSystemTimeZoneById method. By default, the object returned by GetSystemTimeZones is cached only for the zone ID that matches the OS we are running on. Thus, when we try to create a new object with an alternative ID, we cannot benefit from the cache, and we always attempt to read the zone from the system. On non-Windows OS, this requires accessing the file system and probing for the zone file. To address this issue, we introduce a cache for the alternative ID.
The second issue concerns the unnecessary creation of new zone objects when requesting them with FindSystemTimeZoneById. Even if we have already created the object and cached it, we still create a new one every time. However, since the zone objects do not have writable properties, there is no need to clone them. Instead, we can return the cached object to avoid the allocation and the validation of the new object.
Windows
Before
After
Linux
Before
After