mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 14:54:52 +02:00
8325730: StringBuilder.toString allocation for the empty String
Reviewed-by: jlaskey, shade
This commit is contained in:
parent
aa792eabab
commit
d2590c69b4
4 changed files with 25 additions and 5 deletions
|
@ -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.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
@ -735,6 +735,9 @@ import jdk.internal.vm.annotation.IntrinsicCandidate;
|
||||||
@Override
|
@Override
|
||||||
@IntrinsicCandidate
|
@IntrinsicCandidate
|
||||||
public synchronized String toString() {
|
public synchronized String toString() {
|
||||||
|
if (length() == 0) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
if (toStringCache == null) {
|
if (toStringCache == null) {
|
||||||
return toStringCache = new String(this, null);
|
return toStringCache = new String(this, null);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
@ -471,8 +471,11 @@ public final class StringBuilder
|
||||||
@Override
|
@Override
|
||||||
@IntrinsicCandidate
|
@IntrinsicCandidate
|
||||||
public String toString() {
|
public String toString() {
|
||||||
|
if (length() == 0) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
// Create a copy, don't share the array
|
// Create a copy, don't share the array
|
||||||
return new String(this);
|
return new String(this, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -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.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
@ -80,4 +80,10 @@ public class StringBuffers {
|
||||||
return blaha.substring(30, 35);
|
return blaha.substring(30, 35);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
StringBuffer sb = new StringBuffer();
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public String emptyToString() {
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* 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);
|
return sbUtf16.charAt(charAt_index);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public String emptyToString(Data data) {
|
||||||
|
return data.sbEmpty.toString();
|
||||||
|
}
|
||||||
|
|
||||||
@State(Scope.Thread)
|
@State(Scope.Thread)
|
||||||
public static class Data {
|
public static class Data {
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
@ -380,6 +385,7 @@ public class StringBuilders {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
StringBuilder sbEmpty;
|
||||||
String str;
|
String str;
|
||||||
String utf16Str;
|
String utf16Str;
|
||||||
CharSequence cs;
|
CharSequence cs;
|
||||||
|
@ -398,6 +404,8 @@ public class StringBuilders {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void generateData() {
|
private void generateData() {
|
||||||
|
sbEmpty = new StringBuilder(length);
|
||||||
|
|
||||||
char[] chars = "abcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
|
char[] chars = "abcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
|
||||||
|
|
||||||
StringBuilder sb = new StringBuilder(length);
|
StringBuilder sb = new StringBuilder(length);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue