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:
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.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
Don't use Binder, because it creates a proxy (if cross-process).
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).If you use Parcel multiple times, then give it back to the pool with
recycle()
.
精彩评论