You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Jeff Varszegi <jv...@yahoo.com> on 2002/11/17 00:39:50 UTC

[lang] Performance enhancement for SerializationUtils

Here is the code for the serialize(Serializable) method:

    public static byte[] serialize(Serializable obj) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        serialize(obj, baos);
        return baos.toByteArray();
    }

This can be sped up a significant amount just by using a different constructor for the
ByteArrayInputStream instance, passing in a number higher than 32, so it constructs a bigger
initial buffer than it otherwise would.  Bumping up the initial buffer size to 128 worked about as
well as bumping it up to 512 on my machine.

This alone resulted in an overall 6% speed increase for the clone(Serializable) method on my
machine.

For the methods serialize(Serializable, OutputStream) and deserialize(InputStream) it may be
desirable to wrap the passed streams in buffered streams.  Serialization to/from streams is
typically done for persistence, passing objects between JVMs, etc. and it's not hard to imagine
someone passing in unbuffered streams in these situations...

Jeff

__________________________________________________
Do you Yahoo!?
Yahoo! Web Hosting - Let the expert host your site
http://webhosting.yahoo.com

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [lang] Performance enhancement for SerializationUtils

Posted by Stephen Colebourne <sc...@btopenworld.com>.
Thanks for this advice, I've included it in the CVS.

(I've added specific comments for the stream methods about buffering)

Stephen

----- Original Message -----
From: "Jeff Varszegi" <jv...@yahoo.com>
> Here is the code for the serialize(Serializable) method:
>
>     public static byte[] serialize(Serializable obj) {
>         ByteArrayOutputStream baos = new ByteArrayOutputStream();
>         serialize(obj, baos);
>         return baos.toByteArray();
>     }
>
> This can be sped up a significant amount just by using a different
constructor for the
> ByteArrayInputStream instance, passing in a number higher than 32, so it
constructs a bigger
> initial buffer than it otherwise would.  Bumping up the initial buffer
size to 128 worked about as
> well as bumping it up to 512 on my machine.
>
> This alone resulted in an overall 6% speed increase for the
clone(Serializable) method on my
> machine.
>
> For the methods serialize(Serializable, OutputStream) and
deserialize(InputStream) it may be
> desirable to wrap the passed streams in buffered streams.  Serialization
to/from streams is
> typically done for persistence, passing objects between JVMs, etc. and
it's not hard to imagine
> someone passing in unbuffered streams in these situations...
>
> Jeff
>
> __________________________________________________
> Do you Yahoo!?
> Yahoo! Web Hosting - Let the expert host your site
> http://webhosting.yahoo.com
>
> --
> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
> For additional commands, e-mail:
<ma...@jakarta.apache.org>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>