performance patch for stl_vector.h

Wayne Feick waf@brunz.org
Mon Apr 7 22:41:00 GMT 2008


(embarrassed blush) 

You are, of course, right. That should have been __n, not n. This
version should compile fine. Sorry for the mistake.

Wayne.



Index: include/bits/stl_vector.h
===================================================================
--- include/bits/stl_vector.h   (revision 133987)
+++ include/bits/stl_vector.h   (working copy)
@@ -114,6 +114,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GL
       _Vector_base(size_t __n, const allocator_type& __a)
       : _M_impl(__a)
       {
+        if (__n == 0) return;
        this->_M_impl._M_start = this->_M_allocate(__n);
        this->_M_impl._M_finish = this->_M_impl._M_start;
        this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;


On Mon, 2008-04-07 at 15:08 -0700, Joe Buck wrote:
> On Mon, Apr 07, 2008 at 09:52:28PM +0000, Wayne Feick wrote:
> > We've found that the following patch to avoid allocating storage for 0
> > length vectors is a significant performance win in our product. I'd like
> > to get it folded back into libstdc++.
> > 
> > Thanks,
> > Wayne.
> > 
> > 
> > Index: include/bits/stl_vector.h
> > ===================================================================
> > --- include/bits/stl_vector.h   (revision 133987)
> > +++ include/bits/stl_vector.h   (working copy)
> > @@ -114,6 +114,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GL
> >        _Vector_base(size_t __n, const allocator_type& __a)
> >        : _M_impl(__a)
> >        {
> > +        if (n == 0) return;
> >         this->_M_impl._M_start = this->_M_allocate(__n);
> >         this->_M_impl._M_finish = this->_M_impl._M_start;
> >         this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
> 
> This can't be right; it won't even compile.  Are you sure that this
> is what you intended to submit?  There's no "n", no initialization for
> the members.
> 
> 



More information about the Gcc-patches mailing list