Re: [ROOT] Performance of Streamers -- code generated for STL vectors.

From: Aaron Dominguez (DADominguez@lbl.gov)
Date: Mon Dec 03 2001 - 15:39:29 MET


Hi Walter and Rene,

Instead of the for() loop for reading/writing the std::vector, I've been using
ReadFastArray/WriteFastArray, like so:

if (R__b.IsReading()) {
   int n;
   R__b >> n;
   fCoeff.resize(n);
   R__b.ReadFastArray(&fCoeff[0],n);
}
else {
   R__b.WriteVersion(MyClass::IsA());
   R__b << fCoeff.size();
   R__b.WriteFastArray(&fCoeff[0],fCoeff.size());
}


I'm pretty sure that this is ok since I believe that std::vectors are supposed
to be contiguous in memory from beginning to end.  At least this is working
for us at CDF with ROOT v3.01/06a, KCC v4.0f or gcc v3.  And we've always had
great success speeding up streamers with Read/WriteFastArray.


Cheers,
Aaron.



Rene Brun wrote:

> Hi Walter,
>
> I have implemented your suggestion in rootcint. Now in CVS.
>
>
> Walter F.J. Mueller wrote:
> >
> > Hi ROOTers,
> >
> > Currently (ROOT 3.02/03) the read part of an automatically generated
> > streamer contains for reading of a STL vector code like
> >
> >     fCoeff.clear();
> >     int R__i, R__n;
> >     R__b >> R__n;
> >     for (R__i = 0; R__i < R__n; R__i++) {
> >       double R__t;
> >       R__b >> R__t;
> >       fCoeff.push_back(R__t);
> >     }
> >
> > This works, but can cause an unnecessary amount of reallocations of the
> > vector. Since the number of elements to be inserted is known up front
> > this can be avoided by requesting enough capacity before the elements
> > are inserted by calling the reserve() method ahead of time, like in
> >
> >     fCoeff.clear();
> >     int R__i, R__n;
> >     R__b >> R__n;
> >     fCoeff.reserve(R__n);
> >     for (R__i = 0; R__i < R__n; R__i++) {
> >     ... as before ...
> >
> > This ensures that at most one reallocation is done when a vector is read.
> > Only vectors and strings provide capacity management, so in practice this
> > is only relevant for STL vectors and can't be applied to deque's ect.
> >
> > It be nice if rootcint would generate code like this. I haven't checked
> > what the StreamerInfo based I/O does, but also there this might help.
> >
> >                  Cheers, Walter
> >



This archive was generated by hypermail 2b29 : Tue Jan 01 2002 - 17:51:10 MET