On Thu, 18 Jul 2024 11:32:47 GMT, Shaojin Wen <d...@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.

The current logic of adding a value and then substring(1) is not clear, and 
this approach is not elegant at all. I think the logic of changing to 
repeat('0') is concise and clear.

Using repeat implementation will reduce two object allocations, 
Integer.toString & subString(1), and the time consumption will be 1/3 of the 
original.

If JLA is not recommended, we can add a stringSize method in LocalTime, similar 
to DateTimeFormatterBuilder.

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

PR Comment: https://git.openjdk.org/jdk/pull/20232#issuecomment-2236698626

Reply via email to