Skip to content

Commit

Permalink
Refactor: Use ResolvedJavaMethod to sort methods in ClassEntry
Browse files Browse the repository at this point in the history
This way we avoid the expensive calls to "toJavaName".

This patch reduces the time spend in debug info generation from ~12s to
5s.
  • Loading branch information
zakkak committed Aug 25, 2021
1 parent 328db41 commit 69adb17
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.List;
import java.util.Map;

import jdk.vm.ci.meta.ResolvedJavaMethod;
import org.graalvm.compiler.debug.DebugContext;

import com.oracle.objectfile.debuginfo.DebugInfoProvider.DebugFieldInfo;
Expand Down Expand Up @@ -272,7 +273,7 @@ private void processInterface(String interfaceName, DebugInfoBase debugInfoBase,
}

protected MethodEntry processMethod(DebugMethodInfo debugMethodInfo, DebugInfoBase debugInfoBase, DebugContext debugContext) {
String methodName = debugInfoBase.uniqueDebugString(debugMethodInfo.name());
String methodName = debugMethodInfo.name();
String resultTypeName = TypeEntry.canonicalize(debugMethodInfo.valueType());
int modifiers = debugMethodInfo.modifiers();
List<String> paramTypes = debugMethodInfo.paramTypes();
Expand Down Expand Up @@ -339,17 +340,15 @@ public ClassEntry getSuperClass() {

public MethodEntry ensureMethodEntryForDebugRangeInfo(DebugRangeInfo debugRangeInfo, DebugInfoBase debugInfoBase, DebugContext debugContext) {
assert listIsSorted(methods);
String methodName = debugInfoBase.uniqueDebugString(debugRangeInfo.name());
String paramSignature = debugRangeInfo.paramSignature();
String returnTypeName = debugRangeInfo.valueType();
ResolvedJavaMethod javaMethod = debugRangeInfo.getJavaMethod();
/* Since the methods list is sorted we perform a binary search */
int start = 0;
int end = methods.size() - 1;
assert end < (Integer.MAX_VALUE / 2);
while (start <= end) {
int middle = (start + end) / 2;
MethodEntry methodEntry = methods.get(middle);
int comparisonResult = methodEntry.compareTo(methodName, paramSignature, returnTypeName);
int comparisonResult = methodEntry.compareTo(javaMethod);
if (comparisonResult == 0) {
methodEntry.updateRangeInfo(debugInfoBase, debugRangeInfo);
if (methodEntry.fileEntry != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,17 @@
import com.oracle.objectfile.debuginfo.DebugInfoProvider.DebugCodeInfo;
import com.oracle.objectfile.debuginfo.DebugInfoProvider.DebugLineInfo;
import com.oracle.objectfile.debuginfo.DebugInfoProvider.DebugMethodInfo;

import java.util.Arrays;
import java.util.stream.Collectors;
import jdk.vm.ci.meta.ResolvedJavaMethod;

public class MethodEntry extends MemberEntry implements Comparable<MethodEntry> {
final TypeEntry[] paramTypes;
final String[] paramNames;
static final int DEOPT = 1 << 0;
static final int IN_RANGE = 1 << 1;
static final int INLINED = 1 << 2;
private final ResolvedJavaMethod javaMethod;
int flags;
final String symbolName;
private String signature;

public MethodEntry(DebugInfoBase debugInfoBase, DebugMethodInfo debugMethodInfo,
FileEntry fileEntry, String methodName, ClassEntry ownerType,
Expand All @@ -52,6 +50,7 @@ public MethodEntry(DebugInfoBase debugInfoBase, DebugMethodInfo debugMethodInfo,
this.paramTypes = paramTypes;
this.paramNames = paramNames;
this.symbolName = debugMethodInfo.symbolNameForMethod();
this.javaMethod = debugMethodInfo.getJavaMethod();
this.flags = 0;
if (debugMethodInfo.isDeoptTarget()) {
setIsDeopt();
Expand Down Expand Up @@ -161,36 +160,17 @@ public String getSymbolName() {
return symbolName;
}

private String getSignature() {
if (signature == null) {
signature = Arrays.stream(paramTypes).map(TypeEntry::getTypeName).collect(Collectors.joining(", "));
public int compareTo(ResolvedJavaMethod other) {
/* first try to sort methods by name, to have a nice sorting when printing types with ptype */
int comparisonResult = javaMethod.getName().compareTo(other.getName());
if (comparisonResult != 0) {
return comparisonResult;
}
return signature;
}

public int compareTo(String methodName, String paramSignature, String returnTypeName) {
int nameComparison = memberName.compareTo(methodName);
if (nameComparison != 0) {
return nameComparison;
}
int typeComparison = valueType.getTypeName().compareTo(returnTypeName);
if (typeComparison != 0) {
return typeComparison;
}
return getSignature().compareTo(paramSignature);
return this.javaMethod.hashCode() - other.hashCode();
}

@Override
public int compareTo(MethodEntry other) {
assert other != null;
int nameComparison = methodName().compareTo(other.methodName());
if (nameComparison != 0) {
return nameComparison;
}
int typeComparison = valueType.getTypeName().compareTo(other.valueType.getTypeName());
if (typeComparison != 0) {
return typeComparison;
}
return getSignature().compareTo(other.getSignature());
return compareTo(other.javaMethod);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.function.Consumer;
import java.util.stream.Stream;

import jdk.vm.ci.meta.ResolvedJavaMethod;
import jdk.vm.ci.meta.ResolvedJavaType;
import org.graalvm.compiler.debug.DebugContext;

Expand Down Expand Up @@ -201,9 +202,9 @@ interface DebugFieldInfo extends DebugMemberInfo {

interface DebugMethodInfo extends DebugMemberInfo {
/**
* @return a string identifying the method parameters.
* @return the JavaMethod of the method associated with this DebugMethodInfo.
*/
String paramSignature();
ResolvedJavaMethod getJavaMethod();

/**
* @return an array of Strings identifying the method parameters.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -651,8 +651,8 @@ public String valueType() {
}

@Override
public String paramSignature() {
return hostedMethod.format("%P");
public ResolvedJavaMethod getJavaMethod() {
return hostedMethod;
}

@Override
Expand Down Expand Up @@ -877,8 +877,8 @@ public String symbolNameForMethod() {
}

@Override
public String paramSignature() {
return hostedMethod.format("%P");
public ResolvedJavaMethod getJavaMethod() {
return hostedMethod;
}

@Override
Expand Down Expand Up @@ -1101,8 +1101,8 @@ public String valueType() {
}

@Override
public String paramSignature() {
return method.format("%P");
public ResolvedJavaMethod getJavaMethod() {
return method;
}

@Override
Expand Down

0 comments on commit 69adb17

Please sign in to comment.