RFR: 8372460: Use EnumMap instead of HashMap for DateTimeFormatter parsing to improve performance [v4]
Chen Liang
liach at openjdk.org
Wed Dec 3 00:21:21 UTC 2025
On Mon, 1 Dec 2025 02:33:33 GMT, Shaojin Wen <swen 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%
>
> Shaojin Wen has updated the pull request incrementally with one additional commit since the last revision:
>
> bug fix
I think from your experiments, maintaining onlyChronoField is indeed way too painful. So I support updating the map in Parsed to use a custom implemented map. This should be not as risky as that map is never exposed to the public users.
src/java.base/share/classes/java/time/format/DateTimeFormatter.java line 1825:
> 1823: return this;
> 1824: }
> 1825: return new DateTimeFormatter(printerParser, locale, decimalStyle, resolverStyle, fields, chrono, zone, onlyChronoField);
Same check comment
src/java.base/share/classes/java/time/format/DateTimeFormatter.java line 1874:
> 1872: resolverFields = Collections.unmodifiableSet(new HashSet<>(resolverFields));
> 1873: }
> 1874: return new DateTimeFormatter(printerParser, locale, decimalStyle, resolverStyle, resolverFields, chrono, zone, onlyChronoField);
Need to check if `resolverFields` has non-ChronoField
-------------
Changes requested by liach (Reviewer).
PR Review: https://git.openjdk.org/jdk/pull/28471#pullrequestreview-3532627956
PR Review Comment: https://git.openjdk.org/jdk/pull/28471#discussion_r2583190506
PR Review Comment: https://git.openjdk.org/jdk/pull/28471#discussion_r2583190245
More information about the core-libs-dev
mailing list