RFR: 8315999: Improve Date toString performance

Claes Redestad redestad at openjdk.org
Mon Sep 11 13:58:41 UTC 2023


On Mon, 11 Sep 2023 12:12:17 GMT, 温绍锦 <duke at openjdk.org> wrote:

> improve date toString performance, includes:
> 
> java.util.Date.toString
> java.util.Date.toGMTString
> java.time.Instant.toString
> java.time.LocalDate.toString
> java.time.LocalDateTime.toString
> java.time.LocalTime.toString

As @liach says this should be done on top of #15651. As it stands now a thorough reviews seems a bit premature.

Correct me if I'm wrong but the gains come from inlining code into the various `toString` methods to reduce need to go through `DateTimeFormatter`s - which allocate `StringBuilder` and might go through chains of `CompositePrinterParser` and such.. It seems that before inlining and duplicating logic - making the code overall more fragile and harder to maintain - that we ought to see if we can get close enough by optimizing `DateTimeFormatter` and the corresponding builders to produce types that may optimize better.

src/java.base/share/classes/java/time/Instant.java line 1355:

> 1353:     @Override
> 1354:     public String toString() {
> 1355:         return DateTimeFormatter.ISO_INSTANT.format(this);

Have you considered potentially more generalizable optimizations to `DateTimeFormatter.ISO_INSTANT.format(this)` here? 

Hand-rolling a fixed-length buffer, skipping the `StringBuilder` .. understandably this can have a performance edge, but perhaps a `DateTimeFormatter` like `ISO_INSTANT` can be optimized to get closer to whatever speed-up this gets you - with broader implications.

src/java.base/share/classes/java/time/LocalDate.java line 2181:

> 2179:         if (yearAbs < 1000) {
> 2180:             if (year < 0) {
> 2181:                 buf[off] = '-';

`buf[off++] = '-';`

src/java.base/share/classes/java/time/LocalDate.java line 2188:

> 2186:             ByteArrayLittleEndian.setInt(
> 2187:                     buf,
> 2188:                     year < 0 ? 1 : 0,

`off,`

src/java.base/share/classes/java/time/LocalDate.java line 2192:

> 2190:         } else {
> 2191:             if (year > 9999) {
> 2192:                 buf[off] = '+';

`buf[off++] = '+';`?

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

PR Review: https://git.openjdk.org/jdk/pull/15658#pullrequestreview-1620003157
PR Review Comment: https://git.openjdk.org/jdk/pull/15658#discussion_r1321554515
PR Review Comment: https://git.openjdk.org/jdk/pull/15658#discussion_r1321590202
PR Review Comment: https://git.openjdk.org/jdk/pull/15658#discussion_r1321591620
PR Review Comment: https://git.openjdk.org/jdk/pull/15658#discussion_r1321588790



More information about the security-dev mailing list