RFR: 8366224: Introduce DecimalDigits.appendPair for efficient two-digit formatting and refactor DateTimeHelper
Johannes Döbler
duke at openjdk.org
Wed Aug 27 17:39:41 UTC 2025
On Sat, 23 Aug 2025 04:06:13 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.
Nice performance gains, but still this is quite a complex solution with a narrow use case (optimizes toString() of some java.time classes).
What about broadening the scope?
Situation:
You want to build a Latin1 string and number of added chars is more or less known (so we can optimize byte[] allocations)
Solution sketch: Introduce a new Latin1Builder
- owning a byte[] value and int count field
- allowing only latin1 chars to be added
Example:
public static void formatTo(Latin1Builder buf, LocalDate date) {
buf.ensureCapacity(10); // most common size
int year = date.getYear();
if (year < 0)
buf.append('-');
else if (year > 9999)
buf.append('+');
buf.appendInt(year);
buf.append('-');
buf.appendIntPair(buf, date.getMonthValue());
buf.append('-');
buf.appendIntPair(buf, date.getDayOfMonth());
}
-------------
PR Comment: https://git.openjdk.org/jdk/pull/26911#issuecomment-3229130870
More information about the security-dev
mailing list