Cache ZoneId in ZonedDateTimeAdapter to reduce allocation pressure#34
Merged
Merged
Conversation
ZoneId.systemDefault() internally calls TimeZone.getDefault() which clones the ZoneInfo object on every invocation. Since parse() is called for every datetime field in SIRI XML messages, this creates significant allocation pressure. Cache the default ZoneId and ZoneOffset as static final constants to eliminate this per-call overhead.
lassetyr
approved these changes
Feb 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
ZoneId.systemDefault()andZoneOffset.ofHours(0)asstatic finalconstants inZonedDateTimeAdapterinstead of calling them on everyparse()invocation.Motivation
JFR profiling of OTP2 with a live SIRI-ET feed showed that
ZonedDateTimeAdapter.parse()consumed 5.9% of total CPU during real-time processing. The root cause isZoneId.systemDefault(), which internally callsTimeZone.getDefault()— this clones theZoneInfoobject on every call. With thousands of datetime fields per SIRI message, this produced ~717 MB of sampledZoneInfoallocations and ~3.9 GB ofThreadLocalMap$Entryallocations over an 11-minute profiling window (~150 MB/s total allocation rate).Caching the
ZoneIdandZoneOffsetas class-level constants eliminates this per-call overhead entirely.Trade-off
By caching
ZoneId.systemDefault()at class load time, the default zone is resolved once and reused for the lifetime of the JVM. This means that any runtime changes to the default timezone (viaTimeZone.setDefault()orSystem.setProperty("user.timezone", ...)) will not be picked up byZonedDateTimeAdapter. In practice this is a non-issue for server applications like OTP where the timezone is set at startup and never changed, but it is a behavioural change worth noting.