Why was “Avoid Enums Where You Only Need Ints” rem

2019-01-04 05:19发布

The section "Avoid Enums Where You Only Need Ints" was removed from the official developer documentation. (See Why doesn't Android use more enums? for the old section content)

Why? Was there a change in the Android VM that made the tip obsolete?

标签: android enums
5条回答
Summer. ? 凉城
2楼-- · 2019-01-04 06:04

A guess:

  • Gigahertz CPUs like Hummingbird and Snapdragon are now common, and the small-code small-memory requirements which originally constrained the Dalvik VM are no longer as true.
  • Every shipping device uses the JIT (new to 2.2). The enum’s class initializer will run faster, the values might be treated as JIT-time constants, and the JIT might well have special support for streamlining enum classes.
  • Code which is really performance-sensitive uses the NDK, which was still new and unpolished when Android 1.5 was released. The NDK in 2.3 supports native activities, which allows for near-fully unmanaged games.

Thus, for the comparatively mundane requirements of a GUI app, the development-time benefits of enums far outweigh the extra runtime cost.

查看更多
贪生不怕死
3楼-- · 2019-01-04 06:05

Elliott Hughes offers more details about the documentation rewrite on his blog: http://elliotth.blogspot.com/2010/09/java-benchmarks.html

The second half of the post explains that every claim on the Performance doc is now backed up with benchmarks. Previous versions of the doc apparently contained unverified claims, like, "Avoid enums because they are too expensive."

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-01-04 06:08

The 2011 answer from Elliot Hugues said that the original reason to avoid enum was for performance reason... as in "processing performance". As this reason was not backed by fact, it was removed from the official documentation.

It has been added later on because enums add a lot more data in memory than using integer.

查看更多
smile是对你的礼貌
5楼-- · 2019-01-04 06:09

the original version of that document was just a bunch of prejudices. it's been rewritten to only contain facts backed up by actual benchmarks, and it's updated as the VM is updated. you can find the various benchmarks -- plus some of the benchmarks we use to optimize the core libraries -- at http://code.google.com/p/dalvik/.

查看更多
相关推荐>>
6楼-- · 2019-01-04 06:11
登录 后发表回答