This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Patch] libstdc++/23425


Hi,

I think it makes perfect sense to apply the simple patch submitted as
part of the PR: in case of clear() we know a lot more of a generic
erase(iterator, iterator) and, indeed, "manually inlining" the current
erase call and cleaning up the result we end up with the very same code
of ~vector() (+ adjustment for the _M_finish pointer). The impact on the
generated assembly is impressive (x86, -O2 -fomit-frame-pointer):

00000000 <clear1()>:
   0:   a1 00 00 00 00          mov    0x0,%eax
   5:   a3 04 00 00 00          mov    %eax,0x4
   a:   c3                      ret

00000010 <clear2()>:
  10:   83 ec 1c                sub    $0x1c,%esp
  13:   8b 15 04 00 00 00       mov    0x4,%edx
  19:   8d 44 24 18             lea    0x18(%esp),%eax
  1d:   89 04 24                mov    %eax,(%esp)
  20:   89 54 24 0c             mov    %edx,0xc(%esp)
  24:   8b 15 00 00 00 00       mov    0x0,%edx
  2a:   89 54 24 08             mov    %edx,0x8(%esp)
  2e:   ba 00 00 00 00          mov    $0x0,%edx
  33:   89 54 24 04             mov    %edx,0x4(%esp)
  37:   e8 fc ff ff ff          call   38 <clear2()+0x28>
  3c:   83 ec 04                sub    $0x4,%esp
  3f:   83 c4 1c                add    $0x1c,%esp
  42:   c3                      ret

Tested x86-linux. Will wait 'til tomorrow in case of contrary opinions.

Paolo.

//////////////////
2005-11-02  Thomas Kho  <tkho@ucla.edu>

	PR libstdc++/23425
	* include/bits/stl_vector.h (vector<>::clear): Open code
	in terms of _Destroy.
Index: include/bits/stl_vector.h
===================================================================
--- include/bits/stl_vector.h	(revision 106320)
+++ include/bits/stl_vector.h	(working copy)
@@ -742,7 +742,11 @@
        */
       void
       clear()
-      { erase(begin(), end()); }
+      {
+	std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
+		      _M_get_Tp_allocator());
+	this->_M_impl._M_finish = this->_M_impl._M_start;
+      }
 
     protected:
       /**

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]