为什么Parcelable.Creator字段会显着降低杰克逊的表现?

时间:2015-06-21 18:13:17

标签: java android json jackson

我从文件中读取JSON数据并将内容转换为我的模型对象。我最近注意到性能下降,调试后我发现Parcelable.Creator字段是原因。

在将String转换为我的模型类时,简单地删除Parcelable.Creator字段会导致速度提高100-200%。

为什么?

以下是我的时间安排,第一个是Fw课 Parcelable.Creator字段,FwNoParcel是完全相同的课程没有任何Parcelable.Creator字段。

D/BaseActivity$Player﹕ Stats for ReadFw; Took 3086 ms.
D/BaseActivity$Player﹕ Stats for ReadFwNoParcel; Took 1281 ms.

以下是我正在玩的模型对象。

public static final class Fw implements Parcelable{
    public String id;
    public String name;
    public String comment;
    public String programId;
    public int programDayIndex = -1;
    public long startTime;
    public long endTime;
    public ArrayList<FitnessActivity> activities;

    public Fw() {
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public Fw(Parcel in) {
        id = in.readString();
        name = in.readString();
        comment = in.readString();
        programId = in.readString();
        programDayIndex = in.readInt();
        startTime = in.readLong();
        endTime = in.readLong();
        in.readList(activities, FitnessActivity.class.getClassLoader());
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(id);
        dest.writeString(name);
        dest.writeString(comment);
        dest.writeString(programId);
        dest.writeInt(programDayIndex);
        dest.writeLong(startTime);
        dest.writeLong(endTime);
        dest.writeList(activities);
    }

    public static final Creator<Fw> CREATOR = new Creator<Fw>() {
        @Override
        public Fw createFromParcel(Parcel in) {
            return new Fw(in);
        }

        @Override
        public Fw[] newArray(int size) {
            return new Fw[size];
        }
    };
}

最后是我用来实际读取文件并将其转换为模型对象的代码。 (为便于阅读而简化)。

private static final ObjectMapper MAPPER = ...

//getFw
//getFwNoParcel is exactly identical.
final File file = ... //A file containing tons of data in JSON format.
final CollectionType type 
    = MAPPER.getTypeFactory().constructCollectionType(List.class, Fw.class);
return MAPPER.reader(type).readValue(file);

0 个答案:

没有答案