RFR: 8336741: Optimize LocalTime.toString with StringBuilder.repeat
Shaojin Wen
duke at openjdk.org
Thu Jul 18 11:41:41 UTC 2024
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.
-------------
Commit messages:
- use StringBuilder#repeat append zeros
Changes: https://git.openjdk.org/jdk/pull/20232/files
Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=20232&range=00
Issue: https://bugs.openjdk.org/browse/JDK-8336741
Stats: 14 lines in 1 file changed: 10 ins; 0 del; 4 mod
Patch: https://git.openjdk.org/jdk/pull/20232.diff
Fetch: git fetch https://git.openjdk.org/jdk.git pull/20232/head:pull/20232
PR: https://git.openjdk.org/jdk/pull/20232
More information about the core-libs-dev
mailing list