Skip to content

Commit

Permalink
8331114: Further improve performance of MethodTypeDesc::descriptorString
Browse files Browse the repository at this point in the history
Reviewed-by: mchung, liach
  • Loading branch information
cl4es committed Apr 27, 2024
1 parent e3eb652 commit a078b5e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ static ClassDesc of(String packageName, String className) {
static ClassDesc ofDescriptor(String descriptor) {
// implicit null-check
return (descriptor.length() == 1)
? Wrapper.forPrimitiveType(descriptor.charAt(0)).primitiveClassDescriptor()
? Wrapper.forPrimitiveType(descriptor.charAt(0)).classDescriptor()
// will throw IAE on descriptor.length == 0 or if array dimensions too long
: new ReferenceClassDescImpl(descriptor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ static List<ClassDesc> parseMethodDescriptor(String descriptor) {

private static ClassDesc resolveClassDesc(String descriptor, int start, int len) {
if (len == 1) {
return Wrapper.forBasicType(descriptor.charAt(start)).primitiveClassDescriptor();
return Wrapper.forPrimitiveType(descriptor.charAt(start)).classDescriptor();
}
return ClassDesc.ofDescriptor(descriptor.substring(start, start + len));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.StringJoiner;

import static java.util.Objects.requireNonNull;

Expand Down Expand Up @@ -168,11 +167,17 @@ public String descriptorString() {
if (desc != null)
return desc;

var sj = new StringJoiner("", "(", ")" + returnType().descriptorString());
for (int i = 0; i < parameterCount(); i++) {
sj.add(parameterType(i).descriptorString());
int len = 2 + returnType.descriptorString().length();
for (ClassDesc argType : argTypes) {
len += argType.descriptorString().length();
}
return cachedDescriptorString = sj.toString();
StringBuilder sb = new StringBuilder(len).append('(');
for (ClassDesc argType : argTypes) {
sb.append(argType.descriptorString());
}
desc = sb.append(')').append(returnType.descriptorString()).toString();
cachedDescriptorString = desc;
return desc;
}

@Override
Expand Down
23 changes: 16 additions & 7 deletions src/java.base/share/classes/sun/invoke/util/Wrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import java.lang.constant.ConstantDescs;

public enum Wrapper {
// wrapperType simple primitiveType simple char emptyArray format numericClass superClass
// wrapperType simple primitiveType simple char emptyArray format numericClass superClass classDescriptor
BOOLEAN( Boolean.class, "Boolean", boolean.class, "boolean", 'Z', new boolean[0], Format.unsigned( 1), 0, 0, ConstantDescs.CD_boolean),
// These must be in the order defined for widening primitive conversions in JLS 5.1.2
// Avoid boxing integral types here to defer initialization of internal caches
Expand Down Expand Up @@ -60,9 +60,18 @@ public enum Wrapper {
private final int superClasses;
private final String wrapperSimpleName;
private final String primitiveSimpleName;
private final ClassDesc primitiveTypeDesc;

private Wrapper(Class<?> wtype, String wtypeName, Class<?> ptype, String ptypeName, char tchar, Object emptyArray, int format, int numericClass, int superClasses, ClassDesc primitiveTypeDesc) {
private final ClassDesc classDesc;

private Wrapper(Class<?> wtype,
String wtypeName,
Class<?> ptype,
String ptypeName,
char tchar,
Object emptyArray,
int format,
int numericClass,
int superClasses,
ClassDesc classDesc) {
this.wrapperType = wtype;
this.primitiveType = ptype;
this.basicTypeChar = tchar;
Expand All @@ -73,7 +82,7 @@ private Wrapper(Class<?> wtype, String wtypeName, Class<?> ptype, String ptypeNa
this.superClasses = superClasses;
this.wrapperSimpleName = wtypeName;
this.primitiveSimpleName = ptypeName;
this.primitiveTypeDesc = primitiveTypeDesc;
this.classDesc = classDesc;
}

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

/** A nominal descriptor of the primitive type */
public ClassDesc primitiveClassDescriptor() { return primitiveTypeDesc; }
/** A nominal descriptor of the wrapped type */
public ClassDesc classDescriptor() { return classDesc; }

/** What is the primitive type wrapped by this wrapper? */
public Class<?> primitiveType() { return primitiveType; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023, 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
Expand Down Expand Up @@ -54,11 +54,14 @@ public class MethodTypeDescFactories {

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

/** Dots will be replaced by the descriptor of this benchmark class. */
@Param({
"(Ljava/lang/Object;Ljava/lang/String;)I",
"()V",
"([IJLjava/lang/String;Z)Ljava/util/List;",
"()[Ljava/lang/String;"
"()[Ljava/lang/String;",
"(..IIJ)V",
"(.....................)."
})
public String descString;
public MethodTypeDesc desc;
Expand All @@ -68,6 +71,7 @@ public class MethodTypeDescFactories {

@Setup
public void setup() {
descString = descString.replaceAll("\\.", MethodTypeDescFactories.class.descriptorString());
desc = MethodTypeDesc.ofDescriptor(descString);
ret = desc.returnType();
args = desc.parameterArray();
Expand Down

1 comment on commit a078b5e

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.