Skip to content

Commit

Permalink
* Fix Generator performance issue on classes with a lot of methods …
Browse files Browse the repository at this point in the history
  • Loading branch information
saudet committed Jun 20, 2015
1 parent bc4f009 commit 9922f74
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

* Fix `Generator` performance issue on classes with a lot of methods ([issue bytedeco/javacpp-presets#36](https://github.com/bytedeco/javacpp-presets/issues/36))
* Offer the Apache License, Version 2.0, as a new choice of license, in addition to the GPLv2 with Classpath exception
* Fix `NullPointerException` when trying to process an `interface` class
* Fix `Parser` errors on unnamed `namespace` blocks, preprocessor directives with comments, and empty macros
Expand Down
50 changes: 28 additions & 22 deletions src/main/java/org/bytedeco/javacpp/tools/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ public Generator(Logger logger, ClassProperties properties) {
IndexedSet<String> callbacks;
IndexedSet<Class> functions, deallocators, arrayDeallocators, jclasses, jclassesInit;
HashMap<Class,LinkedList<String>> members, virtualFunctions, virtualMembers;
HashMap<Method,MethodInformation> annotationCache;
boolean mayThrowExceptions, usesAdapters;

public boolean generate(String sourceFilename, String headerFilename,
Expand All @@ -175,6 +176,7 @@ public boolean generate(String sourceFilename, String headerFilename,
members = new HashMap<Class,LinkedList<String>>();
virtualFunctions = new HashMap<Class,LinkedList<String>>();
virtualMembers = new HashMap<Class,LinkedList<String>>();
annotationCache = new HashMap<Method,MethodInformation>();
mayThrowExceptions = false;
usesAdapters = false;
if (classes(true, true, classPath, classes)) {
Expand Down Expand Up @@ -2243,28 +2245,32 @@ MethodInformation methodInformation(Method method) {
boolean noReturnGetter = false;
Method pairedMethod = null;
for (Method method2 : info.cls.getDeclaredMethods()) {
int modifiers2 = method2.getModifiers();
Class returnType2 = method2.getReturnType();
String methodName2 = method2.getName();
Class[] parameterTypes2 = method2.getParameterTypes();
Annotation[] annotations2 = method2.getAnnotations();
Annotation[][] parameterAnnotations2 = method2.getParameterAnnotations();
MethodInformation info2 = annotationCache.get(method2);
if (info2 == null) {
annotationCache.put(method2, info2 = new MethodInformation());
info2.modifiers = method2.getModifiers();
info2.returnType = method2.getReturnType();
info2.name = method2.getName();
info2.parameterTypes = method2.getParameterTypes();
info2.annotations = method2.getAnnotations();
info2.parameterAnnotations = method2.getParameterAnnotations();
}
int skipParameters = info.parameterTypes.length > 0 && info.parameterTypes[0] == Class.class ? 1 : 0;
int skipParameters2 = parameterTypes2.length > 0 && parameterTypes2[0] == Class.class ? 1 : 0;
int skipParameters2 = info2.parameterTypes.length > 0 && info2.parameterTypes[0] == Class.class ? 1 : 0;

if (method.equals(method2) || !Modifier.isNative(modifiers2)) {
if (method.equals(method2) || !Modifier.isNative(info2.modifiers)) {
continue;
}

boolean canBeValueGetter = false;
boolean canBeValueSetter = false;
boolean canBeMemberGetter = false;
boolean canBeMemberSetter = false;
if (canBeGetter && "get".equals(info.name) && "put".equals(methodName2)) {
if (canBeGetter && "get".equals(info.name) && "put".equals(info2.name)) {
canBeValueGetter = true;
} else if (canBeSetter && "put".equals(info.name) && "get".equals(methodName2)) {
} else if (canBeSetter && "put".equals(info.name) && "get".equals(info2.name)) {
canBeValueSetter = true;
} else if (methodName2.equals(info.name)) {
} else if (info2.name.equals(info.name)) {
info.overloaded = true;
canBeMemberGetter = canBeGetter;
canBeMemberSetter = canBeSetter;
Expand All @@ -2281,8 +2287,8 @@ MethodInformation methodInformation(Method method) {
}

boolean sameIndexParameters = true;
for (int j = 0; j < info.parameterTypes.length - skipParameters && j < parameterTypes2.length - skipParameters2; j++) {
if (info.parameterTypes[j + skipParameters] != parameterTypes2[j + skipParameters2]) {
for (int j = 0; j < info.parameterTypes.length - skipParameters && j < info2.parameterTypes.length - skipParameters2; j++) {
if (info.parameterTypes[j + skipParameters] != info2.parameterTypes[j + skipParameters2]) {
sameIndexParameters = false;
}
}
Expand All @@ -2292,23 +2298,23 @@ MethodInformation methodInformation(Method method) {

boolean parameterAsReturn = canBeValueGetter && info.parameterTypes.length > 0 &&
info.parameterTypes[0].isArray() && info.parameterTypes[0].getComponentType().isPrimitive();
boolean parameterAsReturn2 = canBeValueSetter && parameterTypes2.length > 0 &&
parameterTypes2[0].isArray() && parameterTypes2[0].getComponentType().isPrimitive();
boolean parameterAsReturn2 = canBeValueSetter && info2.parameterTypes.length > 0 &&
info2.parameterTypes[0].isArray() && info2.parameterTypes[0].getComponentType().isPrimitive();

if (canBeGetter && parameterTypes2.length - (parameterAsReturn ? 0 : 1) == info.parameterTypes.length - skipParameters
if (canBeGetter && info2.parameterTypes.length - (parameterAsReturn ? 0 : 1) == info.parameterTypes.length - skipParameters
&& (parameterAsReturn ? info.parameterTypes[info.parameterTypes.length - 1] : info.returnType) ==
parameterTypes2[parameterTypes2.length - 1] && (returnType2 == void.class || returnType2 == info.cls)
&& (parameterAnnotations2[parameterAnnotations2.length - 1].length == 0
|| (Arrays.equals(parameterAnnotations2[parameterAnnotations2.length - 1], info.annotations)))) {
info2.parameterTypes[info2.parameterTypes.length - 1] && (info2.returnType == void.class || info2.returnType == info.cls)
&& (info2.parameterAnnotations[info2.parameterAnnotations.length - 1].length == 0
|| (Arrays.equals(info2.parameterAnnotations[info2.parameterAnnotations.length - 1], info.annotations)))) {
pairedMethod = method2;
valueGetter = canBeValueGetter;
memberGetter = canBeMemberGetter;
noReturnGetter = parameterAsReturn;
} else if (canBeSetter && info.parameterTypes.length - (parameterAsReturn2 ? 0 : 1) == parameterTypes2.length - skipParameters2
&& (parameterAsReturn2 ? parameterTypes2[parameterTypes2.length - 1] : returnType2) ==
} else if (canBeSetter && info.parameterTypes.length - (parameterAsReturn2 ? 0 : 1) == info2.parameterTypes.length - skipParameters2
&& (parameterAsReturn2 ? info2.parameterTypes[info2.parameterTypes.length - 1] : info2.returnType) ==
info.parameterTypes[info.parameterTypes.length - 1] && (info.returnType == void.class || info.returnType == info.cls)
&& (info.parameterAnnotations[info.parameterAnnotations.length - 1].length == 0
|| (Arrays.equals(info.parameterAnnotations[info.parameterAnnotations.length - 1], annotations2)))) {
|| (Arrays.equals(info.parameterAnnotations[info.parameterAnnotations.length - 1], info2.annotations)))) {
pairedMethod = method2;
valueSetter = canBeValueSetter;
memberSetter = canBeMemberSetter;
Expand Down

0 comments on commit 9922f74

Please sign in to comment.