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);
}