使用 Parcelable 代替序列化对象的好处

据我所知,BundleParcelable属于 Android 执行序列化的方式。例如,它用于在活动之间传递数据。但是我想知道,在例如将业务对象的状态保存到内部内存的情况下,使用 Parcelable而不是经典的序列化是否有好处?它会比传统方式更简单还是更快?我应该在哪里使用经典的序列化,哪里更适合使用捆绑包?

38596 次浏览

Based on this article http://www.mooproductions.org/node/6?page=5 Parcelable should be faster.

Not mentioned in the article, is that I don't htink that serializable objects will work in AIDL for remote services.

From "Pro Android 2"

NOTE: Seeing Parcelable might have triggered the question, why is Android not using the built-in Java serialization mechanism? It turns out that the Android team came to the conclusion that the serialization in Java is far too slow to satisfy Android’s interprocess-communication requirements. So the team built the Parcelable solution. The Parcelable approach requires that you explicitly serialize the members of your class, but in the end, you get a much faster serialization of your objects.

Also realize that Android provides two mechanisms that allow you to pass data to another process. The first is to pass a bundle to an activity using an intent, and the second is to pass a Parcelable to a service. These two mechanisms are not interchangeable and should not be confused. That is, the Parcelable is not meant to be passed to an activity. If you want to start an activity and pass it some data, use a bundle. Parcelable is meant to be used only as part of an AIDL definition.

Parcelable is mainly related to IPC using the Binder infrastructure, where data is passed as Parcels.

Since Android relies a lot on Binder for most, if not all, IPC tasks, it makes sense to implement Parcelable in most places, and especially in the framework, because it allows pass an object to another process if you need that. It makes objects "transportable".

But if you have a non Android-specific business layer which extensively use serializables to save object states, and only need to store them to the file system, then I think that serializable is fine. It allows to avoid the Parcelable boiler-plate code.

Serializable is comically slow on Android. Borderline useless in many cases in fact.

Parcel and Parcelable are fantastically quick, but its documentation says you must not use it for general-purpose serialization to storage, since the implementation varies with different versions of Android (i.e. an OS update could break an app which relied on it).

The best solution for the problem of serializing data to storage at a reasonable speed is to roll your own. I personally use one of my own utility classes which has a similar interface to Parcel and which can serialize all the standard types very efficiently (at the expense of type safety). Here's an abridged version of it :

public interface Packageable {
public void readFromPackage(PackageInputStream in)  throws IOException ;
public void writeToPackage(PackageOutputStream out)  throws IOException ;
}




public final class PackageInputStream {


private DataInputStream input;


public PackageInputStream(InputStream in) {
input = new DataInputStream(new BufferedInputStream(in));
}


public void close() throws IOException {
if (input != null) {
input.close();
input = null;
}
}


// Primitives
public final int readInt() throws IOException {
return input.readInt();
}
public final long readLong() throws IOException {
return input.readLong();
}
public final long[] readLongArray() throws IOException {
int c = input.readInt();
if (c == -1) {
return null;
}
long[] a = new long[c];
for (int i=0 ; i<c ; i++) {
a[i] = input.readLong();
}
return a;
}


...


public final String readString()  throws IOException {
return input.readUTF();
}
public final <T extends Packageable> ArrayList<T> readPackageableList(Class<T> clazz) throws IOException {
int N = readInt();
if (N == -1) {
return null;
}
ArrayList<T> list = new ArrayList<T>();
while (N>0) {
try {
T item = (T) clazz.newInstance();
item.readFromPackage(this);
list.add(item);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
N--;
}
return list;
}


}






public final class PackageOutputStream {


private DataOutputStream output;


public PackageOutputStream(OutputStream out) {
output = new DataOutputStream(new BufferedOutputStream(out));
}


public void close() throws IOException {
if (output != null) {
output.close();
output = null;
}
}


// Primitives
public final void writeInt(int val) throws IOException {
output.writeInt(val);
}
public final void writeLong(long val) throws IOException {
output.writeLong(val);
}
public final void writeLongArray(long[] val) throws IOException {
if (val == null) {
writeInt(-1);
return;
}
writeInt(val.length);
for (int i=0 ; i<val.length ; i++) {
output.writeLong(val[i]);
}
}


public final void writeFloat(float val) throws IOException {
output.writeFloat(val);
}
public final void writeDouble(double val) throws IOException {
output.writeDouble(val);
}
public final void writeString(String val) throws IOException {
if (val == null) {
output.writeUTF("");
return;
}
output.writeUTF(val);
}


public final <T extends Packageable> void writePackageableList(ArrayList<T> val) throws IOException {
if (val == null) {
writeInt(-1);
return;
}
int N = val.size();
int i=0;
writeInt(N);
while (i < N) {
Packageable item = val.get(i);
item.writeToPackage(this);
i++;
}
}


}

If you need serialization for i.e. storage purposes but want to avoid the speed penalty of reflection incurred by the Serializable interface you should explicitly create your own serialization protocol with the Externalizable interface.

When properly implemented this matches the speed of Parcelable and also accounts for compatibility between different versions of Android and/or the Java platform.

This article might clear things up as well:

What is the difference between Serializable and Externalizable in Java?

On a sidenote, it is also the fastest serialization technique in many benchmarks, beating Kryo, Avro, Protocol Buffers and Jackson (json):

http://code.google.com/p/thrift-protobuf-compare/wiki/Benchmarking

It seems that nowadays the difference isn't so noticeble, at least not when you run it between your own activities.

According to tests shown on this website , Parcelable is about 10 times faster on newest devices (like nexus 10), and is about 17 faster on old ones (like desire Z)

so it's up to you to decide if it worths it.

maybe for relatively small and simple classes, Serializable is fine, and for the rest, you should use Parcelable

See how fast Parcelable is than Serializable.


enter image description here

from WHY WE LOVE PARCELABLE


enter image description here

from Parcelable vs Serializable

I just use GSON -> Serialise to JSON String -> Restore Object from JSON String.

Also Parcelable offers custom implementation where the user gets an chance to parcel each of his objects by overriding the writeToParcel(), However serialization does not this custom implementation as its way of passing data involves JAVA reflection API.