1 Star 0 Fork 13

yangzhenyu/openjdk-21

forked from src-openEuler/openjdk-21 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Backport-JDK-8325730-StringBuilder.toString-allocati.patch 4.46 KB
一键复制 编辑 原始数据 按行查看 历史
wuyafang 提交于 2024-10-14 11:38 . sync bishengjdk21 patches
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
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/dev11101/openjdk-21.git
[email protected]:dev11101/openjdk-21.git
dev11101
openjdk-21
openjdk-21
master

搜索帮助