RFR: 8366224: Introduce DecimalDigits.appendPair for efficient two-digit formatting and refactor DateTimeHelper [v5]

Shaojin Wen swen at openjdk.org
Fri Sep 19 01:43:31 UTC 2025


On Mon, 15 Sep 2025 06:11:18 GMT, Shaojin Wen <swen at openjdk.org> wrote:

>> 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.
>
> Shaojin Wen has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Update test/jdk/jdk/internal/util/DecimalDigitsTest.java
>   
>   Co-authored-by: Chen Liang <liach at openjdk.org>

appendPair is a special implementation that uses DecimalDigits for formatting output of second, minute, hour, dayOfMonth, and month values ​​that are >= 0 and less than 100. This eliminates the need to call stringSize or perform the append('0') operation when the value is less than 10, significantly improving performance.

The other widths mentioned by @wangweij can be implemented using repeat('0') + append(int) without adding new methods.

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

PR Comment: https://git.openjdk.org/jdk/pull/26911#issuecomment-3310192257


More information about the core-libs-dev mailing list