Guest Post: How NimbleDroid helped Flipkart speed up app startup by 45%

We found Nimbledroid via http://androidweekly.net/ and checked it out starting in 2016. We had been looking for a service to help us identify where exactly in code we needed to improve Flipkart performance time.
Over the last five months NimbleDroid has helped us detect and fix several performance bottlenecks, as well as preventing two serious performance regressions from going live to millions of users. Below we describe the most interesting issue NimbleDroid identified.
Issue:
While reviewing NimbleDroid’s list of reported issues we discovered that parsing of JSON network responses was taking more time than expected. We use GSON extensively for parsing JSON responses into model objects which is done using reflection to determine the type of a variable or a field at runtime. And reflection is particularly slow on Android
According to NimbleDroid, out of ~4 sec of app Cold Startup time, GSON took approx ~1.3 seconds (since our JSON response is nested, and complex). To avoid this, we needed a way to mitigate reflection.
How we solved it:
GSON has a concept called Type Adapters. They are used to manually map the JSON fields to the model object. Using Type Adapters should improve performance as it does not use reflection. But writing Type Adapters for all the model classes (we had approx 350 models) would be a hectic and bug prone process.
To solve this, we used the Stag Library from Vimeo which uses annotation processing to generate type adapters at compile time. It leverages annotations to automatically generate reflection-less Type Adapters for your model objects.
Consider the following example to understand the basics of how Stag works:
Example:
We have a class LoginConfig in code
@UseStag
public class LoginConfig {
@SerializedName("enableSmartLock")
public boolean enableSmartLock;
}
We are using @UseStag annotation, so that it generates LoginConfigTypeAdapter at compile time for it that will do the Gson parsing in a reflectionless manner for LoginConfig model.
Other issues NimbleDroid identified:
-
Lazy initialization of data which is not needed at app startup.
-
Avoid Reflection in non-Gson code like instead of calling getSimpleName() every time we moved it to final String and add the name in that itself, as getsimpleName() uses reflection to calculate the class name.
App Startup time Improvement:
We are able to improve app launch time from 4 seconds to 2.2 seconds.
If you see the Graph we went to 2.8 and 3 seconds also when we moved to Retrofit from Volley in which we are doing initialisation on UI thread which causes a sudden increase in app startup time which we not at all able to figure out in code Review (code will only get merged when at least 2 developer gives the signoff) , but with NimbleDroid we were able to identify the issue and resolve it before going live.
Comparison with competitor apps:
Flipkart Performance as compare to other popular ecommerce apps in India.
As you see with a 2.2 second startup time we are far ahead of our competitors in the ecommerce domain.
NimbleDroid did an excellent job correctly figuring out bottlenecks and which code developer needed to be corrected to improve app startup time.