Nov11 / kryo

Automatically exported from code.google.com/p/kryo
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

The size of the serialized array which contains the same String refrence is larger than java built-in #43

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1.create a string array which contains the same String reference
2.serialize the array with kryo and java built-in
3.you will see the size of kryo is much more than java

What is the expected output? What do you see instead?
I think the same String should only be serialized once.
And the size of the array should be smaller than java built-in.

What version of the Kryo are you using?
1.04

Please provide any additional information below.
the code is here:
@Test
    public void testStringSer() throws IOException{
        char[] chars = new char[1500];
        Arrays.fill(chars, 'a');
        String str = new String(chars);
        testStringSer(str);

        String[] strArray = new String[]{str, str, str};
        testStringArraySer(strArray);
    }

    private void testStringSer(String str) throws IOException{
        Kryo kryo = new Kryo();
        kryo.setRegistrationOptional(true);
        ObjectBuffer ob = new ObjectBuffer(kryo, 5 * 1024);
        byte[] bytesKryo = ob.writeObjectData(str);
        System.out.println("kryo byte length of string:"+ bytesKryo.length);

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(str);
        byte[] bytesJava = bos.toByteArray();
        bos.close();
        oos.close();
        System.out.println("java byte length of string:"+bytesJava.length);
    }

    private void testStringArraySer(String[] strArray) throws IOException{
        Kryo kryo = new Kryo();
        kryo.setRegistrationOptional(true);
        ObjectBuffer ob = new ObjectBuffer(kryo, 5 * 1024);
        byte[] bytesKryo = ob.writeObjectData(strArray);
        System.out.println("kryo byte length of string array:"+ bytesKryo.length);

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(strArray);
        byte[] bytesJava = bos.toByteArray();
        bos.close();
        oos.close();
        System.out.println("java byte length of string array:"+bytesJava.length);
    }

and the result is:
kryo byte length of string:1502
java byte length of string:1507
kryo byte length of string array:4511
java byte length of string array:1557

Original issue reported on code.google.com by ma.yue.c...@gmail.com on 20 Apr 2011 at 4:24

GoogleCodeExporter commented 8 years ago
Use ReferenceFieldSerializer if you want to write the same object in the graph 
more than once.

Original comment by nathan.s...@gmail.com on 23 Oct 2011 at 1:11