GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing frogermcs.github.io — Dagger 2 - graph creation performance – froger_mcs dev blog – Coding with love {❤️} Dagger 2 - graph creation performance#PerfMatters - recently very popular hashtag, especially in the Android world. The times when the apps just did something, no matter how, are already gone. Now everything should be pleasant, smooth and fast. For example Instagram spent almost half a year just to make its app faster, more beautiful, and more screen-size aware. That’s why today I’d like to share with you short hint which can have a great impact on your app launch time (especially when it uses some external libraries). Objects graph creationIn the most cases over the time of app development process, its launch time increases more or less. Sometimes it’s hard to notice in day-by-day development but when you compare your first app version with the most recent one you may find that the difference is relatively big. And the reason may lay in a dagger objects graph creation process. Dagger 2 you may ask? Exactly - even if you just moved your implementation from other reflection-based solutions, and your code is generated in a compile time don’t forget that object creation still happens in the runtime. Objects (and its dependencies) are created at the first time when they are injected. Jake Wharton showed it clearly on this couple of slides of his presentation about Dagger 2: And here is how it looks in our GithubClient example app:
Bit tangled, but in result, before
created one after another. Hey, but don’t worry, much richer graphs can be created almost instantly. The problemNow let’s imagine that we have to add external library which should be initialized with the app start (e.g. Crashlytics, Mixpanel, Google Analytics, Parse, etc.). Let’s imagine that that it’s our In short - constructor is empty and its call costs almost nothing. But then we have Now our And what happens? Our app launches about 500ms longer, just because of MeasurementAndroid SDK (and Android Studio itself) gives us a tool for visualization of the application execution over time - Traceview. Thanks to this we can see how much time each method takes and find the bottlenecks, among others during the injection process. Btw if you haven’t seen it before, take a look at Udi Cohen’s blog post about Android performance optimizations. Traceview started directly from Android Studio (Android Monitor tab -> CPU -> Start/Stop Method Tracing) sometimes is not so precise, especially when we’re trying to hit “Start” at the moment of app launch. Fortunately for us there is a method which can be used when we know the exact place in code which should be measured. Debug.startMethodTracing() can be used to point out the place in our code where measurement should be started. For practice let’s measure our SplashActivity injection process:
According to the documentation results are saved on To pull them use this in your terminal:
Now all you need to do to read this file is just to drag it into Android Studio:
You should see something similar to this:
Of course results in this case are very clear: From pure curiosity let’s zoom a small piece of trace at the end:
Do you see the difference? Construction of our example class: In case you’d like to see it, here is SplashTrace.trace file used in this example. SolutionUnfortunately sometimes there are no clear answers for this kind of performance issues. Here are two of them which helped me a lot: Lazy loading (temporary solution)
First of all let’s think if you need all of injected dependencies. Maybe some of them can be lazy-loaded bit time later? Of course it doesn’t solve the real problem (UI thread will be blocked in a moment of first calling Lazy<>.get() method). But in some cases it can help with launch time (especially where objects are used rarely). Check Lazy<> interface documentation for more details and code sample. In short, every place where you use Async object creationThe second option (it’s still more the idea than the final solution) is to initialize object somewhere in a background thread and cache all methods calls and invoke them on the initialized object later. Disadvantage of this solution is that it has to be prepared independently for all classes which we want to wrap. And it will work only with those objects which methods calls can be performed in the future (like any kind of analytics - it’s ok if some events will be logged bit later). This is how this solution could look for our When For now the idea is very simple and still under development. There is a possibility to cause memory leaks (e.g. what if we have to pass any argument in callMethod()), but in general ot works for the simplest cases. Any other solutions?Performance improvement process is very individual. But if you would like to share your ideas, please, just do it here. Thanks for your reading! Source codeFull source code of described project is available on Github repository. AuthorMiroslaw Stanek If you liked this post, you can share it with your followers or follow me on Twitter!
Written on September 30, 2015
|
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |