GSON Serialization very very slow

后端 未结 2 1631
小蘑菇
小蘑菇 2021-02-07 20:25

I\'m trying to serialize an array of 7000 POJO using GSON and the serialization time is extremely slow. It\'s on the order of 3-5 seconds to serialize an array of the following

2条回答
  •  孤城傲影
    2021-02-07 21:01

    I attempted to reproduce your problem and could not. I created 7000 objects with nontrivial data in them. On my ThinkPad it took Gson ~260ms to serialize ~3MB of Gson, which is a respectable ~10Mbps.

    A large fraction of that time was spent converting dates to strings. Converting the two date fields to 'long' saved about 50ms.

    I was able to save another ~10ms by migrating from tree adapters (JsonSerializer/JsonDeserializer) to the new streaming adapter class TypeAdaper. The code that sets this up looks like this:

        private static TypeAdapter> keyAdapter = new TypeAdapter>() {
            @Override public void write(JsonWriter out, Key value) throws IOException {
                out.value(value.value);
            }
    
            @Override public Key read(JsonReader in) throws IOException {
                if (in.peek() == JsonToken.NULL) {
                    in.nextNull();
                    return null;
                }
                return new Key(in.nextString());
            }
        };
    
        ...
    
        Gson gson = new GsonBuilder()
                .registerTypeAdapter(Key.class, keyAdapter)
                .create();
    

    The main difference between my scenario and yours is that I'm using my own bogus Key class. But if the Key was the bottleneck that should have come up when you manually serialized each case.

    Fixing the problem

    Your best next step is to remove fields from Case until serialization improves. It's possible that one of your fields contains something that's taking a long time to serialize: perhaps a very long string that requires excessive escaping? Once you isolate the problem report a bug to the Gson project and we'll happily fix the problem. In addition to including the code that reproduces the problem you should also include representative data.

提交回复
热议问题