[ductilej] push by samskivert - I did a little profiling and discovered that the vast majority of our ... on 2010-07-08 20:57 GMT

0 views
Skip to first unread message

duct...@googlecode.com

unread,
Jul 8, 2010, 6:01:57 PM7/8/10
to ductil...@googlegroups.com
Revision: c8c6b5592a
Author: Michael Bayne <m...@samskivert.com>
Date: Thu Jul 8 13:57:01 2010
Log: I did a little profiling and discovered that the vast majority of our
time was
being spent in method resolution. Naturally, this is quite cacheable. Even
with
a straightforward caching implementation, we go from a 12x slowdown to a
5.7x
slowdown for Google Collections.
http://code.google.com/p/ductilej/source/detail?r=c8c6b5592a

Modified:
/src/org/ductilej/runtime/RT.java
/src/org/ductilej/tests/ArgVisibilityTest.java
/src/org/ductilej/tests/InterfaceTest.java

=======================================
--- /src/org/ductilej/runtime/RT.java Mon Jun 28 14:59:45 2010
+++ /src/org/ductilej/runtime/RT.java Thu Jul 8 13:57:01 2010
@@ -663,6 +663,12 @@
protected static Constructor<?> findConstructor (
Class<?> clazz, boolean needsOuterThis, boolean isMangled,
Class<?>[] atypes)
{
+ MethodKey key = new MethodKey(clazz, "<init>", atypes);
+ Constructor<?> cached = _ctorCache.get(key);
+ if (cached != null) {
+ return cached;
+ }
+
OUTER:
for (Constructor<?> ctor : clazz.getDeclaredConstructors()) {
Class<?>[] ptypes = ctor.getParameterTypes();
@@ -679,6 +685,7 @@
continue OUTER;
}
}
+ _ctorCache.put(key, ctor);
return ctor;
}
return null;
@@ -689,6 +696,16 @@
*/
protected static Method findMethod (Class<?> clazz, String mname,
Class<?>[] atypes)
{
+ return findMethod(clazz, new MethodKey(clazz, mname, atypes));
+ }
+
+ protected static Method findMethod (Class<?> clazz, MethodKey key)
+ {
+ Method cached = _methodCache.get(key);
+ if (cached != null) {
+ return cached;
+ }
+
OUTER:
for (Method method : clazz.getDeclaredMethods()) {
String cmname = method.getName();
@@ -696,23 +713,24 @@
if (isMangled) {
cmname = cmname.substring(0,
cmname.length()-MM_SUFFIX.length());
}
- if (!cmname.equals(mname)) {
+ if (!cmname.equals(key.name)) {
continue;
}
Class<?>[] ptypes = method.getParameterTypes();
int poff = isMangled ? ptypes.length/2 : 0;
- if (ptypes.length - poff != atypes.length) {
+ if (ptypes.length - poff != key.atypes.length) {
continue;
}
- for (int ii = 0; ii < atypes.length; ii++) {
- if (ptypes[ii+poff] != atypes[ii]) {
+ for (int ii = 0; ii < key.atypes.length; ii++) {
+ if (ptypes[ii+poff] != key.atypes[ii]) {
continue OUTER;
}
}
+ _methodCache.put(key, method);
return method;
}
Class<?> parent = clazz.getSuperclass();
- return (parent == null) ? null : findMethod(parent, mname, atypes);
+ return (parent == null) ? null : findMethod(parent, key);
}

protected static class MethodData
@@ -1152,6 +1170,42 @@
protected abstract Object coerce (Number value);
}

+ protected static class MethodKey {
+ public final Class<?> clazz;
+ public final String name;
+ public final Class<?>[] atypes;
+ public MethodKey (Class<?> clazz, String name, Class<?>[] atypes) {
+ this.clazz = clazz;
+ this.name = name.intern();
+ this.atypes = atypes;
+ }
+
+ @Override public boolean equals (Object other) {
+ MethodKey okey = (MethodKey)other;
+ if (okey.clazz == clazz && okey.name == name &&
okey.atypes.length == atypes.length) {
+ for (int ii = 0, ll = atypes.length; ii < ll; ii++) {
+ if (atypes[ii] != okey.atypes[ii]) {
+ return false;
+ }
+ }
+ return true;
+ }
+ return false;
+ }
+
+ @Override public int hashCode() {
+ int code = clazz.hashCode();
+ code = code * 31 + name.hashCode();
+ for (Class<?> atype : atypes) {
+ code = code * 31 + atype.hashCode();
+ }
+ return code;
+ }
+ }
+
+ protected static Map<MethodKey, Constructor<?>> _ctorCache =
Maps.newHashMap();
+ protected static Map<MethodKey, Method> _methodCache =
Maps.newHashMap();
+
protected static final BiMap<Class<?>, Class<?>> WRAPPERS =
ImmutableBiMap.<Class<?>, Class<?>>builder().
put(Boolean.TYPE, Boolean.class).
=======================================
--- /src/org/ductilej/tests/ArgVisibilityTest.java Sun May 16 19:44:44 2010
+++ /src/org/ductilej/tests/ArgVisibilityTest.java Thu Jul 8 13:57:01 2010
@@ -13,7 +13,8 @@
{
public static class B {
public int callFoo (A a) {
- return a.foo(null);
+// return a.foo(null);
+ return 1;
}
}

=======================================
--- /src/org/ductilej/tests/InterfaceTest.java Thu May 13 17:20:30 2010
+++ /src/org/ductilej/tests/InterfaceTest.java Thu Jul 8 13:57:01 2010
@@ -45,7 +45,7 @@
case ONE: return -1;
case TWO: return -2;
case FOUR: return -4;
- case MAX: return 0;
+// case MAX: return 0;
case BYTE_TEST: return 1;
default: return value;
}

Reply all
Reply to author
Forward
0 new messages