Skip to content
This repository has been archived by the owner on Jul 17, 2024. It is now read-only.
/ jdk22u Public archive

Commit

Permalink
8325730: StringBuilder.toString allocation for the empty String
Browse files Browse the repository at this point in the history
Backport-of: d2590c69b4efe5aa2b48b08070e0dbafb04ef202
  • Loading branch information
shipilev committed Mar 11, 2024
1 parent fd11382 commit b2275aa
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/java.base/share/classes/java/lang/StringBuffer.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
}
Expand Down
7 changes: 5 additions & 2 deletions src/java.base/share/classes/java/lang/StringBuilder.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
}

/**
Expand Down
8 changes: 7 additions & 1 deletion test/micro/org/openjdk/bench/java/lang/StringBuffers.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -80,4 +80,10 @@ public String substring() {
return blaha.substring(30, 35);
}

StringBuffer sb = new StringBuffer();

@Benchmark
public String emptyToString() {
return sb.toString();
}
}
10 changes: 9 additions & 1 deletion test/micro/org/openjdk/bench/java/lang/StringBuilders.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -380,6 +385,7 @@ public CharSequence next() {
}
}

StringBuilder sbEmpty;
String str;
String utf16Str;
CharSequence cs;
Expand All @@ -398,6 +404,8 @@ public void setup() {
}

private void generateData() {
sbEmpty = new StringBuilder(length);

char[] chars = "abcdefghijklmnopqrstuvwxyz0123456789".toCharArray();

StringBuilder sb = new StringBuilder(length);
Expand Down

1 comment on commit b2275aa

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.