代码拉取完成,页面将自动刷新
同步操作将从 src-openEuler/openjdk-21 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
Subject: Backport JDK-8325730 StringBuilder.toString allocation for the empty String
---
.../share/classes/java/lang/StringBuffer.java | 5 ++++-
.../share/classes/java/lang/StringBuilder.java | 7 +++++--
.../org/openjdk/bench/java/lang/StringBuffers.java | 8 +++++++-
.../org/openjdk/bench/java/lang/StringBuilders.java | 10 +++++++++-
4 files changed, 25 insertions(+), 5 deletions(-)
diff --git a/src/java.base/share/classes/java/lang/StringBuffer.java b/src/java.base/share/classes/java/lang/StringBuffer.java
index d77462f0c..1aeceb959 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 @@ import jdk.internal.vm.annotation.IntrinsicCandidate;
@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 c42422e62..d7ff5ee35 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 final class StringBuilder
@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 b3d4d85c7..fea64c0d2 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 class StringBuffers {
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 40f41659e..29827b7f0 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 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);
--
2.33.0
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。