8325730: StringBuilder.toString allocation for the empty String

Reviewed-by: jlaskey, shade
This commit is contained in:
Claes Redestad 2024-02-20 20:28:55 +00:00
parent aa792eabab
commit d2590c69b4
4 changed files with 25 additions and 5 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1994, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1994, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -735,6 +735,9 @@ import jdk.internal.vm.annotation.IntrinsicCandidate;
@Override
@IntrinsicCandidate
public synchronized String toString() {
if (length() == 0) {
return "";
}
if (toStringCache == null) {
return toStringCache = new String(this, null);
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -471,8 +471,11 @@ public final class StringBuilder
@Override
@IntrinsicCandidate
public String toString() {
if (length() == 0) {
return "";
}
// Create a copy, don't share the array
return new String(this);
return new String(this, null);
}
/**