<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">

<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>

                                <td>

                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="http://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">Community</a></h1>
                                                                </td>

                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px;  -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
    JBoss Reflect Performance Javassist vs Introspection
</h3>
<span style="margin-bottom: 10px;">
    reply from <a href="http://community.jboss.org/people/kabir.khan%40jboss.com">Kabir Khan</a> in <i>JBoss Microcontainer Development</i> - <a href="http://community.jboss.org/message/537798#537798">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">

<div class="jive-rendered-content"><p>I have done some performance measurements where I compare the times taken creating the following class, using javassist.bytecode.* and asm</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><div> </div><pre class="jive-pre"><code class="jive-code jive-java"><font color="navy"><b>public</b></font> <font color="navy"><b>class</b></font> JavassistMethod1 <font color="navy"><b>implements</b></font> JavassistMethod
<font color="navy">{</font> 
&#160; <font color="navy"><b>public</b></font> Object invoke(Object target, Object[] args) <font color="navy"><b>throws</b></font> Throwable
&#160; <font color="navy">{</font>&#160;&#160;&#160;&#160; 
&#160;&#160;&#160;&#160;&#160; <font color="navy"><b>return</b></font> Integer.valueOf(((SomeClass)target).someMethod(((Integer)args[0]).intValue(), (String)args[1])).intValue();
&#160; <font color="navy">}</font>
<font color="navy">}</font> 
</code></pre><div> </div><div> </div><div>Which would be used to call the method:</div><pre class="jive-pre"><code class="jive-code jive-java">&#160;&#160;&#160; <font color="navy"><b>int</b></font> someMethod(<font color="navy"><b>int</b></font> i, String);
</code></pre><div> </div><div> </div><div>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:</div><div> </div><div> </div><div>A) - Create class structure</div><div>B) - Add default constructor with body to call super</div><div>C) - Add invoke method</div><div>C1) - Add invoke method body</div><div>D) - Convert class structure from A) into byte[]</div><div>E) - Define java.lang.Class by calling ClassLoader.defineClass()</div><div>F) - Call Class.newInstance() </div><div> </div><div> </div><div> </div><div>1 - Average times (ms) to do A-F is:</div><div><table border="1" cellpadding="3" cellspacing="0" style="width: 100%; border: 1px solid #000000;"><tbody><tr><th align="center" style="background-color:#6690BC;" valign="middle"><span style="color: #ffffff;"><strong style="font-weight: bold;">Classes</strong></span></th><th align="center" style="background-color:#6690BC;" valign="middle"><span style="color: #ffffff;"><strong style="font-weight: bold;">ASM</strong></span></th><th align="center" style="background-color:#6690BC;" valign="middle"><span style="color: #ffffff;"><strong style="font-weight: bold;">Javassist</strong></span></th><th align="center" style="background-color:#6690BC;" valign="middle"><span style="color: #ffffff;"><strong style="font-weight: bold;">ASM/Javassist</strong></span></th></tr><tr><td>10000</td><td>1079</td><td>1585</td><td>.68</td></tr><tr><td>15000</td><td>1550</td><td>2153</td><td>.72</td></tr><tr><td>20000</td><td>1901</td><td>2665</td><td>.71</td></tr></tbody></table></div><div> </div><div> </div><div>2 - Taking the instantiation out of the equation, so we just do A-E:</div><div><table border="1" cellpadding="3" cellspacing="0" style="width: 100%; border: 1px solid #000000;"><tbody><tr><th align="center" style="background-color:#6690BC;" valign="middle"><span style="color: #ffffff;"><strong style="font-weight: bold;">Classes</strong></span></th><th align="center" style="background-color:#6690BC;" valign="middle"><span style="color: #ffffff;"><strong style="font-weight: bold;">ASM</strong></span></th><th align="center" style="background-color:#6690BC;" valign="middle"><span style="color: #ffffff;"><strong style="font-weight: bold;">Javassist</strong></span></th><th align="center" style="background-color:#6690BC;" valign="middle"><span style="color: #ffffff;">ASM/Javassist</span></th></tr><tr><td>20000</td><td>1290</td><td>1998</td><td>.64</td></tr></tbody></table></div><div> </div><div> </div><div>3 - Not loading the class, so we do A-D:</div><div><table border="1" cellpadding="3" cellspacing="0" style="width: 100%; border: 1px solid #000000;"><tbody><tr><th align="center" style="background-color:#6690BC;" valign="middle"><span style="color: #ffffff;"><strong style="font-weight: bold;">Classes</strong></span></th><th align="center" style="background-color:#6690BC;" valign="middle"><span style="color: #ffffff;"><strong style="font-weight: bold;">ASM</strong></span></th><th align="center" style="background-color:#6690BC;" valign="middle"><span style="color: #ffffff;"><strong style="font-weight: bold;">Javassist</strong></span></th><th align="center" style="background-color:#6690BC;" valign="middle"><span style="color: #ffffff;"><strong style="font-weight: bold;">ASM/Javassist</strong></span></th></tr><tr><td>20000</td><td>391</td><td>1115</td><td>.35</td></tr><tr><td>40000</td><td>499</td><td>1489</td><td>.34</td></tr></tbody></table><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p></div><div> </div><div>4 - Just doing A-C shows that javassist has a massive overhead in step D:</div><div><table border="1" cellpadding="3" cellspacing="0" style="width: 100%; border: 1px solid #000000;"><tbody><tr><th align="center" style="background-color:#6690BC;" valign="middle"><span style="color: #ffffff;"><strong style="font-weight: bold;">Classes</strong></span></th><th align="center" style="background-color:#6690BC;" valign="middle"><span style="color: #ffffff; font-weight: 800; ">ASM</span></th><th align="center" style="background-color:#6690BC;" valign="middle"><span style="color: #ffffff;"><strong style="font-weight: bold;">Javsssist</strong></span></th><th align="center" style="background-color:#6690BC;" valign="middle"><span style="color: #ffffff;"><strong style="font-weight: bold;">ASM/Javassist</strong></span></th></tr><tr><td>40000</td><td>524</td><td>747</td><td>.71</td></tr><tr><td>100000</td><td>823</td><td>1244</td><td>.66</td></tr></tbody></table></div><div> </div><div> </div><div>5 - Doing A-C with a simplified method body in C1 so it is just the equivalent of {return null;}:</div><div><table border="1" cellpadding="3" cellspacing="0" style="width: 100%; border: 1px solid #000000;"><tbody><tr><th align="center" style="background-color:#6690BC;" valign="middle"><span style="color: #ffffff;">Classes</span></th><th align="center" style="background-color:#6690BC;" valign="middle"><span style="color: #ffffff;"><strong style="font-weight: bold;">ASM</strong></span></th><th align="center" style="background-color:#6690BC;" valign="middle"><span style="color: #ffffff;"><strong style="font-weight: bold;">Javassist</strong></span></th><th align="center" style="background-color:#6690BC;" valign="middle"><span style="color: #ffffff;"><strong style="font-weight: bold;">ASM/Javassist</strong></span></th></tr><tr><td>40000</td><td>494</td><td>617</td><td>.80</td></tr><tr><td>100000</td><td>723</td><td>923</td><td>.78</td></tr></tbody></table></div><div> </div><div>6 - If I don't create the method in C at all, so I just do A-B </div><div><table border="1" cellpadding="3" cellspacing="0" style="width: 100%; border: 1px solid #000000;"><tbody><tr><th align="center" style="background-color:#6690BC;" valign="middle"><span style="color: #ffffff;"><strong>Classes</strong></span></th><th align="center" style="background-color:#6690BC;" valign="middle"><span style="color: #ffffff; font-weight: 800; ">ASM</span></th><th align="center" style="background-color:#6690BC;" valign="middle"><span style="color: #ffffff; font-weight: 800; ">Javassist</span></th><th align="center" style="background-color:#6690BC;" valign="middle"><span style="color: #ffffff; font-weight: 800; ">ASM/Javassist</span></th></tr><tr><td>40000</td><td>449</td><td>589</td><td>.76</td></tr><tr><td>100000</td><td>619</td><td>845</td><td>.73</td></tr></tbody></table></div><div> </div><div> </div><div>So unfortunately Javassist is slower than ASM when it comes to creating new classes. Both at generating the bytecode, and a lot slower at converting the javassist.bytecode.ClassFile to a byte[].</div><div> </div><div>The other overhead in this, as mentioned in a previous post, is the actual loading and instantiation of the classes themselves. So, I think generating less classes that do more stuff is the way forward, although that means a bigger overhead in creating the methods which will be bigger. I will play with creating an asm version of that.</div><div> </div></div>

<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
    <p style="margin: 0;">Reply to this message by <a href="http://community.jboss.org/message/537798#537798">going to Community</a></p>
        <p style="margin: 0;">Start a new discussion in JBoss Microcontainer Development at <a href="http://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2115">Community</a></p>
</div></td>
                        </tr>
                    </tbody>
                </table>


                </td>
            </tr>
        </tbody>
    </table>

</div>

</body>
</html>