Skip to content

Commit d2590c6

Browse files
committedFeb 20, 2024
8325730: StringBuilder.toString allocation for the empty String
Reviewed-by: jlaskey, shade
1 parent aa792ea commit d2590c6

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed
 

‎src/java.base/share/classes/java/lang/StringBuffer.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1994, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1994, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* 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) {
735735
@Override
736736
@IntrinsicCandidate
737737
public synchronized String toString() {
738+
if (length() == 0) {
739+
return "";
740+
}
738741
if (toStringCache == null) {
739742
return toStringCache = new String(this, null);
740743
}

‎src/java.base/share/classes/java/lang/StringBuilder.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -471,8 +471,11 @@ public StringBuilder repeat(CharSequence cs, int count) {
471471
@Override
472472
@IntrinsicCandidate
473473
public String toString() {
474+
if (length() == 0) {
475+
return "";
476+
}
474477
// Create a copy, don't share the array
475-
return new String(this);
478+
return new String(this, null);
476479
}
477480

478481
/**

‎test/micro/org/openjdk/bench/java/lang/StringBuffers.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -80,4 +80,10 @@ public String substring() {
8080
return blaha.substring(30, 35);
8181
}
8282

83+
StringBuffer sb = new StringBuffer();
84+
85+
@Benchmark
86+
public String emptyToString() {
87+
return sb.toString();
88+
}
8389
}

‎test/micro/org/openjdk/bench/java/lang/StringBuilders.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -364,6 +364,11 @@ public char charAtUtf16() {
364364
return sbUtf16.charAt(charAt_index);
365365
}
366366

367+
@Benchmark
368+
public String emptyToString(Data data) {
369+
return data.sbEmpty.toString();
370+
}
371+
367372
@State(Scope.Thread)
368373
public static class Data {
369374
int i = 0;
@@ -380,6 +385,7 @@ public CharSequence next() {
380385
}
381386
}
382387

388+
StringBuilder sbEmpty;
383389
String str;
384390
String utf16Str;
385391
CharSequence cs;
@@ -398,6 +404,8 @@ public void setup() {
398404
}
399405

400406
private void generateData() {
407+
sbEmpty = new StringBuilder(length);
408+
401409
char[] chars = "abcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
402410

403411
StringBuilder sb = new StringBuilder(length);

0 commit comments

Comments
 (0)
Please sign in to comment.