Boost logo

Boost Users :

Subject: Re: [Boost-users] [Serialization] Speedding up client-server communication
From: Juraj Ivančić (juraj.ivancic_at_[hidden])
Date: 2010-02-05 08:36:10


Ruediger Berlich wrote:

> Do you have further suggestions for ways of influencing the
> Boost.Serialization library ?

I have a very similar scenario where version tracking does not play an
important role. In this case it is possible to create a single archive
and stream per, say, connection as opposed to creating them for every
object (de)serialized. This was a huge performance boost in my case.

What I do is I create a Serializer class for every connection. All the
(de)serialization is done through it. Note that for this to work all
objects (de)serialized should have tracking turned off.

template <typename InputArchive, typename OutputArchive>
class Serializer
{
     Serializer() :
         iArchive_( iStream_, boost::archive::no_header ),
         oArchive_( oStream_, boost::archive::no_header )
     {}

     template <typename T>
     T const deserialize( std::string const & data )
     {
         resetStream( iStream_ );
         iStream_.write( data.data(), data.size() );
         T result;
         iArchive_ >> result;
         return result;
     }

     template <typename T>
     std::string const serialize( T const & object )
     {
         resetStream( oStream_ );
         oArchive << object;
         return oStream_.str();
     }

private:
     void resetStream( std::stringstream & stream )
     {
         stream.str( "" );
         stream.clear();
     }

private:
     std::stringstream iStream_;
     std::stringstream oStream_;
     InputArchive iArchive_;
     OutputArchive oArchive_;
};

This could be improved further:

1)By replacing stringstreams with something more lightweight.

2)Ideally these serialize methods should have some kind of compile time
assertion that object has serialization tracking turned off.

I'm not quite sure whether this approach is fully supported by the
boost::serialization interface and if this could could be broken by
future versions. OTOH it has been working well with last 6-7 boost
releases (1.41 is the last one I tested).

HTH


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net