-
Notifications
You must be signed in to change notification settings - Fork 868
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimize map accesses in several places #1644
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -327,9 +327,15 @@ private void discoverAccessibleMethods( | |
|
||
if (isPublic(mods) || isProtected(mods) || includePrivate) { | ||
MethodSignature sig = new MethodSignature(method); | ||
if (!map.containsKey(sig)) { | ||
if (includePrivate) method.trySetAccessible(); | ||
map.put(sig, method); | ||
if (includePrivate) { | ||
map.computeIfAbsent( | ||
sig, | ||
k -> { | ||
method.trySetAccessible(); | ||
return method; | ||
}); | ||
} else { | ||
map.putIfAbsent(sig, method); | ||
} | ||
} | ||
} | ||
|
@@ -346,7 +352,7 @@ private void discoverAccessibleMethods( | |
Method[] methods = clazz.getMethods(); | ||
for (Method method : methods) { | ||
MethodSignature sig = new MethodSignature(method); | ||
if (!map.containsKey(sig)) map.put(sig, method); | ||
map.putIfAbsent(sig, method); | ||
} | ||
break; // getMethods gets superclass methods, no | ||
// need to loop any more | ||
|
@@ -387,9 +393,7 @@ void discoverPublicMethods(Class<?> clazz, Map<MethodSignature, Method> map) { | |
static void registerMethod(Map<MethodSignature, Method> map, Method method) { | ||
MethodSignature sig = new MethodSignature(method); | ||
// Array may contain methods with same signature but different return value! | ||
if (!map.containsKey(sig)) { | ||
map.put(sig, method); | ||
} | ||
map.putIfAbsent(sig, method); | ||
} | ||
|
||
static final class MethodSignature { | ||
|
@@ -595,23 +599,21 @@ private void reflect( | |
NativeJavaMethod setters = null; | ||
String setterName = "set".concat(nameComponent); | ||
|
||
if (ht.containsKey(setterName)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can safely remove |
||
// Is this value a method? | ||
Object member = ht.get(setterName); | ||
if (member instanceof NativeJavaMethod) { | ||
NativeJavaMethod njmSet = (NativeJavaMethod) member; | ||
if (getter != null) { | ||
// We have a getter. Now, do we have a matching | ||
// setter? | ||
Class<?> type = getter.method().getReturnType(); | ||
setter = extractSetMethod(type, njmSet.methods, isStatic); | ||
} else { | ||
// No getter, find any set method | ||
setter = extractSetMethod(njmSet.methods, isStatic); | ||
} | ||
if (njmSet.methods.length > 1) { | ||
setters = njmSet; | ||
} | ||
// Is this value a method? | ||
Object member = ht.get(setterName); | ||
if (member instanceof NativeJavaMethod) { | ||
NativeJavaMethod njmSet = (NativeJavaMethod) member; | ||
if (getter != null) { | ||
// We have a getter. Now, do we have a matching | ||
// setter? | ||
Class<?> type = getter.method().getReturnType(); | ||
setter = extractSetMethod(type, njmSet.methods, isStatic); | ||
} else { | ||
// No getter, find any set method | ||
setter = extractSetMethod(njmSet.methods, isStatic); | ||
} | ||
if (njmSet.methods.length > 1) { | ||
setters = njmSet; | ||
} | ||
} | ||
// Make the property. | ||
|
@@ -687,13 +689,11 @@ private Field[] getAccessibleFields(boolean includeProtected, boolean includePri | |
private static MemberBox findGetter( | ||
boolean isStatic, Map<String, Object> ht, String prefix, String propertyName) { | ||
String getterName = prefix.concat(propertyName); | ||
if (ht.containsKey(getterName)) { | ||
// Check that the getter is a method. | ||
Object member = ht.get(getterName); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here. Do not hit map twice |
||
if (member instanceof NativeJavaMethod) { | ||
NativeJavaMethod njmGet = (NativeJavaMethod) member; | ||
return extractGetMethod(njmGet.methods, isStatic); | ||
} | ||
// Check that the getter is a method. | ||
Object member = ht.get(getterName); | ||
if (member instanceof NativeJavaMethod) { | ||
NativeJavaMethod njmGet = (NativeJavaMethod) member; | ||
return extractGetMethod(njmGet.methods, isStatic); | ||
} | ||
return null; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we will save one hash computation, but have the price of an additional invocation of a lambda expression:
I did this short test:
it runs with the old code in apporx 8,5sec and with the new code approx 5,5 sec