Skip to main content

Why are annotations under Android such a performance issue (slow)?



I'm the lead author of ORMLite which uses Java annotations on classes to build database schemas. A big startup performance problem for our package turns out to be the calling of annotation methods under Android 1.6. I see the same behavior up through 3.0.





We are seeing that the following simple annotation code is incredibly GC intensive and a real performance problem. 1000 calls to an annotation method takes almost a second on a fast box. The same code under Java can do 28 million (sic) calls in the same time. We have an annotation that has 25 methods in it and we'd like to do more than 50 of these a second.





Does anyone know why this is happening and if there is any work around? There are certainly things that ORMLite can do in terms of caching this information but is there anything that we can do to "fix" annotations under Android? Thanks.







public void testAndroidAnnotations() throws Exception {

Field field = Foo.class.getDeclaredField("field");

MyAnnotation myAnnotation = field.getAnnotation(MyAnnotation.class);

long before = System.currentTimeMillis();

for (int i = 0; i < 1000; i++)

myAnnotation.foo();

Log.i("test", "in " + (System.currentTimeMillis() - before) + "ms");

}

@Target(FIELD) @Retention(RUNTIME)

private static @interface MyAnnotation {

String foo();

}

private static class Foo {

@MyAnnotation(foo = "bar")

String field;

}







This results in the following log output:







I/TestRunner( 895): started: testAndroidAnnotations

D/dalvikvm( 895): GC freed 6567 objects / 476320 bytes in 85ms

D/dalvikvm( 895): GC freed 8951 objects / 599944 bytes in 71ms

D/dalvikvm( 895): GC freed 7721 objects / 524576 bytes in 68ms

D/dalvikvm( 895): GC freed 7709 objects / 523448 bytes in 73ms

I/test ( 895): in 854ms







EDIT:





After @candrews pointed me in the right direction, I did some poking around the code. The performance problem looks to be caused by some terrible, gross code in Method.equals() . It is calling the toString() of both methods and then comparing them. Each toString() use StringBuilder with a bunch of append methods without a good initializing size. Doing the .equals by comparing fields would be significantly faster.





EDIT:





An interesting reflection performance improvement was given to me. We are now using reflection to peek inside the AnnotationFactory class to read the list of fields directly. This makes the reflection class 20 times faster for us since it bypasses the invoke which is using the method.equals() call. It is not a generic solution but here's the Java code from ORMLite SVN repository .



Source: Tips4all

Comments

  1. Google has acknowledged the issue and fixed it "post-Honeycomb"


    https://code.google.com/p/android/issues/detail?id=7811


    So at least they know about it and have supposedly fixed it for some future version.

    ReplyDelete
  2. Both GC and reflection are multitudes slower on dalvik compared to JVMs. From what I´ve manged to pick up it's basically because of design decisions made for the mobile platform.

    Can you do anything about the @Retention(RUNTIME)?

    You are supposed to reuse objects by overwriting value. See for example how getView is implemented on adapters.

    getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;


    where you reuse an old view and write new values to it instead of throwing it away to make a new one.

    ReplyDelete
  3. I think if you manage to change the RUNTIME retention policy, it should not be that slow.

    EDIT: I know, for your project that may not be an option. Perhaps it is more a problem of what you are doing with that annotation rather than bad performance in general.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.