Kabir Khan wrote:
I have done some performance measurements where I compare the times taken creating the following class, using javassist.bytecode.* and asm
public class JavassistMethod1 implements JavassistMethod
{
public Object invoke(Object target, Object[] args) throws Throwable
{
return Integer.valueOf(((SomeClass)target).someMethod(((Integer)args[0]).intValue(), (String)args[1])).intValue();
}
}
Which would be used to call the method:
int someMethod(int i, String);
The basic flow for what I do for both approaches is the same, whereby I do the following lots of times to generate lots of similar classes:
A) - Create class structure
B) - Add default constructor with body to call super
C) - Add invoke method
C1) - Add invoke method body
D) - Convert class structure from A) into byte[]
E) - Define java.lang.Class by calling ClassLoader.defineClass()
F) - Call Class.newInstance()
Chiba has done some great work on creating a new API for Javassist tailor made to create new classes. Taking the defining of the class and instantiating it out of the equation (since that is JVM stuff out of our control), so we do A-D so we have the bytes ready to create the class the times are now for creating 20000 JavassistMethod implementations
| ASM | Javassist ClassFile | JavassistClassFileWriter |
|---|
| 476 | 1030 | 356 |
| 613 | 1056 | 269 |
| 483 | 1076 | 309 |
| 464 | 1001 | 357 |
| 383 | 1186 | 315 |
I have attached the modified benchmark