GSON Serialization very very slow

后端 未结 2 1632
小蘑菇
小蘑菇 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<Key<String>> keyAdapter = new TypeAdapter<Key<String>>() {
            @Override public void write(JsonWriter out, Key<String> value) throws IOException {
                out.value(value.value);
            }
    
            @Override public Key<String> read(JsonReader in) throws IOException {
                if (in.peek() == JsonToken.NULL) {
                    in.nextNull();
                    return null;
                }
                return new Key<String>(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.

    0 讨论(0)
  • 2021-02-07 21:13

    how about using flat buffers over json.

    https://medium.freecodecamp.com/why-consider-flatbuffer-over-json-2e4aa8d4ed07#.d79exjq8n

    0 讨论(0)
提交回复
热议问题