RFR: 8336741: Optimize LocalTime.toString with StringBuilder.repeat
Shaojin Wen
duke at openjdk.org
Thu Jul 18 14:16:34 UTC 2024
On Thu, 18 Jul 2024 11:32:47 GMT, Shaojin Wen <duke at openjdk.org> wrote:
> class LocalTime {
> public String toString() {
> // ...
> if (nanoValue % 1000_000 == 0) {
> buf.append(Integer.toString((nanoValue / 1000_000) + 1000).substring(1));
> } else if (nanoValue % 1000 == 0) {
> buf.append(Integer.toString((nanoValue / 1000) + 1000_000).substring(1));
> } else {
> buf.append(Integer.toString((nanoValue) + 1000_000_000).substring(1));
> }
> // ...
> }
> }
>
> Currently, LocalTime.toString handles nanos by adding a value and then subString(1) to fill it with zeros. Using StringBuilder.repeat is more concise and has better performance.
DateTimeFormatterBuilder uses DecimalStyle.getZeroDigit(), and printFixedWidth does not meet the requirements.
The scenario where LocalDate.toString handles Year is very simple, and repeat can be used directly.
printFixedWidth requires three different calls in LocalTime.toString.
Is there a good reason to extract it into a public method? I do not reject this approach, but I do not fully support it.
-------------
PR Comment: https://git.openjdk.org/jdk/pull/20232#issuecomment-2236644193
More information about the core-libs-dev
mailing list