<i18n dev> RFR: 8372460: Use EnumMap instead of HashMap for DateTimeFormatter parsing to improve performance

Shaojin Wen swen at openjdk.org
Tue Nov 25 06:32:38 UTC 2025


On Tue, 25 Nov 2025 00:31:27 GMT, Chen Liang <liach at openjdk.org> wrote:

>> This PR optimizes the parsing performance of DateTimeFormatter by replacing HashMap with EnumMap in scenarios where the keys are exclusively ChronoField enum values.
>> 
>> When parsing date/time strings, DateTimeFormatter creates HashMaps to store intermediate parsed values. HashMap has more overhead for operations compared to specialized map implementations.
>> 
>> Since ChronoField is an enum and all keys in these maps are ChronoField instances, we can use EnumMap instead, which provides better performance for enum keys due to its optimized internal structure.
>> 
>> Parsing scenarios show improvements from 12% to 95%
>
> src/java.base/share/classes/java/time/format/Parsed.java line 134:
> 
>> 132:         if (onlyChronoField) {
>> 133:             return new HashMap<>();
>> 134:         } else {
> 
> Is this condition inverted :thinking: seems like it but how do you get the performance numbers

You're right, it's reversed, it's written incorrectly. And this should be in the constructor, not here. The placement is wrong, but it's still using EnumMap, so the performance is still improved.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/28471#discussion_r2558252666


More information about the i18n-dev mailing list