This is the mail archive of the libstdc++@sourceware.cygnus.com 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]

Re: [Fwd: too much copy in map]


"Jeremy G. Siek" wrote:
> 
> And BTW, the !spam thing in your return address is annoying.

what's more annoying when the email collecting robots find my email 
address and than send me spam. anyway if you are using a good mailer
than it's not a problem since my Reply-to: is set to the correct
address and if you press reply the good address will punt into the
To: line.

> Well, I'm not sure what I'm about to say will work for your
> real program, but here's what you can do for this toy example:
> 
> Map::value_type v;
> v.first = 1;
> m.insert(v);
> 
> However, I'm not sure why you are worried about 1 extra copy.
> For small objects, the extra copy gets optimized
> away with a good compiler. For large objects... well you

I thing it's better to optimize code by hand if possible.
anyway your example is wrong. you can't set v.first, since it's
a const int and that's the reason (since pair has no constructor
which inicialize just, the first) why we have to call to copy in 
the this implementation. but operator[] can be implement without
this extra copy, without hurt the standard (but the other insert
can't).

> probably shouldn't put a large object directly into the map... instead
> store pointers in the map and define a custom compare operator.

I thing the best way to avoid all memory management in a code, but 
(unfortunately) in this case stl always copy (sometimes more than
necessary).
 
> Levente Farkas wrote:
> >
> > ok. then the proper question:
> > why is not there a way to insert something into a map with just
> > ONE copy contructor call ?
> > yes you can said that this's the standard, but if there be an
> > insert(const key_type&, const mapped_type&) or at least
> > operator[const key_type&] could have implement in a way to call
> > just one copy constructor.
> >
> > > The first copy is even before you get to the insert().
> > > The "Map::value_type(1,A())" creates a pair, *copying* A()
> > > into the pair's data member. So there is only one copy
> > > inside insert(), and that is necessary.
> 
> --
> ------------------------------------------------------------------------
>  Jeremy Siek                        email: jsiek@engr.sgi.com
>  Ph.D. Candidate               cell-phone: (415) 377-5814
>  Univ. of Notre Dame                phone: (650) 933-8724
>  C++ Library & Compiler Group         fax: (650) 932-0127
>  Silicon Graphics Inc.                www: http://www.lsc.nd.edu/~jsiek/
> ------------------------------------------------------------------------


-- 
 -- lfarkas
 "The only thing worse than not knowing the truth is
  ruining the bliss of ignorance."

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