8337168: Optimize LocalDateTime.toString

Reviewed-by: liach, naoto
This commit is contained in:
Shaojin Wen 2024-07-26 12:10:21 +00:00
parent 374fca0fcb
commit 5ff7c57f9f
3 changed files with 30 additions and 10 deletions

View file

@ -2147,11 +2147,20 @@ public final class LocalDate
*/ */
@Override @Override
public String toString() { public String toString() {
var buf = new StringBuilder(10);
formatTo(buf);
return buf.toString();
}
/**
* Prints the toString result to the given buf, avoiding extra string allocations.
* Requires extra capacity of 10 to avoid StringBuilder reallocation.
*/
void formatTo(StringBuilder buf) {
int yearValue = year; int yearValue = year;
int monthValue = month; int monthValue = month;
int dayValue = day; int dayValue = day;
int absYear = Math.abs(yearValue); int absYear = Math.abs(yearValue);
StringBuilder buf = new StringBuilder(10);
if (absYear < 1000) { if (absYear < 1000) {
if (yearValue < 0) { if (yearValue < 0) {
buf.append('-'); buf.append('-');
@ -2164,11 +2173,10 @@ public final class LocalDate
} }
buf.append(yearValue); buf.append(yearValue);
} }
return buf.append(monthValue < 10 ? "-0" : "-") buf.append(monthValue < 10 ? "-0" : "-")
.append(monthValue) .append(monthValue)
.append(dayValue < 10 ? "-0" : "-") .append(dayValue < 10 ? "-0" : "-")
.append(dayValue) .append(dayValue);
.toString();
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2012, 2023, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -1965,7 +1965,11 @@ public final class LocalDateTime
*/ */
@Override @Override
public String toString() { public String toString() {
return date.toString() + 'T' + time.toString(); var buf = new StringBuilder(29);
date.formatTo(buf);
buf.append('T');
time.formatTo(buf);
return buf.toString();
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------

View file

@ -1631,7 +1631,16 @@ public final class LocalTime
*/ */
@Override @Override
public String toString() { public String toString() {
StringBuilder buf = new StringBuilder(18); var buf = new StringBuilder(18);
formatTo(buf);
return buf.toString();
}
/**
* Prints the toString result to the given buf, avoiding extra string allocations.
* Requires extra capacity of 18 to avoid StringBuilder reallocation.
*/
void formatTo(StringBuilder buf) {
int hourValue = hour; int hourValue = hour;
int minuteValue = minute; int minuteValue = minute;
int secondValue = second; int secondValue = second;
@ -1657,7 +1666,6 @@ public final class LocalTime
buf.append(digits); buf.append(digits);
} }
} }
return buf.toString();
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------