-
Notifications
You must be signed in to change notification settings - Fork 52
Performance improvements using an annotation processor #19
Comments
Hi Stéphane, Thanks for your request. I have already thought about this option and here is what I came to. There are three main operations to be implemented.
Operation 1 can be optimized by using annotation processor. At the same time this is one-time operation. It's executed only once, when a callback is registered for the first time. The possible performance gain can be only expected on application start-up, which is not bad. Operation 2 cannot be optimized due to dynamic nature of callbacks. Callbacks are registered / unregistered in any sequence. A dynamic structure similar to the one I use now is still required. Operation 3 can be optimized by using annotation processor. Generated event dispatch method will contain a sequence of if-else statements calling certain callback's method depending on the event type, which in worst case will have O(n) code complexity. Right now I use a HashMap with quite constant O(1) complexity. I'm not sure whether direct Java calls can compensate much this comparing to a reflection calls I do now. My conclusion was like this. Before I start to optimized something I need to exactly know where are the bottlenecks. It would be interesting to see some numbers showing slow application start-up or slow event dispatching. I'm not saying this is not needed at all. Bus as for now, I would say this has lower priority than other tasks. |
I found some more stuff on this. Jake worked on a AnnotationProcessor for Otto library about two years ago and he stopped to push this branch forward. https://github.com/square/otto/tree/jw/code-gen/library/src/main/java/com/squareup/otto/internal There must be some reasons for that. I suspect it's because resulting gain was not worth the effort. But I might be wrong. Let's close this issue for now and once we have a clear need for such optimization we can revisit it and reopen it again. |
I think you underestimate the gain of not using reflection. But nw, it's 2014-11-17 14:31 GMT-08:00 Sergej Shafarenka notifications@github.com:
|
Btw, Jake can be reached if you need to.. you can maybe ping him via the 2014-11-17 14:33 GMT-08:00 Stéphane NICOLAS steff.nicolas@gmail.com:
|
Hi @beworker,
congratulation for this project and being referenced on Android Weekly.
I looked at your source code and it looks like you are using a lot of reflection to discover the methods that are annotated to subscribe to or produce events (in https://github.com/beworker/tinybus/blob/master/tinybus/src/com/halfbit/tinybus/ObjectMeta.java).
You could actually get some performance gains by using an annotation processor to detect all such methods at compile time and get references to them. Thus you would not need to scan the classes, you would know in advance where such methods are in a given class.
You can find an example of such an annotation processor in :
if you need more high level explanations, please refer to these slides :
https://speakerdeck.com/stephanenicolas/blender-boosting-guice-with-annotation-processing
The text was updated successfully, but these errors were encountered: