Boost logo

Boost :

From: Giovanni Piero Deretta (gpderetta_at_[hidden])
Date: 2007-12-04 07:03:45


On Dec 4, 2007 12:19 PM, Martin Bonner <Martin.Bonner_at_[hidden]> wrote:
> From: Hervé Brönnimann
> >> On Dec 4, 2007, at 5:13 AM, Jens Seidel wrote:
> >> There is only one solution: remove the buggy casting code and
> >> replace it with a proper one as suggested. Whether the proper
> >> one is slower or not is unimportant!
> >
> > I strongly disagree. Performance of string serialization is
> > important, as Robert posted in a previous msg. For that reason, the
> > use of const_cast<char*>s.data() seems justified.
>
> This is the heart of the matter. What is the difference in performance between relying on the const_cast, and the strictly legal version using a vector buffer:
>
> text_iarchive_impl<Archive>::load(std::string &s)
> {
> std::size_t size;
> * this->This() >> size;
> // skip separating space
> is.get();
>
> // Read into a buffer.
> std::vector<char> v(size);
> is.read( &v[0], size );
>
> // Return the string.
> std::swap( s, std::string( v.begin(), v.end() ));
> }
>
> Clearly there are inefficiencies there, but are they bad enough to be worth the const_cast?
>

[of course I think you meant std::string(...).swap(s)]

What about something like this (warning: pseudo code)

text_iarchive_impl<Archive>::load(std::string &s)
{
   static const size_t static_buffer_size = 256;
   std::size_t size;
   std::string temp;
   * this->This() >> size;
   // skip separating space
   is.get();

   size_t left = size;
   while(left) {
     // Read into a buffer.
    boost::array<char, static_buffer_size> v(size);
    size_t read_n = std::min(left, v.size());
    is.read( &v[0], read_n);
    left -= read_n;
      // Return the string.
   temp.insert(v.begin(), v.begin() + read_n);
 }
  temp.swap(s);
}

This way you do not have to pay for an extra allocation for vector. By
choosing static_buffer_size appropriately you will only do one
allocation of temp for small strings (which will dominate over the
cost of copying the small buffer) and for big strings it is likely
that the cost of the read() call will dominate over everything else.

HTH,

gpd


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk