Skip to content

Commit a078b5e

Browse files
committedApr 27, 2024
8331114: Further improve performance of MethodTypeDesc::descriptorString
Reviewed-by: mchung, liach
1 parent e3eb652 commit a078b5e

File tree

5 files changed

+34
-16
lines changed

5 files changed

+34
-16
lines changed
 

‎src/java.base/share/classes/java/lang/constant/ClassDesc.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ static ClassDesc of(String packageName, String className) {
160160
static ClassDesc ofDescriptor(String descriptor) {
161161
// implicit null-check
162162
return (descriptor.length() == 1)
163-
? Wrapper.forPrimitiveType(descriptor.charAt(0)).primitiveClassDescriptor()
163+
? Wrapper.forPrimitiveType(descriptor.charAt(0)).classDescriptor()
164164
// will throw IAE on descriptor.length == 0 or if array dimensions too long
165165
: new ReferenceClassDescImpl(descriptor);
166166
}

‎src/java.base/share/classes/java/lang/constant/ConstantUtils.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ static List<ClassDesc> parseMethodDescriptor(String descriptor) {
226226

227227
private static ClassDesc resolveClassDesc(String descriptor, int start, int len) {
228228
if (len == 1) {
229-
return Wrapper.forBasicType(descriptor.charAt(start)).primitiveClassDescriptor();
229+
return Wrapper.forPrimitiveType(descriptor.charAt(start)).classDescriptor();
230230
}
231231
return ClassDesc.ofDescriptor(descriptor.substring(start, start + len));
232232
}

‎src/java.base/share/classes/java/lang/constant/MethodTypeDescImpl.java

+10-5
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import java.util.Arrays;
3434
import java.util.List;
3535
import java.util.Objects;
36-
import java.util.StringJoiner;
3736

3837
import static java.util.Objects.requireNonNull;
3938

@@ -168,11 +167,17 @@ public String descriptorString() {
168167
if (desc != null)
169168
return desc;
170169

171-
var sj = new StringJoiner("", "(", ")" + returnType().descriptorString());
172-
for (int i = 0; i < parameterCount(); i++) {
173-
sj.add(parameterType(i).descriptorString());
170+
int len = 2 + returnType.descriptorString().length();
171+
for (ClassDesc argType : argTypes) {
172+
len += argType.descriptorString().length();
174173
}
175-
return cachedDescriptorString = sj.toString();
174+
StringBuilder sb = new StringBuilder(len).append('(');
175+
for (ClassDesc argType : argTypes) {
176+
sb.append(argType.descriptorString());
177+
}
178+
desc = sb.append(')').append(returnType.descriptorString()).toString();
179+
cachedDescriptorString = desc;
180+
return desc;
176181
}
177182

178183
@Override

‎src/java.base/share/classes/sun/invoke/util/Wrapper.java

+16-7
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import java.lang.constant.ConstantDescs;
3333

3434
public enum Wrapper {
35-
// wrapperType simple primitiveType simple char emptyArray format numericClass superClass
35+
// wrapperType simple primitiveType simple char emptyArray format numericClass superClass classDescriptor
3636
BOOLEAN( Boolean.class, "Boolean", boolean.class, "boolean", 'Z', new boolean[0], Format.unsigned( 1), 0, 0, ConstantDescs.CD_boolean),
3737
// These must be in the order defined for widening primitive conversions in JLS 5.1.2
3838
// Avoid boxing integral types here to defer initialization of internal caches
@@ -60,9 +60,18 @@ public enum Wrapper {
6060
private final int superClasses;
6161
private final String wrapperSimpleName;
6262
private final String primitiveSimpleName;
63-
private final ClassDesc primitiveTypeDesc;
64-
65-
private Wrapper(Class<?> wtype, String wtypeName, Class<?> ptype, String ptypeName, char tchar, Object emptyArray, int format, int numericClass, int superClasses, ClassDesc primitiveTypeDesc) {
63+
private final ClassDesc classDesc;
64+
65+
private Wrapper(Class<?> wtype,
66+
String wtypeName,
67+
Class<?> ptype,
68+
String ptypeName,
69+
char tchar,
70+
Object emptyArray,
71+
int format,
72+
int numericClass,
73+
int superClasses,
74+
ClassDesc classDesc) {
6675
this.wrapperType = wtype;
6776
this.primitiveType = ptype;
6877
this.basicTypeChar = tchar;
@@ -73,7 +82,7 @@ private Wrapper(Class<?> wtype, String wtypeName, Class<?> ptype, String ptypeNa
7382
this.superClasses = superClasses;
7483
this.wrapperSimpleName = wtypeName;
7584
this.primitiveSimpleName = ptypeName;
76-
this.primitiveTypeDesc = primitiveTypeDesc;
85+
this.classDesc = classDesc;
7786
}
7887

7988
/** For debugging, give the details of this wrapper. */
@@ -380,8 +389,8 @@ public static Wrapper forBasicType(Class<?> type) {
380389
}
381390
}
382391

383-
/** A nominal descriptor of the primitive type */
384-
public ClassDesc primitiveClassDescriptor() { return primitiveTypeDesc; }
392+
/** A nominal descriptor of the wrapped type */
393+
public ClassDesc classDescriptor() { return classDesc; }
385394

386395
/** What is the primitive type wrapped by this wrapper? */
387396
public Class<?> primitiveType() { return primitiveType; }

‎test/micro/org/openjdk/bench/java/lang/constant/MethodTypeDescFactories.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -54,11 +54,14 @@ public class MethodTypeDescFactories {
5454

5555
private static final ClassDesc DUMMY_CD = ClassDesc.of("Dummy_invalid");
5656

57+
/** Dots will be replaced by the descriptor of this benchmark class. */
5758
@Param({
5859
"(Ljava/lang/Object;Ljava/lang/String;)I",
5960
"()V",
6061
"([IJLjava/lang/String;Z)Ljava/util/List;",
61-
"()[Ljava/lang/String;"
62+
"()[Ljava/lang/String;",
63+
"(..IIJ)V",
64+
"(.....................)."
6265
})
6366
public String descString;
6467
public MethodTypeDesc desc;
@@ -68,6 +71,7 @@ public class MethodTypeDescFactories {
6871

6972
@Setup
7073
public void setup() {
74+
descString = descString.replaceAll("\\.", MethodTypeDescFactories.class.descriptorString());
7175
desc = MethodTypeDesc.ofDescriptor(descString);
7276
ret = desc.returnType();
7377
args = desc.parameterArray();

0 commit comments

Comments
 (0)
Please sign in to comment.