Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

enum vs android @Intdef - which one is better optimized

I know that when comparing constants to enums constants take up less space and can be primitive. I am researching @Intdef annotation in android and can someone tell me if its better storage to use @Intdef vs a enum. Is it recommended now in android to put enum aside and use @intdef moving forward when possible ? can @Intdef do polymorphism, i doubt?

from the android docs regarding memory overhead:

Enums often require more than twice as much memory as static constants. You should strictly avoid using enums on Android.

like image 456
j2emanue Avatar asked Jan 04 '16 01:01

j2emanue


2 Answers

@Intdef is clearly more efficient, it carries zero weight over just having static final ints, it's all compile time instructions. enums are classes and as mentioned in your link have a foot print. @Intdef gets you the most basic functionality of enums namely value validation and none of the other features of enums such as automatic String conversions.

Much of the android docs is stale, this could very well be one of them. Back in the early days of android every bit counted but now devices are much more capable. Personally I would choose between those two options depending on what's called for by design and not get too caught up with being efficient. Also the syntax for some of these more advanced annotations doesn't make for clean easy to read code, so not a fan there. However if the situation calls for good old static ints the @Intdef will buy you some protection at the expense of visual clutter.

like image 130
user3259330 Avatar answered Nov 16 '22 16:11

user3259330


In addition to previous answers, I would add that if you are using Proguard (and you should definitely do it to reduce size and obfuscate your code), then your Enums will be automatically converted to @IntDef wherever it is possible:

https://www.guardsquare.com/manual/configuration/optimizations

class/unboxing/enum

Simplifies enum types to integer constants, whenever possible.

Therefore, if you have some discrete values and some method should allow to take only this values and not others of the same type, then I would use Enum, because Proguard will make this manual work of optimizing code for me.

And here is a good post about using enums from Jake Wharton, take a look at it.

As a library developer, I recognize these small optimizations that should be done as we want to have as little impact on the consuming app's size, memory, and performance as possible. But it's important to realize that throwing away an Iterator allocation vs. an indexed loop, using a HashMap vs. a binary-searched collection like SparseArray, and putting an enum in your public API vs. integer values where appropriate is perfectly fine. Knowing the difference to make informed decisions is what's important and the video nearly nails that except for this one stupid stat.

like image 41
Gaket Avatar answered Nov 16 '22 18:11

Gaket