Skip to content

Introduce DecimalDigits.appendPair for efficient two-digit formatting and refactor DateTimeHelper #26911

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

wenshao
Copy link
Contributor

@wenshao wenshao commented Aug 23, 2025

This PR introduces a new efficient API for appending two-digit integers to StringBuilders and refactors DateTimeHelper to leverage this new functionality.

Changes include:

  1. New appendPair method for efficient two-digit integer formatting (00-99):

    • Added AbstractStringBuilder.appendPair(int i) with core implementation
    • Added JavaLangAccess.appendPair(StringBuilder, int) for internal access
    • Added System.JavaLangAccessImpl.appendPair(StringBuilder, int) bridge
    • Added DecimalDigits.appendPair(StringBuilder, int) public static utility method
    • Enhanced Javadoc documentation for all new methods
  2. Refactored DateTimeHelper to use the new DecimalDigits.appendPair:

    • Updated DateTimeHelper.formatTo methods for LocalDate and LocalTime
    • Replaced manual formatting logic with the new efficient two-digit appending
    • Improved code clarity and consistency in date/time formatting

These changes improve code clarity and performance when formatting two-digit numbers, particularly in date/time formatting scenarios.


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

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 26911

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

Using diff file

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

wenshao and others added 2 commits August 23, 2025 10:47
…\n\nThis change adds a new internal API to efficiently append two-digit integers\n(00-99) to a StringBuilder. It includes:\n- AbstractStringBuilder.appendPair(int i): The core implementation.\n- JavaLangAccess.appendPair(StringBuilder, int): For internal access.\n- System.JavaLangAccessImpl.appendPair(StringBuilder, int): Bridge to AbstractStringBuilder.\n- DecimalDigits.appendPair(StringBuilder, int): Public static utility method.\n\nImproved Javadoc comments for clarity and consistency across all new methods.

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
… formatting\n\nThis change updates DateTimeHelper.formatTo methods for LocalDate and LocalTime\nto use the new DecimalDigits.appendPair method for formatting month, day, hour,\nminute, and second components. This improves code clarity and leverages the\nnewly introduced efficient two-digit integer appending functionality.

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
@bridgekeeper
Copy link

bridgekeeper bot commented Aug 23, 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 23, 2025

❗ This change is not yet ready to be integrated.
See the Progress checklist in the description for automated requirements.

@openjdk
Copy link

openjdk bot commented Aug 23, 2025

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

  • core-libs
  • security

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 security security-dev@openjdk.org core-libs core-libs-dev@openjdk.org labels Aug 23, 2025
Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>

Refactored the year formatting logic to use subtraction instead of modulo for calculating the lower two digits, which can be slightly more efficient. Added a comment to clarify the safety of the input range for DecimalDigits.appendPair.
@wenshao
Copy link
Contributor Author

wenshao commented Aug 23, 2025

I've run performance tests comparing the baseline (7b9969d) and this PR's branch (appendPair_202508) using the
make test TEST="micro:java.time.ToStringBench benchmark. The results show significant performance improvements across multiple time
formatting operations:

Performance Comparison Results

Benchmark Baseline (7b9969d) This PR (appendPair_202508) Improvement
instantToString 9.078 ± 0.504 ops/ms 12.904 ± 3.810 ops/ms ~42% faster
localDateTimeToString 15.666 ± 1.916 ops/ms 31.306 ± 5.758 ops/ms ~100% faster
localDateToString 23.257 ± 1.596 ops/ms 24.976 ± 2.456 ops/ms ~7.4% faster
localTimeToString 22.727 ± 1.991 ops/ms 37.266 ± 1.453 ops/ms ~64% faster
zonedDateTimeToString 13.094 ± 0.053 ops/ms 24.040 ± 8.511 ops/ms ~84% faster

The most significant improvements are seen in localDateTimeToString and localTimeToString operations, which are now
approximately 2x faster. This validates that the changes in this PR provide meaningful performance benefits for time
formatting operations.

Tests were run on macOS ARM64 The performance gains are consistent with the optimization goals of this PR.

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 security security-dev@openjdk.org
Development

Successfully merging this pull request may close these issues.

1 participant