diff --git a/src/java.base/share/classes/java/lang/StringBuffer.java b/src/java.base/share/classes/java/lang/StringBuffer.java index d77462f0c70..1aeceb95985 100644 --- a/src/java.base/share/classes/java/lang/StringBuffer.java +++ b/src/java.base/share/classes/java/lang/StringBuffer.java @@ -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 @@ public synchronized StringBuffer repeat(CharSequence cs, int count) { @Override @IntrinsicCandidate public synchronized String toString() { + if (length() == 0) { + return ""; + } if (toStringCache == null) { return toStringCache = new String(this, null); } diff --git a/src/java.base/share/classes/java/lang/StringBuilder.java b/src/java.base/share/classes/java/lang/StringBuilder.java index c42422e6277..d7ff5ee35d7 100644 --- a/src/java.base/share/classes/java/lang/StringBuilder.java +++ b/src/java.base/share/classes/java/lang/StringBuilder.java @@ -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 StringBuilder repeat(CharSequence cs, int count) { @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); } /** diff --git a/test/micro/org/openjdk/bench/java/lang/StringBuffers.java b/test/micro/org/openjdk/bench/java/lang/StringBuffers.java index b3d4d85c75c..fea64c0d2e0 100644 --- a/test/micro/org/openjdk/bench/java/lang/StringBuffers.java +++ b/test/micro/org/openjdk/bench/java/lang/StringBuffers.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2014, 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 @@ -80,4 +80,10 @@ public String substring() { return blaha.substring(30, 35); } + StringBuffer sb = new StringBuffer(); + + @Benchmark + public String emptyToString() { + return sb.toString(); + } } diff --git a/test/micro/org/openjdk/bench/java/lang/StringBuilders.java b/test/micro/org/openjdk/bench/java/lang/StringBuilders.java index 40f41659e7c..29827b7f03a 100644 --- a/test/micro/org/openjdk/bench/java/lang/StringBuilders.java +++ b/test/micro/org/openjdk/bench/java/lang/StringBuilders.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2014, 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 @@ -364,6 +364,11 @@ public char charAtUtf16() { return sbUtf16.charAt(charAt_index); } + @Benchmark + public String emptyToString(Data data) { + return data.sbEmpty.toString(); + } + @State(Scope.Thread) public static class Data { int i = 0; @@ -380,6 +385,7 @@ public CharSequence next() { } } + StringBuilder sbEmpty; String str; String utf16Str; CharSequence cs; @@ -398,6 +404,8 @@ public void setup() { } private void generateData() { + sbEmpty = new StringBuilder(length); + char[] chars = "abcdefghijklmnopqrstuvwxyz0123456789".toCharArray(); StringBuilder sb = new StringBuilder(length);