Skip to content

Conversation

@wenshao
Copy link
Contributor

@wenshao wenshao commented Aug 5, 2025

The DateTimeFormatterBuilder::FIELD_MAP previously used a Map<Character, TemporalField> for mapping pattern characters to TemporalField
instances. This PR refactors that implementation to use a switch expression instead, which eliminates the need to hold a Map in
memory.

The switch expression approach offers these advantages:

  • No memory overhead for maintaining a HashMap structure
  • More direct character-to-field mapping without hash computation
  • Better code readability and maintainability

This change maintains the same functionality while improving the memory efficiency of pattern character lookup in
DateTimeFormatterBuilder by eliminating the static Map that was previously used for character-to-field mapping.

  • before
image

Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8368825: Use switch expression for DateTimeFormatterBuilder pattern character lookup (Enhancement - P4)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/26634/head:pull/26634
$ git checkout pull/26634

Update a local copy of the PR:
$ git checkout pull/26634
$ git pull https://git.openjdk.org/jdk.git pull/26634/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 26634

View PR using the GUI difftool:
$ git pr show -t 26634

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/26634.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Aug 5, 2025

👋 Welcome back swen! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Aug 5, 2025

@wenshao This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8368825: Use switch expression for DateTimeFormatterBuilder pattern character lookup

Reviewed-by: rriggs, naoto, scolebourne

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 835 new commits pushed to the master branch:

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk
Copy link

openjdk bot commented Aug 5, 2025

@wenshao The following labels will be automatically applied to this pull request:

  • core-libs
  • i18n

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing lists. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added core-libs core-libs-dev@openjdk.org i18n i18n-dev@openjdk.org labels Aug 5, 2025
@wenshao wenshao changed the title Refactor DateTimeFormatterBuilder::FIELD_MAP using array instead of Map Change the DateTimeFormatterBuilder::FIELD_MAP type from Map to Array Aug 5, 2025
@liach
Copy link
Member

liach commented Aug 5, 2025

with key values in the range 'A-Z' and 'a-z'.

You can use a table of size 64, and access the table with & 63.

@wenshao
Copy link
Contributor Author

wenshao commented Aug 6, 2025

with key values in the range 'A-Z' and 'a-z'.

You can use a table of size 64, and access the table with & 63.

This will save memory space, but it will also increase the complexity a little bit.

    @Stable
    private static final TemporalField[] FIELD_MAP = new TemporalField[64];
    static {
        // SDF = SimpleDateFormat
        FIELD_MAP['G' & 0x3f] = ChronoField.ERA;                       // SDF, LDML (different to both for 1/2 chars)
        FIELD_MAP['y' & 0x3f] = ChronoField.YEAR_OF_ERA;               // SDF, LDML
        FIELD_MAP['u' & 0x3f] = ChronoField.YEAR;                      // LDML (different in SDF)
        FIELD_MAP['Q' & 0x3f] = IsoFields.QUARTER_OF_YEAR;             // LDML (removed quarter from 310)
        FIELD_MAP['q' & 0x3f] = IsoFields.QUARTER_OF_YEAR;             // LDML (stand-alone)
        FIELD_MAP['M' & 0x3f] = ChronoField.MONTH_OF_YEAR;             // SDF, LDML
        FIELD_MAP['L' & 0x3f] = ChronoField.MONTH_OF_YEAR;             // SDF, LDML (stand-alone)
        FIELD_MAP['D' & 0x3f] = ChronoField.DAY_OF_YEAR;               // SDF, LDML
        FIELD_MAP['d' & 0x3f] = ChronoField.DAY_OF_MONTH;              // SDF, LDML
        FIELD_MAP['F' & 0x3f] = ChronoField.ALIGNED_WEEK_OF_MONTH;     // SDF, LDML
        FIELD_MAP['E' & 0x3f] = ChronoField.DAY_OF_WEEK;               // SDF, LDML (different to both for 1/2 chars)
        FIELD_MAP['c' & 0x3f] = ChronoField.DAY_OF_WEEK;               // LDML (stand-alone)
        FIELD_MAP['e' & 0x3f] = ChronoField.DAY_OF_WEEK;               // LDML (needs localized week number)
        FIELD_MAP['a' & 0x3f] = ChronoField.AMPM_OF_DAY;               // SDF, LDML
        FIELD_MAP['H' & 0x3f] = ChronoField.HOUR_OF_DAY;               // SDF, LDML
        FIELD_MAP['k' & 0x3f] = ChronoField.CLOCK_HOUR_OF_DAY;         // SDF, LDML
        FIELD_MAP['K' & 0x3f] = ChronoField.HOUR_OF_AMPM;              // SDF, LDML
        FIELD_MAP['h' & 0x3f] = ChronoField.CLOCK_HOUR_OF_AMPM;        // SDF, LDML
        FIELD_MAP['m' & 0x3f] = ChronoField.MINUTE_OF_HOUR;            // SDF, LDML
        FIELD_MAP['s' & 0x3f] = ChronoField.SECOND_OF_MINUTE;          // SDF, LDML
        FIELD_MAP['S' & 0x3f] = ChronoField.NANO_OF_SECOND;            // LDML (SDF uses milli-of-second number)
        FIELD_MAP['A' & 0x3f] = ChronoField.MILLI_OF_DAY;              // LDML
        FIELD_MAP['n' & 0x3f] = ChronoField.NANO_OF_SECOND;            // 310 (proposed for LDML)
        FIELD_MAP['N' & 0x3f] = ChronoField.NANO_OF_DAY;               // 310 (proposed for LDML)
        FIELD_MAP['g' & 0x3f] = JulianFields.MODIFIED_JULIAN_DAY;

    private void parsePattern(String pattern) {
        // ...
        TemporalField field = FIELD_MAP[cur & 0x3f];
        // ...
    }

@wenshao wenshao changed the title Change the DateTimeFormatterBuilder::FIELD_MAP type from Map to Array 8368825: Change the DateTimeFormatterBuilder::FIELD_MAP type from Map to Array Sep 29, 2025
@wenshao wenshao marked this pull request as ready for review September 29, 2025 01:08
@openjdk openjdk bot added the rfr Pull request is ready for review label Sep 29, 2025
@mlbridge
Copy link

mlbridge bot commented Sep 29, 2025

Webrevs

@liach
Copy link
Member

liach commented Sep 29, 2025

A sensible minor optimization. Looking at this code, I think to accomplish real performance gains, we might need some redesign to simplify the branchiness and speed up lookup.

@RogerRiggs
Copy link
Contributor

The suggestion, from Claes, to use and expression switch would work well too.

@wenshao
Copy link
Contributor Author

wenshao commented Sep 30, 2025

j.t.f.DateTimeFormatterBuilder#parsePattern This is a large method with codeSize 1300+. I think optimizing it should be a separate PR.

@liach
Copy link
Member

liach commented Sep 30, 2025

We might just remove this map when we optimize that huge method, which is this map's only user.

Copy link
Contributor

@RogerRiggs RogerRiggs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Sep 30, 2025
@openjdk openjdk bot removed the ready Pull request is ready to be integrated label Sep 30, 2025
@wenshao wenshao changed the title 8368825: Change the DateTimeFormatterBuilder::FIELD_MAP type from Map to Array 8368825: Use switch expression for DateTimeFormatterBuilder pattern character lookup Sep 30, 2025
@openjdk openjdk bot added the ready Pull request is ready to be integrated label Oct 1, 2025
@wenshao
Copy link
Contributor Author

wenshao commented Oct 2, 2025

/integrate

@openjdk
Copy link

openjdk bot commented Oct 2, 2025

Going to push as commit 2c7f738.
Since your change was applied there have been 839 commits pushed to the master branch:

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Oct 2, 2025
@openjdk openjdk bot closed this Oct 2, 2025
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Oct 2, 2025
@openjdk
Copy link

openjdk bot commented Oct 2, 2025

@wenshao Pushed as commit 2c7f738.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core-libs core-libs-dev@openjdk.org i18n i18n-dev@openjdk.org integrated Pull request has been integrated

Development

Successfully merging this pull request may close these issues.

5 participants