commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Janek Bogucki <jane...@yahoo.co.uk>
Subject ObjectUtils.deserialize (...): Suggested improvement
Date Fri, 14 Dec 2001 23:47:52 GMT
Hi,

I have a suggestion for improving the performance of
ObjectUtils.deserialize( byte[] objectData ). I did
some tests which showed improvement for the
deserialisation of a serialised Strings with the new
version.

Here are the figures with elapsed times in millis:
    
Time to deserialize "Hello World!" 1000*1000 times:

    original      new
       21408    12186
       21259    12134
     
Time to deserialize a String 10*000 chars long
100*1000 times:

    original      new
       69611    66650
       69412    67809
     
Time to deserialize a String 100*000 chars long
10*1000 times:

    original      new
      122917   122837
      123742   122635

The change to get this improvement is to omit the
BufferedInputStream object as buffering won't be of
any benefit when the data is already in RAM.

(I also dropped the bin.close () invocation as this is
unneccesary since in.close () invokes bin.close ()
automatically.)

Many Thanks,
Janek Bogucki

Improved version
----------------
    /**
     * Deserializes a single object from an array of
bytes.
     *
     * @param objectData The serialized object.
     * @return The deserialized object, or
<code>null</code> on failure.
     */
    public static Object deserialize ( byte[]
objectData ) {
        Object object = null;
        if (objectData != null) {
            // These streams are closed in finally.
            ObjectInputStream in = null;
            ByteArrayInputStream bin =
            new ByteArrayInputStream (objectData) ;
            
            try {
                in = new ObjectInputStream (bin);
                
                // If objectData has not been
initialized, an
                // exception will occur.
                object = in.readObject ();
            }
            catch (Exception e) {
            }
            finally {
                try {
                    if (in != null) in.close ();
                }
                catch(IOException e) {
                }
            }
        }
        return object;
    }

__________________________________________________
Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
http://uk.my.yahoo.com

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message