Skip to content

Commit 38cef2a

Browse files
liachMandy Chung
authored and
Mandy Chung
committed
8309413: Improve the performance of MethodTypeDesc::descriptorString
8304932: MethodTypeDescImpl can be mutated by argument passed to MethodTypeDesc.of Reviewed-by: mchung
1 parent 7edd054 commit 38cef2a

File tree

4 files changed

+239
-85
lines changed

4 files changed

+239
-85
lines changed

src/java.base/share/classes/java/lang/constant/MethodTypeDesc.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ static MethodTypeDesc ofDescriptor(String descriptor) {
6464
* @since 21
6565
*/
6666
static MethodTypeDesc of(ClassDesc returnDesc) {
67-
return new MethodTypeDescImpl(returnDesc, ConstantUtils.EMPTY_CLASSDESC);
67+
return MethodTypeDescImpl.ofTrusted(returnDesc, ConstantUtils.EMPTY_CLASSDESC);
6868
}
6969

7070
/**
@@ -95,7 +95,7 @@ static MethodTypeDesc of(ClassDesc returnDesc, List<ClassDesc> paramDescs) {
9595
* {@link ClassDesc} for {@code void}
9696
*/
9797
static MethodTypeDesc of(ClassDesc returnDesc, ClassDesc... paramDescs) {
98-
return new MethodTypeDescImpl(returnDesc, paramDescs);
98+
return MethodTypeDescImpl.ofTrusted(returnDesc, paramDescs.clone());
9999
}
100100

101101
/**
@@ -195,13 +195,7 @@ static MethodTypeDesc of(ClassDesc returnDesc, ClassDesc... paramDescs) {
195195
* @return the method type descriptor string
196196
* @jvms 4.3.3 Method Descriptors
197197
*/
198-
default String descriptorString() {
199-
return String.format("(%s)%s",
200-
Stream.of(parameterArray())
201-
.map(ClassDesc::descriptorString)
202-
.collect(Collectors.joining()),
203-
returnType().descriptorString());
204-
}
198+
String descriptorString();
205199

206200
/**
207201
* Returns a human-readable descriptor for this method type, using the

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

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2023, 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
@@ -24,13 +24,16 @@
2424
*/
2525
package java.lang.constant;
2626

27+
import jdk.internal.vm.annotation.Stable;
28+
2729
import java.lang.invoke.MethodHandles;
2830
import java.lang.invoke.MethodType;
2931
import java.security.AccessController;
3032
import java.security.PrivilegedAction;
3133
import java.util.Arrays;
3234
import java.util.List;
3335
import java.util.Objects;
36+
import java.util.StringJoiner;
3437

3538
import static java.util.Objects.requireNonNull;
3639

@@ -41,22 +44,38 @@
4144
*/
4245
final class MethodTypeDescImpl implements MethodTypeDesc {
4346
private final ClassDesc returnType;
44-
private final ClassDesc[] argTypes;
47+
private final @Stable ClassDesc[] argTypes;
48+
private @Stable String cachedDescriptorString;
4549

4650
/**
4751
* Constructs a {@linkplain MethodTypeDesc} with the specified return type
48-
* and parameter types
52+
* and a trusted and already-validated parameter types array.
4953
*
5054
* @param returnType a {@link ClassDesc} describing the return type
51-
* @param argTypes {@link ClassDesc}s describing the parameter types
55+
* @param validatedArgTypes {@link ClassDesc}s describing the trusted and validated parameter types
5256
*/
53-
MethodTypeDescImpl(ClassDesc returnType, ClassDesc[] argTypes) {
57+
private MethodTypeDescImpl(ClassDesc returnType, ClassDesc[] validatedArgTypes) {
5458
this.returnType = requireNonNull(returnType);
55-
this.argTypes = requireNonNull(argTypes);
59+
this.argTypes = requireNonNull(validatedArgTypes);
60+
}
61+
62+
/**
63+
* Constructs a {@linkplain MethodTypeDesc} with the specified return type
64+
* and a trusted parameter types array, which will be validated.
65+
*
66+
* @param returnType a {@link ClassDesc} describing the return type
67+
* @param trustedArgTypes {@link ClassDesc}s describing the trusted parameter types
68+
*/
69+
static MethodTypeDescImpl ofTrusted(ClassDesc returnType, ClassDesc[] trustedArgTypes) {
70+
Objects.requireNonNull(returnType);
71+
if (trustedArgTypes.length == 0) // implicit null check
72+
return new MethodTypeDescImpl(returnType, ConstantUtils.EMPTY_CLASSDESC);
5673

57-
for (ClassDesc cr : argTypes)
58-
if (cr.isPrimitive() && cr.descriptorString().equals("V"))
74+
for (ClassDesc cd : trustedArgTypes)
75+
if (cd.isPrimitive() && cd.descriptorString().charAt(0) == 'V') // implicit null check
5976
throw new IllegalArgumentException("Void parameters not permitted");
77+
78+
return new MethodTypeDescImpl(returnType, trustedArgTypes);
6079
}
6180

6281
/**
@@ -70,9 +89,18 @@ final class MethodTypeDescImpl implements MethodTypeDesc {
7089
*/
7190
static MethodTypeDescImpl ofDescriptor(String descriptor) {
7291
requireNonNull(descriptor);
92+
7393
List<String> types = ConstantUtils.parseMethodDescriptor(descriptor);
74-
ClassDesc[] paramTypes = types.stream().skip(1).map(ClassDesc::ofDescriptor).toArray(ClassDesc[]::new);
75-
return new MethodTypeDescImpl(ClassDesc.ofDescriptor(types.get(0)), paramTypes);
94+
95+
int paramCount = types.size() - 1;
96+
var paramTypes = paramCount > 0 ? new ClassDesc[paramCount] : ConstantUtils.EMPTY_CLASSDESC;
97+
for (int i = 0; i < paramCount; i++) {
98+
paramTypes[i] = ClassDesc.ofDescriptor(types.get(i + 1));
99+
}
100+
101+
MethodTypeDescImpl result = ofTrusted(ClassDesc.ofDescriptor(types.getFirst()), paramTypes);
102+
result.cachedDescriptorString = descriptor;
103+
return result;
76104
}
77105

78106
@Override
@@ -102,14 +130,14 @@ public ClassDesc[] parameterArray() {
102130

103131
@Override
104132
public MethodTypeDesc changeReturnType(ClassDesc returnType) {
105-
return MethodTypeDesc.of(returnType, argTypes);
133+
return new MethodTypeDescImpl(returnType, argTypes);
106134
}
107135

108136
@Override
109137
public MethodTypeDesc changeParameterType(int index, ClassDesc paramType) {
110138
ClassDesc[] newArgs = argTypes.clone();
111139
newArgs[index] = paramType;
112-
return MethodTypeDesc.of(returnType, newArgs);
140+
return ofTrusted(returnType, newArgs);
113141
}
114142

115143
@Override
@@ -120,18 +148,33 @@ public MethodTypeDesc dropParameterTypes(int start, int end) {
120148
ClassDesc[] newArgs = new ClassDesc[argTypes.length - (end - start)];
121149
System.arraycopy(argTypes, 0, newArgs, 0, start);
122150
System.arraycopy(argTypes, end, newArgs, start, argTypes.length - end);
123-
return MethodTypeDesc.of(returnType, newArgs);
151+
return ofTrusted(returnType, newArgs);
124152
}
125153

126154
@Override
127155
public MethodTypeDesc insertParameterTypes(int pos, ClassDesc... paramTypes) {
128156
if (pos < 0 || pos > argTypes.length)
129157
throw new IndexOutOfBoundsException(pos);
158+
130159
ClassDesc[] newArgs = new ClassDesc[argTypes.length + paramTypes.length];
131160
System.arraycopy(argTypes, 0, newArgs, 0, pos);
132161
System.arraycopy(paramTypes, 0, newArgs, pos, paramTypes.length);
133162
System.arraycopy(argTypes, pos, newArgs, pos+paramTypes.length, argTypes.length - pos);
134-
return MethodTypeDesc.of(returnType, newArgs);
163+
164+
return ofTrusted(returnType, newArgs);
165+
}
166+
167+
@Override
168+
public String descriptorString() {
169+
var desc = this.cachedDescriptorString;
170+
if (desc != null)
171+
return desc;
172+
173+
var sj = new StringJoiner("", "(", ")" + returnType().descriptorString());
174+
for (int i = 0; i < parameterCount(); i++) {
175+
sj.add(parameterType(i).descriptorString());
176+
}
177+
return cachedDescriptorString = sj.toString();
135178
}
136179

137180
@Override

0 commit comments

Comments
 (0)