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) 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 class StringBuilders {
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 class StringBuilders {
}
}
StringBuilder sbEmpty;
String str;
String utf16Str;
CharSequence cs;
@ -398,6 +404,8 @@ public class StringBuilders {
}
private void generateData() {
sbEmpty = new StringBuilder(length);
char[] chars = "abcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
StringBuilder sb = new StringBuilder(length);