2

Our Android app currently uses Otto EventBus, which uses reflection. We want to avoid the overhead of reflection, but keep the flexibility. Does Guava's event bus use reflection? What about GreenRobot's?

If they don't do they use code generation or something like that?

Ginandi
  • 843
  • 8
  • 20
  • GreenRobot EventBus3 uses APT instead of Reflection, afaik – EpicPandaForce Nov 22 '16 at 13:39
  • 1
    "We want to avoid the overhead of reflection" Are you sure this is actually a problem in your app? When we (Square) prototyped a code generated Otto 4 years ago the number of subscribers had to be ludicrous for it to make any actual difference. And that was back in the Dalvik days and also before they fixed annotation reflection on API 14+. Frankly comments like this often are the result of optimizing something that isn't actually a problem and hasn't been accurately measured. – Jake Wharton Nov 22 '16 at 22:15
  • @Jake Wharton We have not measured it. As a rule of thumb, I tent to avoid reflection when I want to optimize for performance. If you have any data you can share I'd be happy to change my mind. – Ginandi Nov 23 '16 at 08:04
  • 2
    Unfortunately the data has been lost to the years. I will say that layout inflation does something like 10 - 100x more reflection than Otto ever would though. – Jake Wharton Nov 23 '16 at 14:33
  • We are actually using React Native for our UI (which has its own performance overhead). But in general, most of this code runs on a service, in the background, so UI slowness is not a concern here. – Ginandi Nov 23 '16 at 14:39

1 Answers1

4

Otto was never nearly as feature-packed as GreenRobot's EventBus - no threading modes, for example, so it's good riddance. And Otto was deprecated in favor of RxJava - which is massive overkill for many projects (personal opinion).



But in order to reduce reflection usage, GreenRobot EventBus 3.x is able to build an index in compilation time using APT rather than runtime reflection.

http://greenrobot.org/eventbus/documentation/subscriber-index/


Index Preconditions: Note that only @Subscriber methods can be indexed for which the subscriber AND event class are public. Also, due to technical limitations of Java’s annotation processing itself, @Subscribe annotations are not recognized inside of anonymous classes.

buildscript {
    dependencies {
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
}



apply plugin: 'com.neenbedankt.android-apt'

dependencies {
    compile 'org.greenrobot:eventbus:3.0.0'
    apt 'org.greenrobot:eventbus-annotation-processor:3.0.1'
}

apt {
    arguments {
        eventBusIndex "com.example.myapp.MyEventBusIndex"
    }
}

And

EventBus.builder().addIndex(new MyEventBusIndex()).installDefaultEventBus();
// Now the default instance uses the given index. Use it like this:
EventBus eventBus = EventBus.getDefault();

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428