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

Roger Riggs rriggs at openjdk.org
Mon Nov 3 16:03:07 UTC 2025


On Mon, 3 Nov 2025 15:55:45 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 with a new target base due to a merge or a rebase. The pull request now contains 12 commits:
> 
>  - Merge remote-tracking branch 'upstream/master' into appendPair_202508
>  - Merge remote-tracking branch 'upstream/master' into appendPair_202508
>  - appendPair instead of appendLatin1(char, char)
>  - Revert "appendPair instead of append(char, char)"
>    
>    This reverts commit d969a235e54da4ac96b7205982624f52305cae31.
>  - appendPair instead of append(char, char)
>  - Update test/jdk/jdk/internal/util/DecimalDigitsTest.java
>    
>    Co-authored-by: Chen Liang <liach at openjdk.org>
>  - Merge remote-tracking branch 'upstream/master' into appendPair_202508
>    
>    # Conflicts:
>    #	src/java.base/share/classes/jdk/internal/access/JavaLangAccess.java
>  - Add DecimalDigitsTest to verify appendPair method with LATIN1 and UTF16 encoding
>  - Use DecimalDigits.appendPair for formatting in time classes
>    
>    This change modifies the toString() methods in MonthDay, YearMonth, ZoneOffset, and ChronoLocalDateImpl to use DecimalDigits.appendPair for formatting two-digit numbers. This provides a more efficient and consistent way to format these values.
>    
>    Also added a comment in ChronoLocalDateImpl.toString() to explain why get() is used instead of getLong() for performance reasons, as the values are guaranteed to be within the int range for all chronologies.
>    
>    Co-authored-by: Qwen-Coder <qwen-coder at alibabacloud.com>
>  - Optimize year formatting in DateTimeHelper by reducing modulo operation
>    
>    Co-authored-by: Qwen-Coder <qwen-coder at 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.
>  - ... and 2 more: https://git.openjdk.org/jdk/compare/02a72174...678e3066

src/java.base/share/classes/java/lang/AbstractStringBuilder.java line 918:

> 916:             Unsafe unsafe = Unsafe.getUnsafe();
> 917:             unsafe.putChar(value, Unsafe.ARRAY_BYTE_BASE_OFFSET + count, c1);
> 918:             unsafe.putChar(value, Unsafe.ARRAY_BYTE_BASE_OFFSET + count + 1, c2);

Do not introduce Unsafe usage; regular array access is adequate.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/26911#discussion_r2487009000


More information about the core-libs-dev mailing list