开发者

Using protobuffers instead of Parcelables in Android intent extras?

Would it be a good idea to use protocol buffer o开发者_如何学编程bjects (serialized to byte arrays) to pass as intent extras between Android activities instead of implementing Parcelable on classic POJOs? How would it affect performance?

Thanks Markus


To know for sure you'd have to do a test for your particular case.

Things to keep in mind about Parcel:

  1. As you may know, it's a manual process: you have to walk your object tree and call Parcel's various serialization method, e.g. writeFloatArray(..). There is no magic and it is as low-level as it gets.

  2. Parcel.java is a wrapper around native implementation, so it should be pretty optimized already: http://www.google.com/codesearch/p?hl=en#uX1GffpyOZk/libs/binder/Parcel.cpp&q=Parcel.cpp%20package:android&sa=N&cd=1&ct=rc

  3. Don't use Binder, because it creates a proxy (if cross-process).

  4. Probably most important: size the initial buffers properly with setDataCapacity(int size). This way internal array will not need to be grown (= memory alloc + copy).

  5. If you use Parcel multiple times, then give it back to the pool with recycle().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜