Skip to content

Commit 0898ab7

Browse files
wenshaoliach
authored andcommitted
8336741: Optimize LocalTime.toString with StringBuilder.repeat
Reviewed-by: liach, rriggs, naoto
1 parent 24f67d0 commit 0898ab7

File tree

3 files changed

+164
-6
lines changed

3 files changed

+164
-6
lines changed

src/java.base/share/classes/java/time/LocalTime.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -92,6 +92,8 @@
9292
import java.time.temporal.ValueRange;
9393
import java.util.Objects;
9494

95+
import jdk.internal.util.DecimalDigits;
96+
9597
/**
9698
* A time without a time-zone in the ISO-8601 calendar system,
9799
* such as {@code 10:15:30}.
@@ -1640,13 +1642,19 @@ public String toString() {
16401642
buf.append(secondValue < 10 ? ":0" : ":").append(secondValue);
16411643
if (nanoValue > 0) {
16421644
buf.append('.');
1643-
if (nanoValue % 1000_000 == 0) {
1644-
buf.append(Integer.toString((nanoValue / 1000_000) + 1000).substring(1));
1645+
int zeros = 9 - DecimalDigits.stringSize(nanoValue);
1646+
if (zeros > 0) {
1647+
buf.repeat('0', zeros);
1648+
}
1649+
int digits;
1650+
if (nanoValue % 1_000_000 == 0) {
1651+
digits = nanoValue / 1_000_000;
16451652
} else if (nanoValue % 1000 == 0) {
1646-
buf.append(Integer.toString((nanoValue / 1000) + 1000_000).substring(1));
1653+
digits = nanoValue / 1000;
16471654
} else {
1648-
buf.append(Integer.toString((nanoValue) + 1000_000_000).substring(1));
1655+
digits = nanoValue;
16491656
}
1657+
buf.append(digits);
16501658
}
16511659
}
16521660
return buf.toString();

src/java.base/share/classes/jdk/internal/util/DecimalDigits.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -84,4 +84,30 @@ private DecimalDigits() {
8484
public static short digitPair(int i) {
8585
return DIGITS[i];
8686
}
87+
88+
/**
89+
* Returns the string representation size for a given int value.
90+
*
91+
* @param x int value
92+
* @return string size
93+
*
94+
* @implNote There are other ways to compute this: e.g. binary search,
95+
* but values are biased heavily towards zero, and therefore linear search
96+
* wins. The iteration results are also routinely inlined in the generated
97+
* code after loop unrolling.
98+
*/
99+
public static int stringSize(int x) {
100+
int d = 1;
101+
if (x >= 0) {
102+
d = 0;
103+
x = -x;
104+
}
105+
int p = -10;
106+
for (int i = 1; i < 10; i++) {
107+
if (x > p)
108+
return i + d;
109+
p = 10 * p;
110+
}
111+
return 10 + d;
112+
}
87113
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright (c) 2024, Alibaba Group Holding Limited. All Rights Reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
package org.openjdk.bench.java.time;
24+
25+
import java.time.Duration;
26+
import java.time.Instant;
27+
import java.time.LocalDate;
28+
import java.time.LocalDateTime;
29+
import java.time.LocalTime;
30+
import java.time.ZonedDateTime;
31+
import java.time.ZoneOffset;
32+
import java.time.format.DateTimeFormatter;
33+
import java.time.temporal.ChronoUnit;
34+
35+
import java.util.Locale;
36+
import java.util.Random;
37+
import java.util.concurrent.TimeUnit;
38+
import java.util.stream.IntStream;
39+
import java.util.stream.Stream;
40+
41+
import org.openjdk.jmh.annotations.Benchmark;
42+
import org.openjdk.jmh.annotations.BenchmarkMode;
43+
import org.openjdk.jmh.annotations.Fork;
44+
import org.openjdk.jmh.annotations.Measurement;
45+
import org.openjdk.jmh.annotations.Mode;
46+
import org.openjdk.jmh.annotations.OutputTimeUnit;
47+
import org.openjdk.jmh.annotations.Param;
48+
import org.openjdk.jmh.annotations.Scope;
49+
import org.openjdk.jmh.annotations.Setup;
50+
import org.openjdk.jmh.annotations.State;
51+
import org.openjdk.jmh.annotations.Warmup;
52+
import org.openjdk.jmh.infra.Blackhole;
53+
54+
@BenchmarkMode(Mode.Throughput)
55+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
56+
@Warmup(iterations = 5, time = 1)
57+
@Measurement(iterations = 5, time = 1)
58+
@Fork(3)
59+
@State(Scope.Thread)
60+
public class ToStringBench {
61+
private static final Instant[] INSTANTS;
62+
private static final ZonedDateTime[] ZONED_DATE_TIMES;
63+
private static final LocalDateTime[] LOCAL_DATE_TIMES;
64+
private static final LocalDate[] LOCAL_DATES;
65+
private static final LocalTime[] LOCAL_TIMES;
66+
67+
static {
68+
Instant loInstant = Instant.EPOCH.plus(Duration.ofDays(365*50)); // 2020-01-01
69+
Instant hiInstant = loInstant.plus(Duration.ofDays(1));
70+
long maxOffsetNanos = Duration.between(loInstant, hiInstant).toNanos();
71+
Random random = new Random(0);
72+
INSTANTS = IntStream
73+
.range(0, 1_000)
74+
.mapToObj(ignored -> {
75+
final long offsetNanos = (long) Math.floor(random.nextDouble() * maxOffsetNanos);
76+
return loInstant.plus(offsetNanos, ChronoUnit.NANOS);
77+
})
78+
.toArray(Instant[]::new);
79+
80+
ZONED_DATE_TIMES = Stream.of(INSTANTS)
81+
.map(instant -> ZonedDateTime.ofInstant(instant, ZoneOffset.UTC))
82+
.toArray(ZonedDateTime[]::new);
83+
84+
LOCAL_DATE_TIMES = Stream.of(ZONED_DATE_TIMES)
85+
.map(zdt -> zdt.toLocalDateTime())
86+
.toArray(LocalDateTime[]::new);
87+
88+
LOCAL_DATES = Stream.of(LOCAL_DATE_TIMES)
89+
.map(ldt -> ldt.toLocalDate())
90+
.toArray(LocalDate[]::new);
91+
92+
LOCAL_TIMES = Stream.of(LOCAL_DATE_TIMES)
93+
.map(ldt -> ldt.toLocalTime())
94+
.toArray(LocalTime[]::new);
95+
}
96+
97+
@Benchmark
98+
public void zonedDateTimeToString(Blackhole bh) {
99+
for (final ZonedDateTime zonedDateTime : ZONED_DATE_TIMES) {
100+
bh.consume(zonedDateTime.toString());
101+
}
102+
}
103+
104+
@Benchmark
105+
public void localDateTimeToString(Blackhole bh) {
106+
for (LocalDateTime localDateTime : LOCAL_DATE_TIMES) {
107+
bh.consume(localDateTime.toString());
108+
}
109+
}
110+
111+
@Benchmark
112+
public void localDateToString(Blackhole bh) {
113+
for (LocalDate localDate : LOCAL_DATES) {
114+
bh.consume(localDate.toString());
115+
}
116+
}
117+
118+
@Benchmark
119+
public void localTimeToString(Blackhole bh) {
120+
for (LocalTime localTime : LOCAL_TIMES) {
121+
bh.consume(localTime.toString());
122+
}
123+
}
124+
}

0 commit comments

Comments
 (0)