public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/23425] New: vector::clear should be manually inlined
@ 2005-08-16 18:26 tkho at ucla dot edu
  2005-08-16 18:32 ` [Bug libstdc++/23425] " tkho at ucla dot edu
                   ` (5 more replies)
  0 siblings, 6 replies; 10+ messages in thread
From: tkho at ucla dot edu @ 2005-08-16 18:26 UTC (permalink / raw)
  To: gcc-bugs

vector::clear calls erase(begin(), end()), which has an unnecessary call to
memmove, in addition to a function call overhead. The fix results in a
2-instruction clear for simple types.

Compiling the following (-O2 -save-temps) shows the difference:

#include <vector>

using std::vector;

int main() {
  vector<int> v;

  asm("#start1");
  v.clear();
  asm("#end1");

  asm("#start2");
  v.erase(v.begin(), v.end());
  asm("#end2");
}

This was a notable performance difference between libstdc++ stl and stlport I
found in profiling a large application.

-- 
           Summary: vector::clear should be manually inlined
           Product: gcc
           Version: 4.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: libstdc++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: tkho at ucla dot edu
                CC: dank at kegel dot com,gcc-bugs at gcc dot gnu dot org
  GCC host triplet: i686-unknown-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23425


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug libstdc++/23425] vector::clear should be manually inlined
  2005-08-16 18:26 [Bug libstdc++/23425] New: vector::clear should be manually inlined tkho at ucla dot edu
@ 2005-08-16 18:32 ` tkho at ucla dot edu
  2005-08-16 18:44 ` [Bug libstdc++/23425] New: " Andrew Pinski
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 10+ messages in thread
From: tkho at ucla dot edu @ 2005-08-16 18:32 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From tkho at ucla dot edu  2005-08-16 18:25 -------
Created an attachment (id=9503)
 --> (http://gcc.gnu.org/bugzilla/attachment.cgi?id=9503&action=view)
change to vector::clear


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23425


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Bug libstdc++/23425] New: vector::clear should be manually inlined
  2005-08-16 18:26 [Bug libstdc++/23425] New: vector::clear should be manually inlined tkho at ucla dot edu
  2005-08-16 18:32 ` [Bug libstdc++/23425] " tkho at ucla dot edu
@ 2005-08-16 18:44 ` Andrew Pinski
  2005-08-16 18:46 ` [Bug libstdc++/23425] " pinskia at physics dot uc dot edu
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 10+ messages in thread
From: Andrew Pinski @ 2005-08-16 18:44 UTC (permalink / raw)
  To: gcc-bugzilla; +Cc: gcc-bugs

> 
> vector::clear calls erase(begin(), end()), which has an unnecessary call to
> memmove, in addition to a function call overhead. The fix results in a
> 2-instruction clear for simple types.
> 
> Compiling the following (-O2 -save-temps) shows the difference:
> 
> #include <vector>
> 
> using std::vector;
> 
> int main() {
>   vector<int> v;
> 
>   asm("#start1");
>   v.clear();
>   asm("#end1");
> 
>   asm("#start2");
>   v.erase(v.begin(), v.end());
>   asm("#end2");
> }

as I mentioned on IRC, just marking erase as inline or figuring out why erase is not being inlined into clear
will fix this issue.  To the point where we have an empty loop which is removed with -funsafe-loop-optimizations
and a couple extra instructions which should be fixed with the tree combiner (but that is for 4.2 and not for
4.1).

-- Pinski


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug libstdc++/23425] vector::clear should be manually inlined
  2005-08-16 18:26 [Bug libstdc++/23425] New: vector::clear should be manually inlined tkho at ucla dot edu
  2005-08-16 18:32 ` [Bug libstdc++/23425] " tkho at ucla dot edu
  2005-08-16 18:44 ` [Bug libstdc++/23425] New: " Andrew Pinski
@ 2005-08-16 18:46 ` pinskia at physics dot uc dot edu
  2005-08-16 20:26 ` fang at csl dot cornell dot edu
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 10+ messages in thread
From: pinskia at physics dot uc dot edu @ 2005-08-16 18:46 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at physics dot uc dot edu  2005-08-16 18:44 -------
Subject: Re:  New: vector::clear should be manually inlined

> 
> vector::clear calls erase(begin(), end()), which has an unnecessary call to
> memmove, in addition to a function call overhead. The fix results in a
> 2-instruction clear for simple types.
> 
> Compiling the following (-O2 -save-temps) shows the difference:
> 
> #include <vector>
> 
> using std::vector;
> 
> int main() {
>   vector<int> v;
> 
>   asm("#start1");
>   v.clear();
>   asm("#end1");
> 
>   asm("#start2");
>   v.erase(v.begin(), v.end());
>   asm("#end2");
> }

as I mentioned on IRC, just marking erase as inline or figuring out why erase is not being inlined into clear
will fix this issue.  To the point where we have an empty loop which is removed with -funsafe-loop-optimizations
and a couple extra instructions which should be fixed with the tree combiner (but that is for 4.2 and not for
4.1).

-- Pinski


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23425


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug libstdc++/23425] vector::clear should be manually inlined
  2005-08-16 18:26 [Bug libstdc++/23425] New: vector::clear should be manually inlined tkho at ucla dot edu
                   ` (2 preceding siblings ...)
  2005-08-16 18:46 ` [Bug libstdc++/23425] " pinskia at physics dot uc dot edu
@ 2005-08-16 20:26 ` fang at csl dot cornell dot edu
  2005-08-17  2:26 ` pinskia at gcc dot gnu dot org
  2005-08-18  7:26 ` phython at gcc dot gnu dot org
  5 siblings, 0 replies; 10+ messages in thread
From: fang at csl dot cornell dot edu @ 2005-08-16 20:26 UTC (permalink / raw)
  To: gcc-bugs



-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |fang at csl dot cornell dot
                   |                            |edu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23425


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug libstdc++/23425] vector::clear should be manually inlined
  2005-08-16 18:26 [Bug libstdc++/23425] New: vector::clear should be manually inlined tkho at ucla dot edu
                   ` (3 preceding siblings ...)
  2005-08-16 20:26 ` fang at csl dot cornell dot edu
@ 2005-08-17  2:26 ` pinskia at gcc dot gnu dot org
  2005-08-18  7:26 ` phython at gcc dot gnu dot org
  5 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2005-08-17  2:26 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2005-08-17 02:23 -------
Confirmed, there are two different issues here because it looks like the pool allocator injects code 
which initializes a variable, even though there is a different way of doing the initialization.  It should be 
doing the say initialization as the cout/cin/cerr initializes its streams.
This causes us when we call erase:
  if (*_ZGVZN9__gnu_cxx20__common_pool_policyINS_6__poolELb1EE11_S_get_poolEvE7_S_pool.62 == 
0) goto <L12>; else goto <L2>;

<L12>:;
  _S_pool.D.8154._M_options._M_align = 8;
  _S_pool.D.8154._M_options._M_max_bytes = 128;
  _S_pool.D.8154._M_options._M_min_bin = 8;
  _S_pool.D.8154._M_options._M_chunk_size = 4080;
  _S_pool.D.8154._M_options._M_max_threads = 4096;
  _S_pool.D.8154._M_options._M_freelist_headroom = 10;
  D.12526 = getenv (&"GLIBCXX_FORCE_NEW"[0]);
  _S_pool.D.8154._M_options._M_force_new = D.12526 != 0B;
  _S_pool.D.8154._M_binmap = 0B;
  _S_pool.D.8154._M_init = 0;
  _S_pool._M_bin = 0B;
  _S_pool._M_bin_size = 1;
  _S_pool._M_thread_freelist = 0B;
  _S_pool._M_once = 0;
  *_ZGVZN9__gnu_cxx20__common_pool_policyINS_6__poolELb1EE11_S_get_poolEvE7_S_pool.62 = 1;

which blocks a lot of the other optimizations because there is a function call and there is always a 
branch.  This is not a problem on PPC-darwin where we don't have this "junk".

Second we don't inline erase into clear for some reason.  I could not figure it out from the dumps 
maybe someone else will be able to.

The rest is for the reporter really (and any one who is writting missed optimization bugs)
A better way of seeing the different is:

#include <vector>
std::vector<int> v;
void clear1() { v.clear(); }
void clear2(){  v.erase(v.begin(), v.end()); }

As using asm("#start1"); can be moved around and for your example at -O3, we actually get an empty 
function as v is known to be unused.


-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
             Status|UNCONFIRMED                 |NEW
     Ever Confirmed|                            |1
   GCC host triplet|i686-unknown-linux-gnu      |
 GCC target triplet|                            |i686-*-linux-gnu
           Keywords|                            |missed-optimization
   Last reconfirmed|0000-00-00 00:00:00         |2005-08-17 02:23:28
               date|                            |


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23425


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug libstdc++/23425] vector::clear should be manually inlined
  2005-08-16 18:26 [Bug libstdc++/23425] New: vector::clear should be manually inlined tkho at ucla dot edu
                   ` (4 preceding siblings ...)
  2005-08-17  2:26 ` pinskia at gcc dot gnu dot org
@ 2005-08-18  7:26 ` phython at gcc dot gnu dot org
  5 siblings, 0 replies; 10+ messages in thread
From: phython at gcc dot gnu dot org @ 2005-08-18  7:26 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From phython at gcc dot gnu dot org  2005-08-18 07:21 -------
 I see absolutely no difference between the code generated for clear1() and
clear2() on powerpc-linux-gnu.  This includes the final_cleanup tree dump and
the assembly output using g++ -O2.

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23425


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug libstdc++/23425] vector::clear should be manually inlined
       [not found] <bug-23425-11148@http.gcc.gnu.org/bugzilla/>
  2005-11-02 10:29 ` pcarlini at suse dot de
  2005-11-02 10:29 ` pcarlini at suse dot de
@ 2005-11-02 16:24 ` paolo at gcc dot gnu dot org
  2 siblings, 0 replies; 10+ messages in thread
From: paolo at gcc dot gnu dot org @ 2005-11-02 16:24 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from paolo at gcc dot gnu dot org  2005-11-02 16:24 -------
Subject: Bug 23425

Author: paolo
Date: Wed Nov  2 10:27:54 2005
New Revision: 106379

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=106379
Log:
2005-11-02  Thomas Kho  <tkho@ucla.edu>

        PR libstdc++/23425
        * include/bits/stl_vector.h (vector<>::clear): Open code
        in terms of _Destroy.

2005-11-02  Paolo Carlini  <pcarlini@suse.de>

        * include/bits/vector.tcc (vector<>::_M_fill_assign): Qualify fill_n.

Modified:
    trunk/libstdc++-v3/ChangeLog
    trunk/libstdc++-v3/include/bits/stl_vector.h
    trunk/libstdc++-v3/include/bits/vector.tcc


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23425


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug libstdc++/23425] vector::clear should be manually inlined
       [not found] <bug-23425-11148@http.gcc.gnu.org/bugzilla/>
  2005-11-02 10:29 ` pcarlini at suse dot de
@ 2005-11-02 10:29 ` pcarlini at suse dot de
  2005-11-02 16:24 ` paolo at gcc dot gnu dot org
  2 siblings, 0 replies; 10+ messages in thread
From: pcarlini at suse dot de @ 2005-11-02 10:29 UTC (permalink / raw)
  To: gcc-bugs



-- 

pcarlini at suse dot de changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |4.1.0


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23425


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug libstdc++/23425] vector::clear should be manually inlined
       [not found] <bug-23425-11148@http.gcc.gnu.org/bugzilla/>
@ 2005-11-02 10:29 ` pcarlini at suse dot de
  2005-11-02 10:29 ` pcarlini at suse dot de
  2005-11-02 16:24 ` paolo at gcc dot gnu dot org
  2 siblings, 0 replies; 10+ messages in thread
From: pcarlini at suse dot de @ 2005-11-02 10:29 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from pcarlini at suse dot de  2005-11-02 10:29 -------
Fixed for 4.1.0.


-- 

pcarlini at suse dot de changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23425


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2005-11-02 16:24 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-08-16 18:26 [Bug libstdc++/23425] New: vector::clear should be manually inlined tkho at ucla dot edu
2005-08-16 18:32 ` [Bug libstdc++/23425] " tkho at ucla dot edu
2005-08-16 18:44 ` [Bug libstdc++/23425] New: " Andrew Pinski
2005-08-16 18:46 ` [Bug libstdc++/23425] " pinskia at physics dot uc dot edu
2005-08-16 20:26 ` fang at csl dot cornell dot edu
2005-08-17  2:26 ` pinskia at gcc dot gnu dot org
2005-08-18  7:26 ` phython at gcc dot gnu dot org
     [not found] <bug-23425-11148@http.gcc.gnu.org/bugzilla/>
2005-11-02 10:29 ` pcarlini at suse dot de
2005-11-02 10:29 ` pcarlini at suse dot de
2005-11-02 16:24 ` paolo at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).